This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Generator.php
320 lines (282 loc) · 11.3 KB
/
Generator.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
// @todo refactor to use new Definition interface
namespace Zend\Di\ServiceLocator;
use Zend\Di\Di,
Zend\CodeGenerator\Php as CodeGen,
Zend\Di\DependencyInjectionInterface,
Zend\Di\Exception;
class Generator
{
protected $containerClass = 'ApplicationContext';
protected $injector;
protected $namespace;
/**
* Constructor
*
* Requires a DependencyInjection manager on which to operate.
*
* @param DependencyInjectionInterface $injector
* @return void
*/
public function __construct(Di $injector)
{
$this->injector = new DependencyInjectorProxy($injector);
}
/**
* Set the class name for the generated service locator container
*
* @param string $name
* @return Generator
*/
public function setContainerClass($name)
{
$this->containerClass = $name;
return $this;
}
/**
* Set the namespace to use for the generated class file
*
* @param string $namespace
* @return Generator
*/
public function setNamespace($namespace)
{
$this->namespace = $namespace;
return $this;
}
/**
* Construct, configure, and return a PHP classfile code generation object
*
* Creates a Zend\CodeGenerator\Php\PhpFile object that has
* created the specified class and service locator methods.
*
* @param null|string $filename
* @return CodeGen\PhpFile
*/
public function getCodeGenerator($filename = null)
{
$injector = $this->injector;
$im = $injector->instanceManager();
$indent = ' ';
$aliases = $this->reduceAliases($im->getAliases());
$caseStatements = array();
$getters = array();
$definitions = $injector->definitions();
foreach ($definitions->getClasses() as $name) {
$getter = $this->normalizeAlias($name);
$meta = $injector->get($name);
$params = $meta->getParams();
// Build parameter list for instantiation
foreach ($params as $key => $param) {
if (null === $param || is_scalar($param) || is_array($param)) {
$string = var_export($param, 1);
if (strstr($string, '::__set_state(')) {
throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
}
$params[$key] = $string;
} elseif ($param instanceof GeneratorInstance) {
$params[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
} else {
$message = sprintf('Unable to use object arguments when building containers. Encountered with "%s", parameter of type "%s"', $name, get_class($param));
throw new Exception\RuntimeException($message);
}
}
// Strip null arguments from the end of the params list
$reverseParams = array_reverse($params, true);
foreach ($reverseParams as $key => $param) {
if ('NULL' === $param) {
unset($params[$key]);
continue;
}
break;
}
// Create instantiation code
$creation = '';
$constructor = $meta->getConstructor();
if ('__construct' != $constructor) {
// Constructor callback
$callback = var_export($constructor, 1);
if (strstr($callback, '::__set_state(')) {
throw new Exception\RuntimeException('Unable to build containers that use callbacks requiring object instances');
}
if (count($params)) {
$creation = sprintf('$object = call_user_func(%s, %s);', $callback, implode(', ', $params));
} else {
$creation = sprintf('$object = call_user_func(%s);', $callback);
}
} else {
// Normal instantiation
$className = '\\' . ltrim($name, '\\');
$creation = sprintf('$object = new %s(%s);', $className, implode(', ', $params));
}
// Create method call code
$methods = '';
foreach ($meta->getMethods() as $key => $methodData) {
if (!isset($methodData['name']) && !isset($methodData['method'])) {
continue;
}
$methodName = isset($methodData['name']) ? $methodData['name'] : $methodData['method'];
$methodParams = $methodData['params'];
// Create method parameter representation
foreach ($methodParams as $key => $param) {
if (null === $param || is_scalar($param) || is_array($param)) {
$string = var_export($param, 1);
if (strstr($string, '::__set_state(')) {
throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
}
$methodParams[$key] = $string;
} elseif ($param instanceof GeneratorInstance) {
$methodParams[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
} else {
$message = sprintf('Unable to use object arguments when generating method calls. Encountered with class "%s", method "%s", parameter of type "%s"', $name, $methodName, get_class($param));
throw new Exception\RuntimeException($message);
}
}
// Strip null arguments from the end of the params list
$reverseParams = array_reverse($methodParams, true);
foreach ($reverseParams as $key => $param) {
if ('NULL' === $param) {
unset($methodParams[$key]);
continue;
}
break;
}
$methods .= sprintf("\$object->%s(%s);\n", $methodName, implode(', ', $methodParams));
}
// Generate caching statement
$storage = '';
if ($im->hasSharedInstance($name, $params)) {
$storage = sprintf("\$this->services['%s'] = \$object;\n", $name);
}
// Start creating getter
$getterBody = '';
// Create fetch of stored service
if ($im->hasSharedInstance($name, $params)) {
$getterBody .= sprintf("if (isset(\$this->services['%s'])) {\n", $name);
$getterBody .= sprintf("%sreturn \$this->services['%s'];\n}\n\n", $indent, $name);
}
// Creation and method calls
$getterBody .= sprintf("%s\n", $creation);
$getterBody .= $methods;
// Stored service
$getterBody .= $storage;
// End getter body
$getterBody .= "return \$object;\n";
$getterDef = new CodeGen\PhpMethod();
$getterDef->setName($getter)
->setBody($getterBody);
$getters[] = $getterDef;
// Get cases for case statements
$cases = array($name);
if (isset($aliases[$name])) {
$cases = array_merge($aliases[$name], $cases);
}
// Build case statement and store
$statement = '';
foreach ($cases as $value) {
$statement .= sprintf("%scase '%s':\n", $indent, $value);
}
$statement .= sprintf("%sreturn \$this->%s();\n", str_repeat($indent, 2), $getter);
$caseStatements[] = $statement;
}
// Build switch statement
$switch = sprintf("switch (%s) {\n%s\n", '$name', implode("\n", $caseStatements));
$switch .= sprintf("%sdefault:\n%sreturn parent::get(%s, %s);\n", $indent, str_repeat($indent, 2), '$name', '$params');
$switch .= "}\n\n";
// Build get() method
$nameParam = new CodeGen\PhpParameter();
$nameParam->setName('name');
$defaultParams = new CodeGen\PhpParameterDefaultValue();
$defaultParams->setValue(array());
$paramsParam = new CodeGen\PhpParameter();
$paramsParam->setName('params')
->setType('array')
->setDefaultValue($defaultParams);
$get = new CodeGen\PhpMethod();
$get->setName('get');
$get->setParameters(array(
$nameParam,
$paramsParam,
));
$get->setBody($switch);
// Create getters for aliases
$aliasMethods = array();
foreach ($aliases as $class => $classAliases) {
foreach ($classAliases as $alias) {
$aliasMethods[] = $this->getCodeGenMethodFromAlias($alias, $class);
}
}
// Create class code generation object
$container = new CodeGen\PhpClass();
$container->setName($this->containerClass)
->setExtendedClass('ServiceLocator')
->setMethod($get)
->setMethods($getters)
->setMethods($aliasMethods);
// Create PHP file code generation object
$classFile = new CodeGen\PhpFile();
$classFile->setUse('Zend\Di\ServiceLocator')
->setClass($container);
if (null !== $this->namespace) {
$classFile->setNamespace($this->namespace);
}
if (null !== $filename) {
$classFile->setFilename($filename);
}
return $classFile;
}
/**
* Reduces aliases
*
* Takes alias list and reduces it to a 2-dimensional array of
* class names pointing to an array of aliases that resolve to
* it.
*
* @param array $aliasList
* @return array
*/
protected function reduceAliases(array $aliasList)
{
$reduced = array();
$aliases = array_keys($aliasList);
foreach ($aliasList as $alias => $service) {
if (in_array($service, $aliases)) {
do {
$service = $aliasList[$service];
} while (in_array($service, $aliases));
}
if (!isset($reduced[$service])) {
$reduced[$service] = array();
}
$reduced[$service][] = $alias;
}
return $reduced;
}
/**
* Create a PhpMethod code generation object named after a given alias
*
* @param string $alias
* @param class $class Class to which alias refers
* @return CodeGen\PhpMethod
*/
protected function getCodeGenMethodFromAlias($alias, $class)
{
$alias = $this->normalizeAlias($alias);
$method = new CodeGen\PhpMethod();
$method->setName($alias)
->setBody(sprintf('return $this->get(\'%s\');', $class));
return $method;
}
/**
* Normalize an alias to a getter method name
*
* @param string $alias
* @return string
*/
protected function normalizeAlias($alias)
{
$normalized = preg_replace('/[^a-zA-Z0-9]/', ' ', $alias);
$normalized = 'get' . str_replace(' ', '', ucwords($normalized));
return $normalized;
}
}