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