Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.85% |
56 / 66 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ApieExtension | |
84.85% |
56 / 66 |
|
0.00% |
0 / 1 |
16.89 | |
0.00% |
0 / 1 |
| load | |
84.85% |
56 / 66 |
|
0.00% |
0 / 1 |
16.89 | |||
| 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_basic_auth' => [ |
| 36 | 'add_basic_auth.yaml', |
| 37 | ], |
| 38 | 'enable_ai_instructor' => [ |
| 39 | 'ai_instructor.yaml' |
| 40 | ], |
| 41 | 'enable_common_plugin' => [ |
| 42 | 'apie_common_plugin.yaml', |
| 43 | ], |
| 44 | 'enable_cms' => [ |
| 45 | 'common.yaml', |
| 46 | 'cms.yaml', |
| 47 | 'html_builders.yaml', |
| 48 | 'serializer.yaml', |
| 49 | 'sf_cms.yaml', |
| 50 | ], |
| 51 | 'enable_cms_dropdown' => [ |
| 52 | 'common.yaml', |
| 53 | 'cms_dropdown.yaml', |
| 54 | ], |
| 55 | 'enable_core' => [ |
| 56 | 'core.yaml', |
| 57 | 'sf_core.yaml', |
| 58 | ], |
| 59 | 'enable_doctrine_entity_converter' => [ |
| 60 | 'core.yaml', |
| 61 | 'doctrine_entity_converter.yaml', |
| 62 | ], |
| 63 | 'enable_doctrine_entity_datalayer' => [ |
| 64 | 'core.yaml', |
| 65 | 'doctrine_entity_converter.yaml', |
| 66 | 'doctrine_entity_datalayer.yaml', |
| 67 | ], |
| 68 | 'enable_doctrine_bundle_connection' => [ |
| 69 | 'core.yaml', |
| 70 | 'doctrine_entity_converter.yaml', |
| 71 | 'doctrine_entity_datalayer.yaml', |
| 72 | 'sf_doctrine.yaml', |
| 73 | ], |
| 74 | 'enable_export' => [ |
| 75 | 'html_builders.yaml', |
| 76 | 'serializer.yaml', |
| 77 | 'export.yaml', |
| 78 | ], |
| 79 | 'enable_ftp' => [ |
| 80 | 'apie_file_system.yaml', |
| 81 | 'ftp.yaml', |
| 82 | ], |
| 83 | 'enable_console' => [ |
| 84 | 'common.yaml', |
| 85 | 'console.yaml', |
| 86 | 'serializer.yaml', |
| 87 | ], |
| 88 | 'enable_maker' => [ |
| 89 | 'maker.yaml', |
| 90 | "sf_maker.yaml", |
| 91 | ], |
| 92 | 'enable_mcp_server' => [ |
| 93 | 'common.yaml', |
| 94 | 'mcp_server.yaml', |
| 95 | 'schema_generator.yaml', |
| 96 | 'serializer.yaml', |
| 97 | ], |
| 98 | 'enable_messenger' => [ |
| 99 | 'sf_messenger.yaml', |
| 100 | ], |
| 101 | 'enable_profiler' => [ |
| 102 | 'sf_profiler.yaml', |
| 103 | ], |
| 104 | 'enable_security' => [ |
| 105 | 'common.yaml', |
| 106 | 'serializer.yaml', |
| 107 | 'security.yaml', |
| 108 | ], |
| 109 | 'enable_rest_api' => [ |
| 110 | 'common.yaml', |
| 111 | 'rest_api.yaml', |
| 112 | 'schema_generator.yaml', |
| 113 | 'serializer.yaml', |
| 114 | ], |
| 115 | 'enable_faker' => [ |
| 116 | 'faker.yaml', |
| 117 | ], |
| 118 | 'enable_twig_template_layout_renderer' => [ |
| 119 | 'twig_template_layout_renderer.yaml', |
| 120 | ], |
| 121 | 'enable_typescript_client_builder' => [ |
| 122 | 'typescript_client_builder.yaml', |
| 123 | ], |
| 124 | 'enable_webdav' => [ |
| 125 | 'apie_file_system.yaml', |
| 126 | 'webdav.yaml', |
| 127 | ], |
| 128 | ]; |
| 129 | |
| 130 | /** |
| 131 | * @param array<string, mixed> $configs |
| 132 | */ |
| 133 | public function load(array $configs, ContainerBuilder $container): void |
| 134 | { |
| 135 | $loader = new YamlFileLoader($container, new ApieConfigFileLocator(__DIR__.'/../../resources/config')); |
| 136 | $loader->load('services.yaml'); |
| 137 | $loader->load('psr7.yaml'); |
| 138 | $configuration = $this->getConfiguration($configs, $container); |
| 139 | $config = $this->processConfiguration($configuration, $configs); |
| 140 | $container->setParameter('apie.encryption_key', $config['encryption_key'] ?? null); |
| 141 | $container->setParameter('apie.bounded_contexts', $config['bounded_contexts']); |
| 142 | $container->setParameter('apie.scan_bounded_contexts', $config['scan_bounded_contexts'] ?? []); |
| 143 | $container->setParameter('apie.storage', $config['storage'] ? : [['class' => InlineStorage::class]]); |
| 144 | $container->setParameter('apie.datalayers', $config['datalayers'] ?? []); |
| 145 | $container->setParameter('apie.ai.base_url', $config['ai']['base_url'] ?? 'http://ollama:11434'); |
| 146 | $container->setParameter('apie.ai.api_key', $config['ai']['api_key'] ?? 'No key'); |
| 147 | $container->setParameter('apie.cms.asset_folders', $config['cms']['asset_folders'] ?? []); |
| 148 | $container->setParameter('apie.cms.dashboard_template', $config['cms']['dashboard_template'] ?? '@Apie/dashboard.html.twig'); |
| 149 | $container->setParameter('apie.cms.error_template', $config['cms']['error_template'] ?? '@Apie/error.html.twig'); |
| 150 | $container->setParameter('apie.cms.base_url', rtrim($config['cms']['base_url'] ?? '/cms', '/')); |
| 151 | $container->setParameter('apie.doctrine.build_once', $config['doctrine']['build_once'] ?? false); |
| 152 | $container->setParameter('apie.doctrine.connection_params', $config['doctrine']['connection_params'] ?? []); |
| 153 | $container->setParameter('apie.doctrine.run_migrations', $config['doctrine']['run_migrations'] ?? false); |
| 154 | $container->setParameter('apie.rest_api.base_url', rtrim($config['rest_api']['base_url'] ?? '/api', '/')); |
| 155 | $container->setParameter('apie.remote_mcp_path', $config['remote_mcp_path'] ?? null); |
| 156 | $container->setParameter('apie.ftp_server.public_ip', $config['ftp_server']['public_ip'] ?? '127.0.0.1'); |
| 157 | if (($config['enable_maker'] ?? false) && is_array($config['maker'] ?? null)) { |
| 158 | $container->setParameter('apie.maker', $config['maker']); |
| 159 | } else { |
| 160 | $container->setParameter('apie.maker', null); |
| 161 | } |
| 162 | if ($config['enable_doctrine_entity_datalayer']) { |
| 163 | $type = $config['doctrine']['indexing']['type'] ?? 'direct'; |
| 164 | $container->addAliases([ |
| 165 | IndexStrategyInterface::class => match($type) { |
| 166 | 'direct' => DirectIndexStrategy::class, |
| 167 | 'late' => IndexAfterResponseIsSentStrategy::class, |
| 168 | 'background' => BackgroundIndexStrategy::class, |
| 169 | default => $config['doctrine']['indexing']['service'] ?? DirectIndexStrategy::class, |
| 170 | }, |
| 171 | ]); |
| 172 | if ($type === 'background') { |
| 173 | $definition = new Definition(ApieUpdateIdfCommand::class); |
| 174 | $definition->setArguments([ |
| 175 | new Reference(EntityReindexer::class), |
| 176 | new Reference(OrmBuilder::class) |
| 177 | ]); |
| 178 | $definition->addTag('console.command'); |
| 179 | $container->addDefinitions([ApieUpdateIdfCommand::class => $definition]); |
| 180 | } |
| 181 | } |
| 182 | $loaded = []; |
| 183 | foreach ($this->dependencies as $configName => $dependencyList) { |
| 184 | if ($config[$configName]) { |
| 185 | foreach ($dependencyList as $dependency) { |
| 186 | if (!isset($loaded[$dependency])) { |
| 187 | $loaded[$dependency] = true; |
| 188 | $loader->load($dependency); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | $container->registerForAutoconfiguration(ContextBuilderInterface::class) |
| 195 | ->addTag('apie.core.context_builder'); |
| 196 | $container->registerForAutoconfiguration(ApieContextService::class) |
| 197 | ->addTag('apie.context'); |
| 198 | $container->registerForAutoconfiguration(RouteDefinitionProviderInterface::class) |
| 199 | ->addTag('apie.common.route_definition'); |
| 200 | $container->registerForAutoconfiguration(ApieDatalayer::class) |
| 201 | ->addTag('apie.datalayer'); |
| 202 | $container->registerForAutoconfiguration(ApieClassFaker::class) |
| 203 | ->addTag('apie.faker'); |
| 204 | if ($config['enable_cms']) { |
| 205 | $container->registerForAutoconfiguration(FormComponentProviderInterface::class) |
| 206 | ->addTag(FormComponentProviderInterface::class); |
| 207 | } |
| 208 | if ($config['enable_cms_dropdown']) { |
| 209 | $container->registerForAutoconfiguration(DropdownOptionProviderInterface::class) |
| 210 | ->addTag(DropdownOptionProviderInterface::class); |
| 211 | } |
| 212 | } |
| 213 | } |