1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta; |
4
|
|
|
|
5
|
|
|
// phpcs:disable |
6
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
9
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand; |
10
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand; |
11
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand; |
12
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\LatestCommand; |
13
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand; |
14
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand; |
15
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\UpToDateCommand; |
16
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand; |
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
18
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
19
|
|
|
use Doctrine\ORM\Tools\SchemaValidator; |
20
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Builder\Builder; |
21
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateConstraintAction; |
22
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateDtosForAllEntitiesAction; |
23
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateEmbeddableAction; |
24
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateEntityAction; |
25
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
26
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\CliConfigCommandFactory; |
27
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\CreateConstraintCommand; |
28
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\FinaliseBuildCommand; |
29
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateEmbeddableFromArchetypeCommand; |
30
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateEmbeddableSkeletonCommand; |
31
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateEntityCommand; |
32
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateFieldCommand; |
33
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateRelationsCommand; |
34
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\OverrideCreateCommand; |
35
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\OverridesUpdateCommand; |
36
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\RemoveUnusedRelationsCommand; |
37
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\SetEmbeddableCommand; |
38
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\SetFieldCommand; |
39
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\SetRelationCommand; |
40
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entities\EntityCreator; |
41
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\DataTransferObjects\DtoCreator; |
42
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\FakerData\EmbeddableFakerDataCreator; |
43
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Interfaces\HasEmbeddableInterfaceCreator; |
44
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Interfaces\Objects\EmbeddableInterfaceCreator; |
45
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Objects\EmbeddableCreator; |
46
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Traits\HasEmbeddableTraitCreator; |
47
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Factories\AbstractEntityFactoryCreator; |
48
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Factories\EntityDtoFactoryCreator; |
49
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Factories\EntityFactoryCreator; |
50
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Interfaces\EntityInterfaceCreator; |
51
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Repositories\AbstractEntityRepositoryCreator; |
52
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Repositories\EntityRepositoryCreator; |
53
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Savers\EntitySaverCreator; |
54
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Savers\EntityUnitOfWorkHelperCreator; |
55
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Savers\EntityUpserterCreator; |
56
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintCreator; |
57
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintValidatorCreator; |
58
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintCreator; |
59
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintValidatorCreator; |
60
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\Assets\Entity\Fixtures\EntityFixtureCreator; |
61
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\BootstrapCreator; |
62
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\Entities\AbstractEntityTestCreator; |
63
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\Entities\EntityTestCreator; |
64
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
65
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
66
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer; |
67
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable\ArchetypeEmbeddableGenerator; |
68
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable\EntityEmbeddableSetter; |
69
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\EntityGenerator; |
70
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\AbstractTestFakerDataProviderUpdater; |
71
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\EntityFieldSetter; |
72
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\FieldGenerator; |
73
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait; |
74
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\StandardLibraryTestGenerator; |
75
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FileCreationTransaction; |
76
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FindAndReplaceHelper; |
77
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator; |
78
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
79
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper; |
80
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\CopyPhpstormMeta; |
81
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\EntityFormatter; |
82
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider; |
83
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\ReflectionHelper; |
84
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper; |
85
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\UnusedRelationsRemover; |
86
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\DtoFactory; |
87
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityDependencyInjector; |
88
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactory; |
89
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface; |
90
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories\UuidFactory; |
91
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityDataValidatorInterface; |
92
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories\RepositoryFactory; |
93
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntitySaver; |
94
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntityUpdater; |
95
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkSimpleEntityCreator; |
96
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver; |
97
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory; |
98
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\FakerDataFillerFactory; |
99
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory; |
100
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelperFactory; |
101
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityDataValidator; |
102
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityDataValidatorFactory; |
103
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\Initialiser; |
104
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\EntityManagerFactory; |
105
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
106
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
107
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\MysqliConnectionFactory; |
108
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema; |
109
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\UuidFunctionPolyfill; |
110
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator; |
111
|
|
|
use Psr\Container\ContainerExceptionInterface; |
112
|
|
|
use Psr\Container\ContainerInterface; |
113
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
114
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
115
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
116
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
117
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
118
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
119
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
120
|
|
|
use Symfony\Component\Finder\Finder; |
121
|
|
|
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; |
122
|
|
|
use Symfony\Component\Validator\ContainerConstraintValidatorFactory; |
123
|
|
|
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache; |
124
|
|
|
|
125
|
|
|
// phpcs:enable |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Class Container |
129
|
|
|
* |
130
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta |
131
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
132
|
|
|
*/ |
133
|
|
|
class Container implements ContainerInterface |
134
|
|
|
{ |
135
|
|
|
/** |
136
|
|
|
* This is the list of services managed by this container |
137
|
|
|
* |
138
|
|
|
* This list is used to also generate a PHPStorm meta data file which assists with dynamic type hinting when using |
139
|
|
|
* the container as a service locator |
140
|
|
|
* |
141
|
|
|
* @see ./../../.phpstorm.meta.php/container.meta.php |
142
|
|
|
*/ |
143
|
|
|
public const SERVICES = [ |
144
|
|
|
\Ramsey\Uuid\UuidFactory::class, |
145
|
|
|
AbstractEntityFactoryCreator::class, |
146
|
|
|
AbstractEntityRepositoryCreator::class, |
147
|
|
|
AbstractEntityTestCreator::class, |
148
|
|
|
AbstractTestFakerDataProviderUpdater::class, |
149
|
|
|
ArchetypeEmbeddableGenerator::class, |
150
|
|
|
ArrayCache::class, |
151
|
|
|
BootstrapCreator::class, |
152
|
|
|
Builder::class, |
153
|
|
|
BulkEntitySaver::class, |
154
|
|
|
BulkEntityUpdater::class, |
155
|
|
|
BulkSimpleEntityCreator::class, |
156
|
|
|
CliConfigCommandFactory::class, |
157
|
|
|
CodeHelper::class, |
158
|
|
|
Config::class, |
159
|
|
|
ContainerConstraintValidatorFactory::class, |
160
|
|
|
CopyPhpstormMeta::class, |
161
|
|
|
CreateConstraintAction::class, |
162
|
|
|
CreateConstraintCommand::class, |
163
|
|
|
CreateDtosForAllEntitiesAction::class, |
164
|
|
|
CreateEmbeddableAction::class, |
165
|
|
|
CreateEntityAction::class, |
166
|
|
|
Database::class, |
167
|
|
|
DiffCommand::class, |
168
|
|
|
DoctrineCache::class, |
169
|
|
|
DtoCreator::class, |
170
|
|
|
DtoFactory::class, |
171
|
|
|
EmbeddableCreator::class, |
172
|
|
|
EmbeddableFakerDataCreator::class, |
173
|
|
|
EmbeddableInterfaceCreator::class, |
174
|
|
|
EntityCreator::class, |
175
|
|
|
EntityDataValidator::class, |
176
|
|
|
EntityDataValidatorFactory::class, |
177
|
|
|
EntityDependencyInjector::class, |
178
|
|
|
EntityDtoFactoryCreator::class, |
179
|
|
|
EntityEmbeddableSetter::class, |
180
|
|
|
EntityFactory::class, |
181
|
|
|
EntityFactoryCreator::class, |
182
|
|
|
EntityFieldSetter::class, |
183
|
|
|
EntityFixtureCreator::class, |
184
|
|
|
EntityFormatter::class, |
185
|
|
|
EntityGenerator::class, |
186
|
|
|
EntityInterfaceCreator::class, |
187
|
|
|
EntityIsValidConstraintCreator::class, |
188
|
|
|
EntityIsValidConstraintValidatorCreator::class, |
189
|
|
|
EntityManagerFactory::class, |
190
|
|
|
EntityManagerInterface::class, |
191
|
|
|
EntityRepositoryCreator::class, |
192
|
|
|
EntitySaver::class, |
193
|
|
|
EntitySaverCreator::class, |
194
|
|
|
EntitySaverFactory::class, |
195
|
|
|
EntityTestCreator::class, |
196
|
|
|
EntityUnitOfWorkHelperCreator::class, |
197
|
|
|
EntityUpserterCreator::class, |
198
|
|
|
ExecuteCommand::class, |
199
|
|
|
FakerDataFillerFactory::class, |
200
|
|
|
FieldGenerator::class, |
201
|
|
|
FileCreationTransaction::class, |
202
|
|
|
FileFactory::class, |
203
|
|
|
FileOverrider::class, |
204
|
|
|
Filesystem::class, |
205
|
|
|
FilesystemCache::class, |
206
|
|
|
FinaliseBuildCommand::class, |
207
|
|
|
FindAndReplaceHelper::class, |
208
|
|
|
FindReplaceFactory::class, |
209
|
|
|
Finder::class, |
210
|
|
|
FixturesHelperFactory::class, |
211
|
|
|
GenerateCommand::class, |
212
|
|
|
GenerateEmbeddableFromArchetypeCommand::class, |
213
|
|
|
GenerateEmbeddableSkeletonCommand::class, |
214
|
|
|
GenerateEntityCommand::class, |
215
|
|
|
GenerateFieldCommand::class, |
216
|
|
|
GenerateRelationsCommand::class, |
217
|
|
|
HasEmbeddableInterfaceCreator::class, |
218
|
|
|
HasEmbeddableTraitCreator::class, |
219
|
|
|
IdTrait::class, |
220
|
|
|
LatestCommand::class, |
221
|
|
|
MigrateCommand::class, |
222
|
|
|
MysqliConnectionFactory::class, |
223
|
|
|
NamespaceHelper::class, |
224
|
|
|
OverrideCreateCommand::class, |
225
|
|
|
OverridesUpdateCommand::class, |
226
|
|
|
PathHelper::class, |
227
|
|
|
PropertyConstraintCreator::class, |
228
|
|
|
PropertyConstraintValidatorCreator::class, |
229
|
|
|
ReflectionHelper::class, |
230
|
|
|
RelationsGenerator::class, |
231
|
|
|
RemoveUnusedRelationsCommand::class, |
232
|
|
|
RepositoryFactory::class, |
233
|
|
|
Schema::class, |
234
|
|
|
SchemaTool::class, |
235
|
|
|
SchemaValidator::class, |
236
|
|
|
SetEmbeddableCommand::class, |
237
|
|
|
SetFieldCommand::class, |
238
|
|
|
SetRelationCommand::class, |
239
|
|
|
StandardLibraryTestGenerator::class, |
240
|
|
|
StatusCommand::class, |
241
|
|
|
TestCodeGenerator::class, |
242
|
|
|
TestEntityGeneratorFactory::class, |
243
|
|
|
TypeHelper::class, |
244
|
|
|
UnusedRelationsRemover::class, |
245
|
|
|
UpToDateCommand::class, |
246
|
|
|
UuidFactory::class, |
247
|
|
|
UuidFunctionPolyfill::class, |
248
|
|
|
VersionCommand::class, |
249
|
|
|
Writer::class, |
250
|
|
|
Initialiser::class, |
251
|
|
|
]; |
252
|
|
|
|
253
|
|
|
public const ALIASES = [ |
254
|
|
|
EntityFactoryInterface::class => EntityFactory::class, |
255
|
|
|
EntityDataValidatorInterface::class => EntityDataValidator::class, |
256
|
|
|
ConstraintValidatorFactoryInterface::class => ContainerConstraintValidatorFactory::class, |
257
|
|
|
]; |
258
|
|
|
|
259
|
|
|
public const NOT_SHARED_SERVICES = [ |
260
|
|
|
]; |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* The directory that container cache files will be stored |
265
|
|
|
*/ |
266
|
|
|
public const CACHE_PATH = __DIR__ . '/../cache/'; |
267
|
|
|
|
268
|
|
|
public const SYMFONY_CACHE_PATH = self::CACHE_PATH . '/container.symfony.php'; |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @var bool |
272
|
|
|
*/ |
273
|
|
|
private $useCache = false; |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @var ContainerInterface |
277
|
|
|
*/ |
278
|
|
|
private $container; |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param bool $useCache |
282
|
|
|
* |
283
|
|
|
* @return Container |
284
|
|
|
*/ |
285
|
|
|
public function setUseCache(bool $useCache): Container |
286
|
|
|
{ |
287
|
|
|
$this->useCache = $useCache; |
288
|
|
|
|
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param array $server - normally you would pass in $_SERVER |
294
|
|
|
* |
295
|
|
|
* @throws DoctrineStaticMetaException |
296
|
|
|
*/ |
297
|
2 |
|
public function buildSymfonyContainer(array $server): void |
298
|
|
|
{ |
299
|
2 |
|
if (true === $this->useCache && file_exists(self::SYMFONY_CACHE_PATH)) { |
300
|
|
|
/** @noinspection PhpIncludeInspection */ |
301
|
|
|
require self::SYMFONY_CACHE_PATH; |
302
|
|
|
$this->setContainer(new \ProjectServiceContainer()); |
303
|
|
|
|
304
|
|
|
return; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
try { |
308
|
2 |
|
$container = new ContainerBuilder(); |
309
|
2 |
|
$this->addConfiguration($container, $server); |
310
|
2 |
|
$container->compile(); |
311
|
2 |
|
$this->setContainer($container); |
312
|
2 |
|
$dumper = new PhpDumper($container); |
313
|
2 |
|
file_put_contents(self::SYMFONY_CACHE_PATH, $dumper->dump()); |
314
|
|
|
} catch (ServiceNotFoundException | InvalidArgumentException $e) { |
315
|
|
|
throw new DoctrineStaticMetaException( |
316
|
|
|
'Exception building the container: ' . $e->getMessage(), |
317
|
|
|
$e->getCode(), |
318
|
|
|
$e |
319
|
|
|
); |
320
|
|
|
} |
321
|
2 |
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Set a container instance |
325
|
|
|
* |
326
|
|
|
* @param ContainerInterface $container |
327
|
|
|
* |
328
|
|
|
* @return $this |
329
|
|
|
*/ |
330
|
2 |
|
public function setContainer(ContainerInterface $container): self |
331
|
|
|
{ |
332
|
2 |
|
$this->container = $container; |
333
|
|
|
|
334
|
2 |
|
return $this; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Build all the definitions, alias and other configuration for this container. Each of these steps need to be |
339
|
|
|
* carried out to allow the everything to work, however you may wish to change individual bits. Therefore this |
340
|
|
|
* method has been made final, but the individual methods can be overwritten if you extend off the class |
341
|
|
|
* |
342
|
|
|
* @param ContainerBuilder $containerBuilder |
343
|
|
|
* @param array $server |
344
|
|
|
* |
345
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
346
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException |
347
|
|
|
*/ |
348
|
2 |
|
final public function addConfiguration(ContainerBuilder $containerBuilder, array $server): void |
349
|
|
|
{ |
350
|
2 |
|
$this->autoWireServices($containerBuilder); |
351
|
2 |
|
$this->defineConfig($containerBuilder, $server); |
352
|
2 |
|
$this->defineCache($containerBuilder, $server); |
353
|
2 |
|
$this->defineEntityManager($containerBuilder); |
354
|
2 |
|
$this->configureValidationComponents($containerBuilder); |
355
|
2 |
|
$this->defineAliases($containerBuilder); |
356
|
2 |
|
$this->registerCustomFakerDataFillers($containerBuilder); |
357
|
2 |
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* This takes every class from the getServices method, auto wires them and marks them as public. You may wish to |
361
|
|
|
* override this if you want to mark certain classes as private |
362
|
|
|
* |
363
|
|
|
* @param ContainerBuilder $containerBuilder |
364
|
|
|
*/ |
365
|
2 |
|
public function autoWireServices(ContainerBuilder $containerBuilder): void |
366
|
|
|
{ |
367
|
2 |
|
$services = $this->getServices(); |
368
|
2 |
|
foreach ($services as $class) { |
369
|
2 |
|
$containerBuilder->autowire($class, $class)->setPublic(true); |
370
|
|
|
} |
371
|
2 |
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* This is a simple wrapper around the class constants. You can use this to add, remove, or replace individual |
375
|
|
|
* services that will be auto wired |
376
|
|
|
* |
377
|
|
|
* @return array |
378
|
|
|
*/ |
379
|
2 |
|
public function getServices(): array |
380
|
|
|
{ |
381
|
2 |
|
return self::SERVICES; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* This is used to auto wire the config interface. It sets the $server param as a constructor argument and then |
386
|
|
|
* sets the concrete class as the implementation for the Interface. Override this if you wish to use different |
387
|
|
|
* logic for where the config comes from |
388
|
|
|
* |
389
|
|
|
* @param ContainerBuilder $containerBuilder |
390
|
|
|
* @param array $server |
391
|
|
|
*/ |
392
|
2 |
|
public function defineConfig(ContainerBuilder $containerBuilder, array $server): void |
393
|
|
|
{ |
394
|
2 |
|
$containerBuilder->getDefinition(Config::class)->setArgument('$server', $this->configVars($server)); |
395
|
2 |
|
$containerBuilder->setAlias(ConfigInterface::class, Config::class); |
396
|
2 |
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* This is used to auto wire the doctrine cache. If we are in dev mode then this will always use the Array Cache, |
400
|
|
|
* if not then the cache will be set to what is in the $server array. Override this method if you wish to use |
401
|
|
|
* different logic to handle caching |
402
|
|
|
* |
403
|
|
|
* @param ContainerBuilder $containerBuilder |
404
|
|
|
* @param array $server |
405
|
|
|
*/ |
406
|
2 |
|
public function defineCache(ContainerBuilder $containerBuilder, array $server): void |
407
|
|
|
{ |
408
|
2 |
|
$cacheDriver = $server[Config::PARAM_DOCTRINE_CACHE_DRIVER] ?? Config::DEFAULT_DOCTRINE_CACHE_DRIVER; |
409
|
2 |
|
$containerBuilder->autowire($cacheDriver)->setPublic(true); |
410
|
2 |
|
$this->configureFilesystemCache($containerBuilder); |
411
|
|
|
/** |
412
|
|
|
* Which Cache Driver is used for the Cache Interface? |
413
|
|
|
* |
414
|
|
|
* If Dev mode, we always use the Array Cache |
415
|
|
|
* |
416
|
|
|
* Otherwise, we use the Configured Cache driver (which defaults to Array Cache) |
417
|
|
|
*/ |
418
|
2 |
|
$cache = ($server[Config::PARAM_DEVMODE] ?? false) ? ArrayCache::class : $cacheDriver; |
419
|
2 |
|
$containerBuilder->setAlias(Cache::class, $cache)->setPublic(true); |
420
|
2 |
|
$containerBuilder->getDefinition(DoctrineCache::class)->addArgument(new Reference($cache))->setPublic(true); |
421
|
2 |
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* This is used to auto wire the entity manager. It first adds the DSM factory as the factory for the class, and |
425
|
|
|
* sets the Entity Manager as the implementation of the interface. Overrider this if you want to use your own |
426
|
|
|
* factory to create and configure the entity manager |
427
|
|
|
* |
428
|
|
|
* @param ContainerBuilder $container |
429
|
|
|
*/ |
430
|
2 |
|
public function defineEntityManager(ContainerBuilder $container): void |
431
|
|
|
{ |
432
|
2 |
|
$container->getDefinition(EntityManagerInterface::class) |
433
|
2 |
|
->addArgument(new Reference(Config::class)) |
434
|
2 |
|
->setFactory( |
435
|
|
|
[ |
436
|
2 |
|
new Reference(EntityManagerFactory::class), |
437
|
2 |
|
'getEntityManager', |
438
|
|
|
] |
439
|
|
|
); |
440
|
2 |
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Ensure we are using the container constraint validator factory so that custom validators with dependencies can |
444
|
|
|
* simply declare them as normal. Note that you will need to define each custom validator as a service in your |
445
|
|
|
* container. |
446
|
|
|
* |
447
|
|
|
* @param ContainerBuilder $containerBuilder |
448
|
|
|
*/ |
449
|
2 |
|
public function configureValidationComponents(ContainerBuilder $containerBuilder): void |
450
|
|
|
{ |
451
|
2 |
|
$containerBuilder->getDefinition(EntityDataValidator::class) |
452
|
2 |
|
->setFactory( |
453
|
|
|
[ |
454
|
2 |
|
new Reference(EntityDataValidatorFactory::class), |
455
|
2 |
|
'buildEntityDataValidator', |
456
|
|
|
] |
457
|
2 |
|
)->setShared(false); |
458
|
2 |
|
} |
459
|
|
|
|
460
|
2 |
|
public function defineAliases(ContainerBuilder $containerBuilder): void |
461
|
|
|
{ |
462
|
2 |
|
foreach (self::ALIASES as $interface => $service) { |
463
|
2 |
|
$containerBuilder->setAlias($interface, $service)->setPublic(true); |
464
|
|
|
} |
465
|
2 |
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Some service should not be Singletons (shared) but should always be a new instance |
469
|
|
|
* |
470
|
|
|
* @param ContainerBuilder $containerBuilder |
471
|
|
|
*/ |
472
|
|
|
public function updateNotSharedServices(ContainerBuilder $containerBuilder): void |
473
|
|
|
{ |
474
|
|
|
foreach (self::NOT_SHARED_SERVICES as $service) { |
475
|
|
|
$containerBuilder->getDefinition($service)->setShared(false); |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
479
|
2 |
|
private function registerCustomFakerDataFillers(ContainerBuilder $containerBuilder): void |
480
|
|
|
{ |
481
|
2 |
|
$config = $this->getConfig($containerBuilder); |
482
|
2 |
|
$path = $config->get(Config::PARAM_ENTITIES_CUSTOM_DATA_FILLER_PATH); |
483
|
2 |
|
if (!is_dir($path)) { |
484
|
2 |
|
return; |
485
|
|
|
} |
486
|
|
|
/** @var Finder $finder */ |
487
|
|
|
$finder = $containerBuilder->get(Finder::class); |
488
|
|
|
$files = $finder->files()->name('*FakerDataFiller.php')->in($path); |
489
|
|
|
$baseNameSpace = $config->get(Config::PARAM_PROJECT_ROOT_NAMESPACE); |
490
|
|
|
$mappings = []; |
491
|
|
|
foreach ($files as $file) { |
492
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */ |
493
|
|
|
$dataFillerClassName = $baseNameSpace . '\\Assets\\Entity\\FakerDataFillers'; |
494
|
|
|
$entityClassName = $baseNameSpace . '\\Entities'; |
495
|
|
|
$relativePath = str_replace('/', '\\', $file->getRelativePath()); |
496
|
|
|
if ($relativePath !== '') { |
497
|
|
|
$dataFillerClassName .= '\\' . $relativePath; |
498
|
|
|
$entityClassName .= '\\' . $relativePath; |
499
|
|
|
} |
500
|
|
|
$fileName = $file->getBasename('.php'); |
501
|
|
|
$dataFillerClassName .= '\\' . $fileName; |
502
|
|
|
$entityClassName .= '\\' . str_replace('FakerDataFiller', '', $fileName); |
503
|
|
|
$mappings[$entityClassName] = $dataFillerClassName; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$containerBuilder->getDefinition(FakerDataFillerFactory::class) |
507
|
|
|
->addMethodCall('setCustomFakerDataFillersFqns', [$mappings]); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* @param string $id |
512
|
|
|
* |
513
|
|
|
* @return mixed |
514
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
515
|
|
|
* @throws DoctrineStaticMetaException |
516
|
|
|
*/ |
517
|
2 |
|
public function get($id) |
518
|
|
|
{ |
519
|
|
|
try { |
520
|
2 |
|
return $this->container->get($id); |
521
|
|
|
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) { |
522
|
|
|
throw new DoctrineStaticMetaException('Exception getting service ' . $id, $e->getCode(), $e); |
|
|
|
|
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* @param string $id |
528
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
529
|
|
|
* |
530
|
|
|
* @return bool|void |
531
|
|
|
*/ |
532
|
|
|
public function has($id) |
533
|
|
|
{ |
534
|
|
|
return $this->container->has($id); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Take the $server array, normally a copy of $_SERVER, and pull out just the bits required by config |
539
|
|
|
* |
540
|
|
|
* @param array $server |
541
|
|
|
* |
542
|
|
|
* @return array |
543
|
|
|
*/ |
544
|
2 |
|
protected function configVars(array $server): array |
545
|
|
|
{ |
546
|
2 |
|
$return = array_intersect_key( |
547
|
2 |
|
$server, |
548
|
2 |
|
array_flip(ConfigInterface::PARAMS) |
549
|
|
|
); |
550
|
|
|
|
551
|
2 |
|
return $return; |
552
|
|
|
} |
553
|
|
|
|
554
|
2 |
|
private function configureFilesystemCache(ContainerBuilder $containerBuilder): void |
555
|
|
|
{ |
556
|
2 |
|
$config = $this->getConfig($containerBuilder); |
557
|
2 |
|
$containerBuilder->getDefinition(FilesystemCache::class) |
558
|
2 |
|
->addArgument($config->get(Config::PARAM_FILESYSTEM_CACHE_PATH)) |
559
|
2 |
|
->setPublic(true); |
560
|
2 |
|
} |
561
|
|
|
|
562
|
2 |
|
private function getConfig(ContainerBuilder $containerBuilder): Config |
563
|
|
|
{ |
564
|
2 |
|
return $containerBuilder->get(Config::class); |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
|