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
33:02
created

Container::getServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
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\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
        EntityUpserterCreator::class,
238
        EntityUnitOfWorkHelperCreator::class,
239
        ExecuteCommand::class,
240
        GenerateCommand::class,
241
        LatestCommand::class,
242
        MigrateCommand::class,
243
        DiffCommand::class,
244
        UpToDateCommand::class,
245
        StatusCommand::class,
246
        VersionCommand::class,
247
    ];
248
249
    public const ALIASES = [
250
        EntityFactoryInterface::class              => EntityFactory::class,
251
        EntityDataValidatorInterface::class        => EntityDataValidator::class,
252
        ConstraintValidatorFactoryInterface::class => ContainerConstraintValidatorFactory::class,
253
    ];
254
255
    public const NOT_SHARED_SERVICES = [
256
        FixturesHelper::class,
257
    ];
258
259
260
    /**
261
     * The directory that container cache files will be stored
262
     */
263
    public const CACHE_PATH = __DIR__ . '/../cache/';
264
265
    public const SYMFONY_CACHE_PATH = self::CACHE_PATH . '/container.symfony.php';
266
267
    /**
268
     * @var bool
269
     */
270
    private $useCache = false;
271
272
    /**
273
     * @var ContainerInterface
274
     */
275
    private $container;
276
277
    /**
278
     * @param bool $useCache
279
     *
280
     * @return Container
281
     */
282
    public function setUseCache(bool $useCache): Container
283
    {
284
        $this->useCache = $useCache;
285
286
        return $this;
287
    }
288
289
    /**
290
     * @param array $server - normally you would pass in $_SERVER
291
     *
292
     * @throws DoctrineStaticMetaException
293
     */
294
    public function buildSymfonyContainer(array $server): void
295
    {
296
        if (true === $this->useCache && file_exists(self::SYMFONY_CACHE_PATH)) {
297
            /** @noinspection PhpIncludeInspection */
298
            require self::SYMFONY_CACHE_PATH;
299
            $this->setContainer(new \ProjectServiceContainer());
300
301
            return;
302
        }
303
304
        try {
305
            $container = new ContainerBuilder();
306
            $this->addConfiguration($container, $server);
307
            $container->compile();
308
            $this->setContainer($container);
309
            $dumper = new PhpDumper($container);
310
            file_put_contents(self::SYMFONY_CACHE_PATH, $dumper->dump());
311
        } catch (ServiceNotFoundException | InvalidArgumentException $e) {
312
            throw new DoctrineStaticMetaException(
313
                'Exception building the container: ' . $e->getMessage(),
314
                $e->getCode(),
315
                $e
316
            );
317
        }
318
    }
319
320
    /**
321
     * Set a container instance
322
     *
323
     * @param ContainerInterface $container
324
     *
325
     * @return $this
326
     */
327
    public function setContainer(ContainerInterface $container): self
328
    {
329
        $this->container = $container;
330
331
        return $this;
332
    }
333
334
    /**
335
     * Build all the definitions, alias and other configuration for this container. Each of these steps need to be
336
     * carried out to allow the everything to work, however you may wish to change individual bits. Therefore this
337
     * method has been made final, but the individual methods can be overwritten if you extend off the class
338
     *
339
     * @param ContainerBuilder $containerBuilder
340
     * @param array            $server
341
     *
342
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
343
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
344
     */
345
    final public function addConfiguration(ContainerBuilder $containerBuilder, array $server): void
346
    {
347
        $this->autoWireServices($containerBuilder);
348
        $this->defineConfig($containerBuilder, $server);
349
        $this->defineCache($containerBuilder, $server);
350
        $this->defineEntityManager($containerBuilder);
351
        $this->configureValidationComponents($containerBuilder);
352
        $this->defineAliases($containerBuilder);
353
    }
354
355
    /**
356
     * This takes every class from the getServices method, auto wires them and marks them as public. You may wish to
357
     * override this if you want to mark certain classes as private
358
     *
359
     * @param ContainerBuilder $containerBuilder
360
     */
361
    public function autoWireServices(ContainerBuilder $containerBuilder): void
362
    {
363
        $services = $this->getServices();
364
        foreach ($services as $class) {
365
            $containerBuilder->autowire($class, $class)->setPublic(true);
366
        }
367
    }
368
369
    /**
370
     * This is a simple wrapper around the class constants. You can use this to add, remove, or replace individual
371
     * services that will be auto wired
372
     *
373
     * @return array
374
     */
375
    public function getServices(): array
376
    {
377
        return self::SERVICES;
378
    }
379
380
    /**
381
     * This is used to auto wire the config interface. It sets the $server param as a constructor argument and then
382
     * sets the concrete class as the implementation for the Interface. Override this if you wish to use different
383
     * logic for where the config comes from
384
     *
385
     * @param ContainerBuilder $containerBuilder
386
     * @param array            $server
387
     */
388
    public function defineConfig(ContainerBuilder $containerBuilder, array $server): void
