Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.62% |
55 / 65 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ApieExtension | |
84.62% |
55 / 65 |
|
0.00% |
0 / 1 |
16.93 | |
0.00% |
0 / 1 |
| load | |
84.62% |
55 / 65 |
|
0.00% |
0 / 1 |
16.93 | |||
| 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_mcp_server' => [ |
| 81 | 'common.yaml', |
| 82 | 'mcp_server.yaml', |
| 83 | 'schema_generator.yaml', |
| 84 | 'serializer.yaml', |
| 85 | ], |
| 86 | 'enable_messenger' => [ |
| 87 | 'sf_messenger.yaml', |
| 88 | ], |
| 89 | 'enable_profiler' => [ |
| 90 | 'sf_profiler.yaml', |
| 91 | ], |
| 92 | 'enable_security' => [ |
| 93 | 'common.yaml', |
| 94 | 'serializer.yaml', |
| 95 | 'security.yaml', |
| 96 | ], |
| 97 | 'enable_rest_api' => [ |
| 98 | 'common.yaml', |
| 99 | 'rest_api.yaml', |
| 100 | 'schema_generator.yaml', |
| 101 | 'serializer.yaml', |
| 102 | ], |
| 103 | 'enable_faker' => [ |
| 104 | 'faker.yaml', |
| 105 | ], |
| 106 | 'enable_twig_template_layout_renderer' => [ |
| 107 | 'twig_template_layout_renderer.yaml', |
| 108 | ], |
| 109 | 'enable_typescript_client_builder' => [ |
| 110 | 'typescript_client_builder.yaml', |
| 111 | ], |
| 112 | 'enable_webdav' => [ |
| 113 | 'apie_file_system.yaml', |
| 114 | 'webdav.yaml', |
| 115 | ], |
| 116 | ]; |
| 117 | |
| 118 | /** |
| 119 | * @param array<string, mixed> $configs |
| 120 | */ |
| 121 | public function load(array $configs, ContainerBuilder $container): void |
| 122 | { |
| 123 | $loader = new YamlFileLoader($container, new ApieConfigFileLocator(__DIR__.'/../../resources/config')); |
| 124 | $loader->load('services.yaml'); |
| 125 | $loader->load('psr7.yaml'); |
| 126 | $configuration = $this->getConfiguration($configs, $container); |
| 127 | $config = $this->processConfiguration($configuration, $configs); |
| 128 | $container->setParameter('apie.encryption_key', $config['encryption_key'] ?? null); |
| 129 | $container->setParameter('apie.bounded_contexts', $config['bounded_contexts']); |
| 130 | $container->setParameter('apie.scan_bounded_contexts', $config['scan_bounded_contexts'] ?? []); |
| 131 | $container->setParameter('apie.storage', $config['storage'] ? : [['class' => InlineStorage::class]]); |
| 132 | $container->setParameter('apie.datalayers', $config['datalayers'] ?? []); |
| 133 | $container->setParameter('apie.ai.base_url', $config['ai']['base_url'] ?? 'http://ollama:11434'); |
| 134 | $container->setParameter('apie.ai.api_key', $config['ai']['api_key'] ?? 'No key'); |
| 135 | $container->setParameter('apie.cms.asset_folders', $config['cms']['asset_folders'] ?? []); |
| 136 | $container->setParameter('apie.cms.dashboard_template', $config['cms']['dashboard_template'] ?? '@Apie/dashboard.html.twig'); |
| 137 | $container->setParameter('apie.cms.error_template', $config['cms']['error_template'] ?? '@Apie/error.html.twig'); |
| 138 | $container->setParameter('apie.cms.base_url', rtrim($config['cms']['base_url'] ?? '/cms', '/')); |
| 139 | $container->setParameter('apie.doctrine.build_once', $config['doctrine']['build_once'] ?? false); |
| 140 | $container->setParameter('apie.doctrine.connection_params', $config['doctrine']['connection_params'] ?? []); |
| 141 | $container->setParameter('apie.doctrine.run_migrations', $config['doctrine']['run_migrations'] ?? false); |
| 142 | $container->setParameter('apie.rest_api.base_url', rtrim($config['rest_api']['base_url'] ?? '/api', '/')); |
| 143 | $container->setParameter('apie.remote_mcp_path', $config['remote_mcp_path'] ?? null); |
| 144 | if (($config['enable_maker'] ?? false) && is_array($config['maker'] ?? null)) { |
| 145 | $container->setParameter('apie.maker', $config['maker']); |
| 146 | } else { |
| 147 | $container->setParameter('apie.maker', null); |
| 148 | } |
| 149 | if ($config['enable_doctrine_entity_datalayer']) { |
| 150 | $type = $config['doctrine']['indexing']['type'] ?? 'direct'; |
| 151 | $container->addAliases([ |
| 152 | IndexStrategyInterface::class => match($type) { |
| 153 | 'direct' => DirectIndexStrategy::class, |
| 154 | 'late' => IndexAfterResponseIsSentStrategy::class, |
| 155 | 'background' => BackgroundIndexStrategy::class, |
| 156 | default => $config['doctrine']['indexing']['service'] ?? DirectIndexStrategy::class, |
| 157 | }, |
| 158 | ]); |
| 159 | if ($type === 'background') { |
| 160 | $definition = new Definition(ApieUpdateIdfCommand::class); |
| 161 | $definition->setArguments([ |
| 162 | new Reference(EntityReindexer::class), |
| 163 | new Reference(OrmBuilder::class) |
| 164 | ]); |
| 165 | $definition->addTag('console.command'); |
| 166 | $container->addDefinitions([ApieUpdateIdfCommand::class => $definition]); |
| 167 | } |
| 168 | } |
| 169 | $loaded = []; |
| 170 | foreach ($this->dependencies as $configName => $dependencyList) { |
| 171 | if ($config[$configName]) { |
| 172 | foreach ($dependencyList as $dependency) { |
| 173 | if (!isset($loaded[$dependency])) { |
| 174 | $loaded[$dependency] = true; |
| 175 | $loader->load($dependency); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | $container->registerForAutoconfiguration(ContextBuilderInterface::class) |
| 182 | ->addTag('apie.core.context_builder'); |
| 183 | $container->registerForAutoconfiguration(ApieContextService::class) |
| 184 | ->addTag('apie.context'); |
| 185 | $container->registerForAutoconfiguration(RouteDefinitionProviderInterface::class) |
| 186 | ->addTag('apie.common.route_definition'); |
| 187 | $container->registerForAutoconfiguration(ApieDatalayer::class) |
| 188 | ->addTag('apie.datalayer'); |
| 189 | $container->registerForAutoconfiguration(ApieClassFaker::class) |
| 190 | ->addTag('apie.faker'); |
| 191 | if ($config['enable_cms']) { |
| 192 | $container->registerForAutoconfiguration(FormComponentProviderInterface::class) |
| 193 | ->addTag(FormComponentProviderInterface::class); |
| 194 | } |
| 195 | if ($config['enable_cms_dropdown']) { |
| 196 | $container->registerForAutoconfiguration(DropdownOptionProviderInterface::class) |
| 197 | ->addTag(DropdownOptionProviderInterface::class); |
| 198 | } |
| 199 | } |
| 200 | } |