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 (#154)
by joseph
26:44
created

Container::updateNotSharedServices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

515
            throw new DoctrineStaticMetaException('Exception getting service ' . $id, $e->/** @scrutinizer ignore-call */ getCode(), $e);
Loading history...
516
        }
517
    }
518
519
    /**
520
     * @param string $id
521
     * @SuppressWarnings(PHPMD.ShortVariable)
522
     *
523
     * @return bool|void
524
     */
525
    public function has($id)
526
    {
527
        return $this->container->has($id);
528
    }
529
}
530