1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino™ (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoAppLib\Application\Traits; |
12
|
|
|
|
13
|
|
|
use WebinoAppLib\Exception; |
14
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
15
|
|
|
use Zend\ServiceManager\FactoryInterface; |
16
|
|
|
use Zend\ServiceManager\ServiceManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait ServiceProviderTrait |
20
|
|
|
*/ |
21
|
|
|
trait ServicesTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ServiceManager |
25
|
|
|
*/ |
26
|
|
|
private $services; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return ServiceManager |
30
|
|
|
*/ |
31
|
|
|
public function getServices() |
32
|
|
|
{ |
33
|
|
|
return $this->services; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return registered service |
38
|
|
|
* |
39
|
|
|
* @param string $service Service name |
40
|
|
|
* @return mixed |
41
|
|
|
* @throws Exception\UnknownServiceException |
42
|
|
|
*/ |
43
|
|
|
public function get($service) |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
|
|
return $this->getServices()->get($service); |
47
|
|
|
} catch (ServiceNotFoundException $exc) { |
48
|
|
|
throw (new Exception\UnknownServiceException('Unable to get an instance for %s', null, $exc)) |
49
|
|
|
->format($service); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function set($service, $factory = null) |
57
|
|
|
{ |
58
|
|
|
$services = $this->getServices(); |
59
|
|
|
|
60
|
|
|
if ($factory instanceof FactoryInterface |
61
|
|
|
|| is_callable($factory) |
62
|
|
|
|| (is_string($factory) && class_exists($factory)) |
63
|
|
|
) { |
64
|
|
|
// factory |
65
|
|
|
$services->setFactory($service, $factory); |
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (null !== $factory && is_string($service)) { |
70
|
|
|
// service object |
71
|
|
|
$services->setService($service, $factory); |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// invokable |
76
|
|
|
if (is_array($service)) { |
77
|
|
|
$services->setInvokableClass(key($service), current($service)); |
78
|
|
|
} elseif (is_string($service)) { |
79
|
|
|
$services->setInvokableClass($service, $service); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function has($service) |
89
|
|
|
{ |
90
|
|
|
return $this->getServices()->has((string) $service); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Require service from services into application |
95
|
|
|
* |
96
|
|
|
* @param string $service Service name |
97
|
|
|
* @throws Exception\DomainException Unable to get service |
98
|
|
|
*/ |
99
|
|
|
protected function requireService($service) |
100
|
|
|
{ |
101
|
|
|
if (!$this->services->has($service)) { |
102
|
|
|
throw (new Exception\DomainException('Unable to get required application service %s'))->format($service); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->setService($service); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set optional service from services into application |
110
|
|
|
* |
111
|
|
|
* @param string $service Service name |
112
|
|
|
*/ |
113
|
|
|
protected function optionalService($service) |
114
|
|
|
{ |
115
|
|
|
$this->services->has($service) and $this->setService($service); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $service |
120
|
|
|
*/ |
121
|
|
|
private function setService($service) |
122
|
|
|
{ |
123
|
|
|
call_user_func([$this, 'set' . $service], $this->services->get($service), false); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @param mixed $service |
129
|
|
|
*/ |
130
|
|
|
protected function setServicesService($name, $service) |
131
|
|
|
{ |
132
|
|
|
$this->services |
133
|
|
|
->setAllowOverride(true) |
134
|
|
|
->setService($name, $service) |
135
|
|
|
->setAllowOverride(false); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.