Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.87% covered (warning)
83.87%
52 / 62
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApieExtension
83.87% covered (warning)
83.87%
52 / 62
0.00% covered (danger)
0.00%
0 / 1
17.07
0.00% covered (danger)
0.00%
0 / 1
 load
83.87% covered (warning)
83.87%
52 / 62
0.00% covered (danger)
0.00%
0 / 1
17.07
1<?php
2namespace Apie\ApieBundle\DependencyInjection;
3
4use Apie\ApieBundle\Interfaces\ApieContextService;
5use Apie\CmsApiDropdownOption\DropdownOptionProvider\DropdownOptionProviderInterface;
6use Apie\Common\DependencyInjection\ApieConfigFileLocator;
7use Apie\Common\Interfaces\RouteDefinitionProviderInterface;
8use Apie\Core\ContextBuilders\ContextBuilderInterface;
9use Apie\Core\Datalayers\ApieDatalayer;
10use Apie\Core\FileStorage\InlineStorage;
11use Apie\DoctrineEntityDatalayer\Commands\ApieUpdateIdfCommand;
12use Apie\DoctrineEntityDatalayer\EntityReindexer;
13use Apie\DoctrineEntityDatalayer\IndexStrategy\BackgroundIndexStrategy;
14use Apie\DoctrineEntityDatalayer\IndexStrategy\DirectIndexStrategy;
15use Apie\DoctrineEntityDatalayer\IndexStrategy\IndexAfterResponseIsSentStrategy;
16use Apie\DoctrineEntityDatalayer\IndexStrategy\IndexStrategyInterface;
17use Apie\DoctrineEntityDatalayer\OrmBuilder;
18use Apie\Faker\Interfaces\ApieClassFaker;
19use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface;
20use Symfony\Component\DependencyInjection\ContainerBuilder;
21use Symfony\Component\DependencyInjection\Definition;
22use Symfony\Component\DependencyInjection\Extension\Extension;
23use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
24use Symfony\Component\DependencyInjection\Reference;
25
26/**
27 * Loads all services into symfony. Which services depend on the apie configuration set up.
28 */
29final class ApieExtension extends Extension
30{
31    /**
32     * @var array<string, array<int, string>>
33     */
34    private array $dependencies = [
35        'enable_common_plugin' => [
36            'apie_common_plugin.yaml',
37        ],
38        'enable_cms' => [
39            'common.yaml',
40            'cms.yaml',
41            'html_builders.yaml',
42            'serializer.yaml',
43            'sf_cms.yaml',
44        ],
45        'enable_cms_dropdown' => [
46             'common.yaml',
47             'cms_dropdown.yaml',
48        ],
49        'enable_core' => [
50            'core.yaml',
51            'sf_core.yaml',
52        ],
53        'enable_doctrine_entity_converter' => [
54            'core.yaml',
55            'doctrine_entity_converter.yaml',
56        ],
57        'enable_doctrine_entity_datalayer' => [
58            'core.yaml',
59            'doctrine_entity_converter.yaml',
60            'doctrine_entity_datalayer.yaml',
61        ],
62        'enable_doctrine_bundle_connection' => [
63            'core.yaml',
64            'doctrine_entity_converter.yaml',
65            'doctrine_entity_datalayer.yaml',
66            'sf_doctrine.yaml',
67        ],
68        'enable_console' => [
69            'common.yaml',
70            'console.yaml',
71            'serializer.yaml',
72        ],
73        'enable_maker' => [
74            'maker.yaml',
75            "sf_maker.yaml",
76        ],
77        'enable_profiler' => [
78            'sf_profiler.yaml',
79        ],
80        'enable_security' => [
81            'common.yaml',
82            'serializer.yaml',
83            'security.yaml',
84        ],
85        'enable_rest_api' => [
86            'common.yaml',
87            'rest_api.yaml',
88            'schema_generator.yaml',
89            'serializer.yaml',
90        ],
91        'enable_faker' => [
92            'faker.yaml',
93        ],
94        'enable_twig_template_layout_renderer' => [
95            'twig_template_layout_renderer.yaml',
96        ],
97    ];
98
99    /**
100     * @param array<string, mixed> $configs
101     */
102    public function load(array $configs, ContainerBuilder $container): void
103    {
104        $loader = new YamlFileLoader($container, new ApieConfigFileLocator(__DIR__.'/../../resources/config'));
105        $loader->load('services.yaml');
106        $loader->load('psr7.yaml');
107        $configuration = $this->getConfiguration($configs, $container);
108        $config = $this->processConfiguration($configuration, $configs);
109        $container->setParameter('apie.encryption_key', $config['encryption_key'] ?? null);
110        $container->setParameter('apie.bounded_contexts', $config['bounded_contexts']);
111        $container->setParameter('apie.scan_bounded_contexts', $config['scan_bounded_contexts'] ?? []);
112        $container->setParameter('apie.storage', $config['storage'] ? : [['class' => InlineStorage::class]]);
113        $container->setParameter('apie.datalayers', $config['datalayers'] ?? []);
114        $container->setParameter('apie.cms.asset_folders', $config['cms']['asset_folders'] ?? []);
115        $container->setParameter('apie.cms.dashboard_template', $config['cms']['dashboard_template'] ?? '@Apie/dashboard.html.twig');
116        $container->setParameter('apie.cms.error_template', $config['cms']['error_template'] ?? '@Apie/error.html.twig');
117        $container->setParameter('apie.cms.base_url', rtrim($config['cms']['base_url'] ?? '/cms', '/'));
118        $container->setParameter('apie.doctrine.build_once', $config['doctrine']['build_once'] ?? false);
119        $container->setParameter('apie.doctrine.connection_params', $config['doctrine']['connection_params'] ?? []);
120        $container->setParameter('apie.doctrine.run_migrations', $config['doctrine']['run_migrations'] ?? false);
121        $container->setParameter('apie.rest_api.base_url', rtrim($config['rest_api']['base_url'] ?? '/api', '/'));
122        if (($config['enable_maker'] ?? false) && is_array($config['maker'] ?? null)) {
123            $container->setParameter('apie.maker', $config['maker']);
124        } else {
125            $container->setParameter('apie.maker', null);
126        }
127        if ($config['enable_doctrine_entity_datalayer']) {
128            $type = $config['doctrine']['indexing']['type'] ?? 'direct';
129            $container->addAliases([
130                IndexStrategyInterface::class => match($type) {
131                    'direct' => DirectIndexStrategy::class,
132                    'late' => IndexAfterResponseIsSentStrategy::class,
133                    'background' => BackgroundIndexStrategy::class,
134                    default => $config['doctrine']['indexing']['service'] ?? DirectIndexStrategy::class,
135                },
136            ]);
137            if ($type === 'background') {
138                $definition = new Definition(ApieUpdateIdfCommand::class);
139                $definition->setArguments([
140                    new Reference(EntityReindexer::class),
141                    new Reference(OrmBuilder::class)
142                ]);
143                $definition->addTag('console.command');
144                $container->addDefinitions([ApieUpdateIdfCommand::class => $definition]);
145            }
146        }
147        $loaded = [];
148        foreach ($this->dependencies as $configName => $dependencyList) {
149            if ($config[$configName]) {
150                foreach ($dependencyList as $dependency) {
151                    if (!isset($loaded[$dependency])) {
152                        $loaded[$dependency] = true;
153                        $loader->load($dependency);
154                    }
155                }
156            }
157        }
158
159        $container->registerForAutoconfiguration(ContextBuilderInterface::class)
160            ->addTag('apie.core.context_builder');
161        $container->registerForAutoconfiguration(ApieContextService::class)
162            ->addTag('apie.context');
163        $container->registerForAutoconfiguration(RouteDefinitionProviderInterface::class)
164            ->addTag('apie.common.route_definition');
165        $container->registerForAutoconfiguration(ApieDatalayer::class)
166            ->addTag('apie.datalayer');
167        $container->registerForAutoconfiguration(ApieClassFaker::class)
168            ->addTag('apie.faker');
169        if ($config['enable_cms']) {
170            $container->registerForAutoconfiguration(FormComponentProviderInterface::class)
171                ->addTag(FormComponentProviderInterface::class);
172        }
173        if ($config['enable_cms_dropdown']) {
174            $container->registerForAutoconfiguration(DropdownOptionProviderInterface::class)
175                ->addTag(DropdownOptionProviderInterface::class);
176        }
177    }
178}