1 | <?php |
||
39 | class FactoryManagerImpl implements FactoryManager |
||
40 | { |
||
41 | /** |
||
42 | * The name of the resource repository variable. |
||
43 | */ |
||
44 | const REPO_VAR_NAME = 'repo'; |
||
45 | |||
46 | /** |
||
47 | * The name of the discovery variable. |
||
48 | */ |
||
49 | const DISCOVERY_VAR_NAME = 'discovery'; |
||
50 | |||
51 | /** |
||
52 | * @var ProjectContext |
||
53 | */ |
||
54 | private $context; |
||
55 | |||
56 | /** |
||
57 | * @var Config |
||
58 | */ |
||
59 | private $config; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $rootDir; |
||
65 | |||
66 | /** |
||
67 | * @var GeneratorRegistry |
||
68 | */ |
||
69 | private $generatorRegistry; |
||
70 | |||
71 | /** |
||
72 | * @var ClassWriter |
||
73 | */ |
||
74 | private $classWriter; |
||
75 | |||
76 | /** |
||
77 | * @var PackageCollection |
||
78 | */ |
||
79 | private $packages; |
||
80 | |||
81 | /** |
||
82 | * @var ServerCollection |
||
83 | */ |
||
84 | private $servers; |
||
85 | |||
86 | /** |
||
87 | * Creates a new factory generator. |
||
88 | * |
||
89 | * @param ProjectContext $context The project context. |
||
90 | * @param GeneratorRegistry $generatorRegistry The registry providing |
||
91 | * the generators for the |
||
92 | * services returned by the |
||
93 | * factory. |
||
94 | * @param ClassWriter $classWriter The writer that writes |
||
95 | * the class to a file. |
||
96 | * @param PackageCollection|null $packages The loaded packages. |
||
97 | * @param ServerCollection|null $servers The configured servers. |
||
98 | */ |
||
99 | 44 | public function __construct(ProjectContext $context, GeneratorRegistry $generatorRegistry, ClassWriter $classWriter, PackageCollection $packages = null, ServerCollection $servers = null) |
|
109 | |||
110 | /** |
||
111 | * Sets the packages included in the getPackageOrder() method. |
||
112 | * |
||
113 | * @param PackageCollection $packages The loaded packages. |
||
114 | */ |
||
115 | 14 | public function setPackages(PackageCollection $packages) |
|
116 | { |
||
117 | 14 | $this->packages = $packages; |
|
118 | 14 | } |
|
119 | |||
120 | /** |
||
121 | * Sets the servers included in the createUrlGenerator() method. |
||
122 | * |
||
123 | * @param ServerCollection $servers The configured servers. |
||
124 | */ |
||
125 | 14 | public function setServers(ServerCollection $servers) |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 13 | public function createFactory($path = null, $className = null) |
|
134 | { |
||
135 | 13 | Assert::nullOrStringNotEmpty($path, 'The path to the generated factory file must be a non-empty string or null. Got: %s'); |
|
136 | 13 | Assert::nullOrStringNotEmpty($className, 'The class name of the generated factory must be a non-empty string or null. Got: %s'); |
|
137 | |||
138 | 13 | $this->refreshFactoryClass($path, $className); |
|
139 | |||
140 | 13 | $className = $className ?: $this->config->get(Config::FACTORY_IN_CLASS); |
|
141 | 13 | $path = $path ?: $this->config->get(Config::FACTORY_IN_FILE); |
|
142 | |||
143 | 13 | if (null !== $path && !class_exists($className, false)) { |
|
144 | 12 | require_once Path::makeAbsolute($path, $this->rootDir); |
|
145 | 12 | } |
|
146 | |||
147 | 13 | return new $className(); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function isFactoryClassAutoGenerated() |
|
154 | { |
||
155 | 1 | return $this->config->get(Config::FACTORY_AUTO_GENERATE); |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 32 | public function generateFactoryClass($path = null, $className = null) |
|
162 | { |
||
163 | 32 | Assert::nullOrStringNotEmpty($path, 'The path to the generated factory file must be a non-empty string or null. Got: %s'); |
|
164 | 30 | Assert::nullOrStringNotEmpty($className, 'The class name of the generated factory must be a non-empty string or null. Got: %s'); |
|
165 | |||
166 | 28 | $path = Path::makeAbsolute($path ?: $this->config->get(Config::FACTORY_OUT_FILE), $this->rootDir); |
|
167 | 28 | $className = $className ?: $this->config->get(Config::FACTORY_OUT_CLASS); |
|
168 | 28 | $dispatcher = $this->context->getEventDispatcher(); |
|
169 | |||
170 | 28 | $class = new Clazz($className); |
|
171 | 28 | $class->setFilePath($path); |
|
172 | 28 | $class->setDescription( |
|
173 | <<<EOF |
||
174 | Creates Puli's core services. |
||
175 | |||
176 | This class was auto-generated by Puli. |
||
177 | |||
178 | IMPORTANT: Before modifying the code below, set the "factory.auto-generate" |
||
179 | configuration key to false: |
||
180 | |||
181 | $ puli config factory.auto-generate false |
||
182 | |||
183 | Otherwise any modifications will be overwritten! |
||
184 | EOF |
||
185 | 28 | ); |
|
186 | |||
187 | 28 | $this->addCreateRepositoryMethod($class); |
|
188 | 28 | $this->addCreateDiscoveryMethod($class); |
|
189 | 28 | $this->addCreateUrlGeneratorMethod($class); |
|
190 | 28 | $this->addGetPackageOrderMethod($class); |
|
191 | |||
192 | 28 | if ($dispatcher->hasListeners(PuliEvents::GENERATE_FACTORY)) { |
|
193 | 1 | $dispatcher->dispatch(PuliEvents::GENERATE_FACTORY, new GenerateFactoryEvent($class)); |
|
194 | 1 | } |
|
195 | |||
196 | 28 | $this->classWriter->writeClass($class); |
|
197 | 28 | } |
|
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | 3 | public function autoGenerateFactoryClass($path = null, $className = null) |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | 24 | public function refreshFactoryClass($path = null, $className = null) |
|
259 | |||
260 | /** |
||
261 | * Adds the createRepository() method. |
||
262 | * |
||
263 | * @param Clazz $class The factory class model. |
||
264 | */ |
||
265 | 28 | private function addCreateRepositoryMethod(Clazz $class) |
|
297 | |||
298 | /** |
||
299 | * Adds the createDiscovery() method. |
||
300 | * |
||
301 | * @param Clazz $class The factory class model. |
||
302 | */ |
||
303 | 28 | private function addCreateDiscoveryMethod(Clazz $class) |
|
344 | |||
345 | /** |
||
346 | * Adds the createUrlGenerator() method. |
||
347 | * |
||
348 | * @param Clazz $class The factory class model. |
||
349 | */ |
||
350 | 28 | public function addCreateUrlGeneratorMethod(Clazz $class) |
|
396 | |||
397 | /** |
||
398 | * Adds the getPackageOrder() method. |
||
399 | * |
||
400 | * @param Clazz $class The factory class model. |
||
401 | */ |
||
402 | 28 | public function addGetPackageOrderMethod(Clazz $class) |
|
434 | |||
435 | /** |
||
436 | * Recursively camelizes the keys of an array. |
||
437 | * |
||
438 | * @param array $array The array to process. |
||
439 | * |
||
440 | * @return array The input array with camelized keys. |
||
441 | */ |
||
442 | 28 | private function camelizeKeys(array $array) |
|
454 | |||
455 | /** |
||
456 | * Camelizes a string. |
||
457 | * |
||
458 | * @param string $string A string. |
||
459 | * |
||
460 | * @return string The camelized string. |
||
461 | */ |
||
462 | private function camelize($string) |
||
468 | } |
||
469 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.