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