GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#199)
by joseph
28:43
created

Container::addConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...ection\Dumper\PhpDumper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     * Take the $server array, normally a copy of $_SERVER, and pull out just the bits required by config
400
     *
401
     * @param array $server
402
     *
403
     * @return array
404
     */
405 2
    protected function configVars(array $server): array
406
    {
407 2
        $return = array_intersect_key(
408 2
            $server,
409 2
            array_flip(ConfigInterface::PARAMS)
410
        );
411
412 2
        return $return;
413
    }
414
415
    /**
416
     * This is used to auto wire the doctrine cache. If we are in dev mode then this will always use the Array Cache,
417
     * if not then the cache will be set to what is in the $server array. Override this method if you wish to use
418
     * different logic to handle caching
419
     *
420
     * @param ContainerBuilder $containerBuilder
421
     * @param array            $server
422
     */
423 2
    public function defineCache(ContainerBuilder $containerBuilder, array $server): void
424
    {
425 2
        $cacheDriver = $server[Config::PARAM_DOCTRINE_CACHE_DRIVER] ?? Config::DEFAULT_DOCTRINE_CACHE_DRIVER;
426 2
        $containerBuilder->autowire($cacheDriver)->setPublic(true);
427 2
        $this->configureFilesystemCache($containerBuilder);
428
        /**
429
         * Which Cache Driver is used for the Cache Interface?
430
         *
431
         * If Dev mode, we always use the Array Cache
432
         *
433
         * Otherwise, we use the Configured Cache driver (which defaults to Array Cache)
434
         */
435 2
        $cache = ($server[Config::PARAM_DEVMODE] ?? false) ? ArrayCache::class : $cacheDriver;
436 2
        $containerBuilder->setAlias(Cache::class, $cache)->setPublic(true);
437 2
        $containerBuilder->getDefinition(DoctrineCache::class)->addArgument(new Reference($cache))->setPublic(true);
438 2
    }
439
440 2
    private function configureFilesystemCache(ContainerBuilder $containerBuilder): void
441
    {
442 2
        $config = $this->getConfig($containerBuilder);
443 2
        $containerBuilder->getDefinition(FilesystemCache::class)
444 2
                         ->addArgument($config->get(Config::PARAM_FILESYSTEM_CACHE_PATH))
445 2
                         ->setPublic(true);
446 2
    }
447
448 2
    private function getConfig(ContainerBuilder $containerBuilder): Config
449
    {
450 2
        return $containerBuilder->get(Config::class);
451
    }
452
453
    /**
454
     * This is used to auto wire the entity manager. It first adds the DSM factory as the factory for the class, and
455
     * sets the Entity Manager as the implementation of the interface. Overrider this if you want to use your own
456
     * factory to create and configure the entity manager
457
     *
458
     * @param ContainerBuilder $container
459
     */
460 2
    public function defineEntityManager(ContainerBuilder $container): void
461
    {
462 2
        $container->getDefinition(EntityManagerInterface::class)
463 2
                  ->addArgument(new Reference(Config::class))
464 2
                  ->setFactory(
465
                      [
466 2
                          new Reference(EntityManagerFactory::class),
467 2
                          'getEntityManager',
468
                      ]
469
                  );
470 2
    }
471
472
    /**
473
     * Ensure we are using the container constraint validator factory so that custom validators with dependencies can
474
     * simply declare them as normal. Note that you will need to define each custom validator as a service in your
475
     * container.
476
     *
477
     * @param ContainerBuilder $containerBuilder
478
     */
479 2
    public function configureValidationComponents(ContainerBuilder $containerBuilder): void
480
    {
481 2
        $containerBuilder->getDefinition(EntityDataValidator::class)
482 2
                         ->setFactory(
483
                             [
484 2
                                 new Reference(EntityDataValidatorFactory::class),
485 2
                                 'buildEntityDataValidator',
486
                             ]
487 2
                         )->setShared(false);
488 2
    }
489
490 2
    public function defineAliases(ContainerBuilder $containerBuilder): void
491
    {
492 2
        foreach (self::ALIASES as $interface => $service) {
493 2
            $containerBuilder->setAlias($interface, $service)->setPublic(true);
494
        }
495 2
    }
496
497 2
    private function registerCustomFakerDataFillers(ContainerBuilder $containerBuilder): void
498
    {
499 2
        $config = $this->getConfig($containerBuilder);
500 2
        $path   = $config->get(Config::PARAM_ENTITIES_CUSTOM_DATA_FILLER_PATH);
501 2
        if (!is_dir($path)) {
502 2
            return;
503
        }
504
        /** @var Finder $finder */
505
        $finder        = $containerBuilder->get(Finder::class);
506
        $files         = $finder->files()->name('*FakerDataFiller.php')->in($path);
507
        $baseNameSpace = $config->get(Config::PARAM_PROJECT_ROOT_NAMESPACE);
508
        $mappings      = [];
509
        foreach ($files as $file) {
510
            /** @var \Symfony\Component\Finder\SplFileInfo $file */
511
            $dataFillerClassName = $baseNameSpace . '\\Assets\\Entity\\FakerDataFillers';
512
            $entityClassName     = $baseNameSpace . '\\Entities';
513
            $relativePath        = str_replace('/', '\\', $file->getRelativePath());
514
            if ($relativePath !== '') {
515
                $dataFillerClassName .= '\\' . $relativePath;
516
                $entityClassName     .= '\\' . $relativePath;
517
            }
518
            $fileName                   = $file->getBasename('.php');
519
            $dataFillerClassName        .= '\\' . $fileName;
520
            $entityClassName            .= '\\' . str_replace('FakerDataFiller', '', $fileName);
521
            $mappings[$entityClassName] = $dataFillerClassName;
522
        }
523
524
        $containerBuilder->getDefinition(FakerDataFillerFactory::class)
525
                         ->addMethodCall('setCustomFakerDataFillersFqns', [$mappings]);
526
    }
527
528
    /**
529
     * Some service should not be Singletons (shared) but should always be a new instance
530
     *
531
     * @param ContainerBuilder $containerBuilder
532
     */
533
    public function updateNotSharedServices(ContainerBuilder $containerBuilder): void
534
    {
535
        foreach (self::NOT_SHARED_SERVICES as $service) {
536
            $containerBuilder->getDefinition($service)->setShared(false);
537
        }
538
    }
539
540
    /**
541
     * @param string $id
542
     *
543
     * @return mixed
544
     * @SuppressWarnings(PHPMD.ShortVariable)
545
     * @throws DoctrineStaticMetaException
546
     */
547 2
    public function get($id)
548
    {
549
        try {
550 2
            return $this->container->get($id);
551
        } catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
552
            throw new DoctrineStaticMetaException(
553
                get_class($e) . ' getting service ' . $id . ': ' . $e->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

553
                get_class($e) . ' getting service ' . $id . ': ' . $e->/** @scrutinizer ignore-call */ getMessage(),
Loading history...
554
                $e->getCode(),
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

554
                $e->/** @scrutinizer ignore-call */ 
555
                    getCode(),
Loading history...
555
                $e
556
            );
557
        }
558
    }
559
560
    /**
561
     * @param string $id
562
     * @SuppressWarnings(PHPMD.ShortVariable)
563
     *
564
     * @return bool|void
565
     */
566
    public function has($id)
567
    {
568
        return $this->container->has($id);
569
    }
570
}
571