This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractServiceFactoryExtension.php
236 lines (213 loc) · 5.04 KB
/
AbstractServiceFactoryExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Services\Common;
use Ulrack\Services\Common\ServiceFactoryInterface;
abstract class AbstractServiceFactoryExtension implements ServiceFactoryExtensionInterface
{
/**
* Contains the key of the service extension.
*
* @var string
*/
private $key;
/**
* Contains the parameters for the extension.
*
* @var array
*/
private $parameters;
/**
* Contains the compiled services.
*
* @var array
*/
private $services;
/**
* Contains the callable to retrieve the hooks for a key.
*
* @var callable
*/
private $getHooks;
/**
* Contains the main service factory.
*
* @var ServiceFactoryInterface
*/
private $serviceFactory;
/**
* Constructor
*
* @param ServiceFactoryInterface $serviceFactory
* @param string $key
* @param array $parameters
* @param array $services
* @param callable $getHooks
* @param array $internalServices
*/
public function __construct(
ServiceFactoryInterface $serviceFactory,
string $key,
array $parameters,
array $services,
callable $getHooks,
array $internalServices = []
) {
$this->serviceFactory = $serviceFactory;
$this->key = $key;
$this->parameters = $parameters;
$this->services = $services;
$this->internalServices = $internalServices;
$this->getHooks = $getHooks;
}
/**
* Invokes the defined connected hooks.
*
* @param string $method
* @param array $input
*
* @return array
*/
private function invokeOnHooks(string $method, array $input): array
{
foreach (($this->getHooks)($this->getKey()) as $hook) {
$input = array_merge(
$input,
call_user_func_array([$hook, $method], $input)
);
}
return $input;
}
/**
* Hooks in before the creation of a service.
*
* @param string $serviceKey
* @param array $parameters
*
* @return array
*/
public function preCreate(
string $serviceKey,
array $parameters = []
): array {
return $this->invokeOnHooks(
'preCreate',
['serviceKey' => $serviceKey, 'parameters' => $parameters]
);
}
/**
* Hooks in after the creation of a service.
*
* @param string $serviceKey
* @param mixed $return
* @param array $parameters
*
* @return array
*/
public function postCreate(
string $serviceKey,
$return,
array $parameters = []
): array {
return $this->invokeOnHooks(
'postCreate',
[
'serviceKey' => $serviceKey,
'return' => $return,
'parameters' => $parameters
]
);
}
/**
* Retrieves the internal service.
*
* @param string $serviceKey
*
* @return object
*/
public function getInternalService(string $serviceKey): object
{
return $this->internalServices[$serviceKey];
}
/**
* Retrieves the registry.
*
* @return array
*/
public function getServices(): array
{
return $this->services;
}
/**
* Retrieves the key of the service.
*
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* Retrieves the requested extension parameter.
*
* @param string $key
*
* @return mixed
*/
public function getParameter(string $key)
{
return $this->parameters[$key];
}
/**
* Retrieves the parameters for the extension.
*
* @return array
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* Determines whether a string is a reference to another registration.
*
* @param string $input
*
* @return bool
*/
public function isReference(string $input): bool
{
return preg_match('/^@{.+}$/', $input) === 1;
}
/**
* Calls the main service factory to retrieve a value.
*
* @param string $service
*
* @return mixed
*/
public function superCreate(string $service)
{
return $this->serviceFactory->create($service);
}
/**
* Resolves references in an object to other services if applicable.
*
* @param mixed $value
*
* @return mixed
*/
public function resolveReferences($value)
{
if (is_string($value) && $this->isReference($value)) {
$value = $this->superCreate(trim($value, '@{}'));
}
if (is_array($value)) {
foreach ($value as $key => $item) {
$value[$key] = $this->resolveReferences($item);
}
}
return $value;
}
}