389
    {
390
        $containerBuilder->getDefinition(Config::class)->setArgument('$server', $this->configVars($server));
391
        $containerBuilder->setAlias(ConfigInterface::class, Config::class);
392
    }
393
394
    /**
395
     * Take the $server array, normally a copy of $_SERVER, and pull out just the bits required by config
396
     *
397
     * @param array $server
398
     *
399
     * @return array
400
     */
401
    protected function configVars(array $server): array
402
    {
403
        $return = array_intersect_key(
404
            $server,
405
            array_flip(ConfigInterface::PARAMS)
406
        );
407
408
        return $return;
409
    }
410
411
    /**
412
     * This is used to auto wire the doctrine cache. If we are in dev mode then this will always use the Array Cache,
413
     * if not then the cache will be set to what is in the $server array. Override this method if you wish to use
414
     * different logic to handle caching
415
     *
416
     * @param ContainerBuilder $containerBuilder
417
     * @param array            $server
418
     */
419
    public function defineCache(ContainerBuilder $containerBuilder, array $server): void
420
    {
421
        $cacheDriver = $server[Config::PARAM_DOCTRINE_CACHE_DRIVER] ?? Config::DEFAULT_DOCTRINE_CACHE_DRIVER;
422
        $containerBuilder->autowire($cacheDriver)->setPublic(true);
423
        $this->configureFilesystemCache($containerBuilder);
424
        /**
425
         * Which Cache Driver is used for the Cache Interface?
426
         *
427
         * If Dev mode, we always use the Array Cache
428
         *
429
         * Otherwise, we use the Configured Cache driver (which defaults to Array Cache)
430
         */
431
        $cache = ($server[Config::PARAM_DEVMODE] ?? false) ? ArrayCache::class : $cacheDriver;
432
        $containerBuilder->setAlias(Cache::class, $cache)->setPublic(true);
433
        $containerBuilder->getDefinition(DoctrineCache::class)->addArgument(new Reference($cache))->setPublic(true);
434
    }
435
436
    private function configureFilesystemCache(ContainerBuilder $containerBuilder): void
437
    {
438
        $config = $this->getConfig($containerBuilder);
439
        $containerBuilder->getDefinition(FilesystemCache::class)
440
                         ->addArgument($config->get(Config::PARAM_FILESYSTEM_CACHE_PATH))
441
                         ->setPublic(true);
442
    }
443
444
    private function getConfig(ContainerBuilder $containerBuilder): Config
445
    {
446
        return $containerBuilder->get(Config::class);
447
    }
448
449
    /**
450
     * This is used to auto wire the entity manager. It first adds the DSM factory as the factory for the class, and
451
     * sets the Entity Manager as the implementation of the interface. Overrider this if you want to use your own
452
     * factory to create and configure the entity manager
453
     *
454
     * @param ContainerBuilder $container
455
     */
456
    public function defineEntityManager(ContainerBuilder $container): void
457
    {
458
        $container->getDefinition(EntityManagerInterface::class)
459
                  ->addArgument(new Reference(Config::class))
460
                  ->setFactory(
461
                      [
462
                          new Reference(EntityManagerFactory::class),
463
                          'getEntityManager',
464
                      ]
465
                  );
466
    }
467
468
    /**
469
     * Ensure we are using the container constraint validator factory so that custom validators with dependencies can
470
     * simply declare them as normal. Note that you will need to define each custom validator as a service in your
471
     * container.
472
     *
473
     * @param ContainerBuilder $containerBuilder
474
     */
475
    public function configureValidationComponents(ContainerBuilder $containerBuilder): void
476
    {
477
        $containerBuilder->getDefinition(EntityDataValidator::class)
478
                         ->setFactory(
479
                             [
480
                                 new Reference(EntityDataValidatorFactory::class),
481
                                 'buildEntityDataValidator',
482
                             ]
483
                         )->setShared(false);
484
    }
485
486
    public function defineAliases(ContainerBuilder $containerBuilder): void
487
    {
488
        foreach (self::ALIASES as $interface => $service) {
489
            $containerBuilder->setAlias($interface, $service)->setPublic(true);
490
        }
491
    }
492
493
    /**
494
     * Some service should not be Singletons (shared) but should always be a new instance
495
     *
496
     * @param ContainerBuilder $containerBuilder
497
     */
498
    public function updateNotSharedServices(ContainerBuilder $containerBuilder): void
499
    {
500
        foreach (self::NOT_SHARED_SERVICES as $service) {
501
            $containerBuilder->getDefinition($service)->setShared(false);
502
        }
503
    }
504
505
    /**
506
     * @param string $id
507
     *
508
     * @return mixed
509
     * @SuppressWarnings(PHPMD.ShortVariable)
510
     * @throws DoctrineStaticMetaException
511
     */
512
    public function get($id)
513
    {
514
        try {
515
            return $this->container->get($id);
516
        } catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
517
            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

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