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:39
created

Container::getServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

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

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