1 | <?php |
||
28 | class ServiceAdapter extends AbstractAdapter |
||
29 | { |
||
30 | /** @var ContainerBuilder */ |
||
31 | private $container; |
||
32 | /** @var int */ |
||
33 | private static $parameterIndex = 0; |
||
34 | |||
35 | /** |
||
36 | * ServiceAdapter constructor. |
||
37 | * |
||
38 | * @param ConfigurationInterface $configuration |
||
39 | */ |
||
40 | 10 | public function __construct(ConfigurationInterface $configuration) |
|
46 | |||
47 | /** |
||
48 | * Returns true if the given service is registered. |
||
49 | * |
||
50 | * @param string $identifier |
||
51 | * @return bool |
||
52 | */ |
||
53 | 1 | public function has(string $identifier) : bool |
|
59 | |||
60 | /** |
||
61 | * Gets a service. |
||
62 | * |
||
63 | * @param string $identifier |
||
64 | * @return object |
||
65 | */ |
||
66 | 8 | public function get(string $identifier) |
|
83 | |||
84 | /** |
||
85 | * Registers the service into the container. |
||
86 | * |
||
87 | * @param string $identifier |
||
88 | * @return ServiceAdapter |
||
89 | */ |
||
90 | 8 | private function registerServiceToContainer(string $identifier) : ServiceAdapter |
|
91 | { |
||
92 | // At this point the service must be in the library |
||
93 | 8 | if (!isset($this->serviceLibrary[$identifier])) { |
|
94 | 2 | throw new InvalidArgumentException( |
|
95 | 2 | sprintf('Invalid service name: %s', $identifier), |
|
96 | 2 | 1000 |
|
97 | ); |
||
98 | } |
||
99 | |||
100 | // Create the definition. |
||
101 | 8 | $definition = new Definition($this->serviceLibrary[$identifier][self::SERVICE_CLASS]); |
|
102 | 8 | $definition->setShared($this->serviceLibrary[$identifier][self::SERVICE_SHARE]); |
|
103 | |||
104 | // Register the service in the container. |
||
105 | 8 | $service = $this->container->setDefinition($identifier, $definition); |
|
106 | |||
107 | // Add arguments. |
||
108 | 8 | $argumentList = $this->setArgumentListReferences($this->serviceLibrary[$identifier][self::SERVICE_ARGUMENTS]); |
|
109 | 8 | foreach ($argumentList as $parameter) { |
|
110 | // Create a normalized name for the argument. |
||
111 | 8 | $serviceClass = $this->serviceLibrary[$identifier][self::SERVICE_CLASS]; |
|
112 | 8 | $normalizedName = $this->getNormalizedName($serviceClass, $parameter); |
|
113 | 8 | $this->container->setParameter($normalizedName, $parameter); |
|
114 | 8 | $service->addArgument('%'.$normalizedName.'%'); |
|
115 | } |
||
116 | |||
117 | // Register method callings. |
||
118 | 8 | foreach ($this->serviceLibrary[$identifier][self::SERVICE_METHOD_CALL] as $methodCallList) { |
|
119 | 1 | $method = $methodCallList[0]; |
|
120 | 1 | $argumentList = $this->setArgumentListReferences($methodCallList[1] ?? []); |
|
121 | 1 | $service->addMethodCall($method, $argumentList); |
|
122 | } |
||
123 | |||
124 | 8 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Tries to identify referce services in the argument list. |
||
129 | * |
||
130 | * @param array $argumentList |
||
131 | * @return array |
||
132 | */ |
||
133 | 8 | private function setArgumentListReferences(array $argumentList) : array |
|
134 | { |
||
135 | 8 | foreach ($argumentList as $key => &$value) { |
|
136 | // Associative array keys marks literal values |
||
137 | 8 | if (!is_numeric($key)) { |
|
138 | 8 | continue; |
|
139 | } |
||
140 | |||
141 | // Try to get the service. If exists (or can be registered), then it can be referenced too. |
||
142 | try { |
||
143 | 7 | $this->get($value); |
|
144 | 7 | $value = new Reference($value); |
|
145 | 1 | } catch (Exception $e) { |
|
146 | // Not a valid service: no action, go on; |
||
147 | 7 | continue; |
|
148 | } |
||
149 | } |
||
150 | |||
151 | 8 | return $argumentList; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Creates a safe normalized name. |
||
156 | * |
||
157 | * @param string $className |
||
158 | * @param mixed $parameter |
||
159 | * @return string |
||
160 | */ |
||
161 | 8 | private function getNormalizedName(string $className, $parameter) : string |
|
170 | |||
171 | /** |
||
172 | * Register the service object instance. |
||
173 | * |
||
174 | * @param string $identifier |
||
175 | * @param object $serviceInstance |
||
176 | * @return ServiceInterface |
||
177 | */ |
||
178 | 8 | public function registerServiceInstance(string $identifier, $serviceInstance) : ServiceInterface |
|
210 | } |
||
211 |