From d4af63369c7e7e72c4c2e037abab074de16d97da Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Wed, 13 Dec 2023 13:47:39 +0100 Subject: [PATCH] Migrate config to PHP --- .../DoctrineMongoDBExtension.php | 23 +-- Resources/config/cache.php | 23 +++ Resources/config/cache_warmer.php | 32 ++++ Resources/config/command.php | 57 +++++++ Resources/config/form.php | 25 +++ Resources/config/logger.php | 34 ++++ Resources/config/messenger.php | 18 +++ Resources/config/messenger.xml | 13 -- Resources/config/mongodb.php | 83 ++++++++++ Resources/config/mongodb.xml | 145 ------------------ Resources/config/profiler.php | 19 +++ Resources/config/rest.php | 73 +++++++++ Resources/config/validator.php | 25 +++ Resources/config/value_resolver.php | 28 ++++ Resources/config/value_resolver.xml | 20 --- .../AbstractMongoDBExtensionTestCase.php | 3 - 16 files changed, 431 insertions(+), 190 deletions(-) create mode 100644 Resources/config/cache.php create mode 100644 Resources/config/cache_warmer.php create mode 100644 Resources/config/command.php create mode 100644 Resources/config/form.php create mode 100644 Resources/config/logger.php create mode 100644 Resources/config/messenger.php delete mode 100644 Resources/config/messenger.xml create mode 100644 Resources/config/mongodb.php create mode 100644 Resources/config/profiler.php create mode 100644 Resources/config/rest.php create mode 100644 Resources/config/validator.php create mode 100644 Resources/config/value_resolver.php delete mode 100644 Resources/config/value_resolver.xml diff --git a/DependencyInjection/DoctrineMongoDBExtension.php b/DependencyInjection/DoctrineMongoDBExtension.php index 4d948580..7f2141f3 100644 --- a/DependencyInjection/DoctrineMongoDBExtension.php +++ b/DependencyInjection/DoctrineMongoDBExtension.php @@ -32,7 +32,7 @@ use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use Symfony\Component\Messenger\MessageBusInterface; @@ -63,13 +63,19 @@ class DoctrineMongoDBExtension extends AbstractDoctrineExtension */ public function load(array $configs, ContainerBuilder $container): void { - // Load DoctrineMongoDBBundle/Resources/config/mongodb.xml - $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); + $loader = new PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $loader->load('mongodb.xml'); + $loader->load('mongodb.php'); + $loader->load('cache.php'); + $loader->load('cache_warmer.php'); + $loader->load('command.php'); + $loader->load('form.php'); + $loader->load('logger.php'); + $loader->load('profiler.php'); + $loader->load('validator.php'); if (empty($config['default_connection'])) { $keys = array_keys($config['connections']); @@ -144,7 +150,7 @@ public function load(array $configs, ContainerBuilder $container): void ]); }); - $this->loadMessengerServices($container); + $this->loadMessengerServices($container, $loader); $this->loadEntityValueResolverServices($container, $loader, $config); } @@ -386,15 +392,14 @@ protected function loadConnections(array $connections, ContainerBuilder $contain $container->setParameter('doctrine_mongodb.odm.connections', $cons); } - private function loadMessengerServices(ContainerBuilder $container): void + private function loadMessengerServices(ContainerBuilder $container, FileLoader $loader): void { /** @psalm-suppress UndefinedClass Optional dependency */ if (! interface_exists(MessageBusInterface::class) || ! class_exists(DoctrineClearEntityManagerWorkerSubscriber::class)) { return; } - $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); - $loader->load('messenger.xml'); + $loader->load('messenger.php'); } /** @param array $config */ @@ -405,7 +410,7 @@ private function loadEntityValueResolverServices(ContainerBuilder $container, Fi return; } - $loader->load('value_resolver.xml'); + $loader->load('value_resolver.php'); if (! class_exists(ExpressionLanguage::class)) { $container->removeDefinition('doctrine_mongodb.odm.document_value_resolver.expression_language'); diff --git a/Resources/config/cache.php b/Resources/config/cache.php new file mode 100644 index 00000000..33ebab13 --- /dev/null +++ b/Resources/config/cache.php @@ -0,0 +1,23 @@ +parameters() + ->set('doctrine_mongodb.odm.cache.array.class', 'Doctrine\Common\Cache\ArrayCache') + ->set('doctrine_mongodb.odm.cache.apc.class', 'Doctrine\Common\Cache\ApcCache') + ->set('doctrine_mongodb.odm.cache.apcu.class', 'Doctrine\Common\Cache\ApcuCache') + ->set('doctrine_mongodb.odm.cache.memcache.class', 'Doctrine\Common\Cache\MemcacheCache') + ->set('doctrine_mongodb.odm.cache.memcache_host', 'localhost') + ->set('doctrine_mongodb.odm.cache.memcache_port', 11211) + ->set('doctrine_mongodb.odm.cache.memcache_instance.class', 'Memcache') + ->set('doctrine_mongodb.odm.cache.xcache.class', 'Doctrine\Common\Cache\XcacheCache'); + + $containerConfigurator->services() + + ->set('doctrine_mongodb.odm.cache.array', '%doctrine_mongodb.odm.cache.array.class%') + + ->alias('doctrine_mongodb.odm.cache', 'doctrine_mongodb.odm.cache.array'); +}; diff --git a/Resources/config/cache_warmer.php b/Resources/config/cache_warmer.php new file mode 100644 index 00000000..38d8181c --- /dev/null +++ b/Resources/config/cache_warmer.php @@ -0,0 +1,32 @@ +services() + + ->set('doctrine_mongodb.odm.proxy_cache_warmer', ProxyCacheWarmer::class) + ->tag('kernel.cache_warmer', ['priority' => 25]) + ->args([ + service('service_container'), + ]) + + ->set('doctrine_mongodb.odm.hydrator_cache_warmer', HydratorCacheWarmer::class) + ->tag('kernel.cache_warmer', ['priority' => 25]) + ->args([ + service('service_container'), + ]) + + ->set('doctrine_mongodb.odm.persistent_collection_cache_warmer', PersistentCollectionCacheWarmer::class) + ->tag('kernel.cache_warmer', ['priority' => 25]) + ->args([ + service('service_container'), + ]); +}; diff --git a/Resources/config/command.php b/Resources/config/command.php new file mode 100644 index 00000000..01e4e744 --- /dev/null +++ b/Resources/config/command.php @@ -0,0 +1,57 @@ +services() + ->set('doctrine_mongodb.odm.command.clear_metadata_cache', ClearMetadataCacheDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:cache:clear-metadata']) + + ->set('doctrine_mongodb.odm.command.create_schema', CreateSchemaDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:schema:create']) + + ->set('doctrine_mongodb.odm.command.drop_schema', DropSchemaDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:schema:drop']) + + ->set('doctrine_mongodb.odm.command.generate_hydrators', GenerateHydratorsDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:generate:hydrators']) + + ->set('doctrine_mongodb.odm.command.generate_proxies', GenerateProxiesDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:generate:proxies']) + + ->set('doctrine_mongodb.odm.command.info', InfoDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:mapping:info']) + ->args([ + service('doctrine_mongodb'), + ]) + + ->set('doctrine_mongodb.odm.command.load_data_fixtures', LoadDataFixturesDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:fixtures:load']) + ->args([ + service('doctrine_mongodb'), + service('doctrine_mongodb.odm.symfony.fixtures.loader'), + ]) + + ->set('doctrine_mongodb.odm.command.query', QueryDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:query']) + + ->set('doctrine_mongodb.odm.command.shard', ShardDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:schema:shard']) + + ->set('doctrine_mongodb.odm.command.update_schema', UpdateSchemaDoctrineODMCommand::class) + ->tag('console.command', ['command' => 'doctrine:mongodb:schema:update']); +}; diff --git a/Resources/config/form.php b/Resources/config/form.php new file mode 100644 index 00000000..647e4a53 --- /dev/null +++ b/Resources/config/form.php @@ -0,0 +1,25 @@ +services() + + ->set('form.type.mongodb_document', DocumentType::class) + ->tag('form.type', ['alias' => 'document']) + ->args([ + service('doctrine_mongodb'), + ]) + + ->set('form.type_guesser.doctrine.mongodb', DoctrineMongoDBTypeGuesser::class) + ->tag('form.type_guesser') + ->args([ + service('doctrine_mongodb'), + ]); +}; diff --git a/Resources/config/logger.php b/Resources/config/logger.php new file mode 100644 index 00000000..5c2f6ff9 --- /dev/null +++ b/Resources/config/logger.php @@ -0,0 +1,34 @@ +services() + + ->set('doctrine_mongodb.odm.command_logger_registry', CommandLoggerRegistry::class) + ->public() + ->args([ + tagged_iterator('doctrine_mongodb.odm.command_logger'), + ]) + + ->set('doctrine_mongodb.odm.stopwatch_command_logger', StopwatchCommandLogger::class) + ->args([ + service('debug.stopwatch')->nullOnInvalid(), + ]) + + ->set('doctrine_mongodb.odm.psr_command_logger', PSRCommandLogger::class) + ->tag('monolog.logger', ['channel' => 'doctrine']) + ->args([ + service('logger')->nullOnInvalid(), + ]) + + ->alias('doctrine_mongodb.odm.command_logger', 'doctrine_mongodb.odm.psr_command_logger'); +}; diff --git a/Resources/config/messenger.php b/Resources/config/messenger.php new file mode 100644 index 00000000..c3fdd90a --- /dev/null +++ b/Resources/config/messenger.php @@ -0,0 +1,18 @@ +services(); + + $services->set('doctrine_mongodb.messenger.event_subscriber.doctrine_clear_document_manager', DoctrineClearEntityManagerWorkerSubscriber::class) + ->tag('kernel.event_subscriber') + ->args([ + service('doctrine_mongodb'), + ]); +}; diff --git a/Resources/config/messenger.xml b/Resources/config/messenger.xml deleted file mode 100644 index d50cd761..00000000 --- a/Resources/config/messenger.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - diff --git a/Resources/config/mongodb.php b/Resources/config/mongodb.php new file mode 100644 index 00000000..a309b571 --- /dev/null +++ b/Resources/config/mongodb.php @@ -0,0 +1,83 @@ +parameters() + ->set('doctrine_mongodb.odm.connection.class', Client::class) + ->set('doctrine_mongodb.odm.configuration.class', Configuration::class) + ->set('doctrine_mongodb.odm.document_manager.class', DocumentManager::class) + ->set('doctrine_mongodb.odm.manager_configurator.class', ManagerConfigurator::class) + ->set('doctrine_mongodb.odm.class', ManagerRegistry::class) + ->set('doctrine_mongodb.odm.metadata.driver_chain.class', MappingDriverChain::class) + ->set('doctrine_mongodb.odm.metadata.attribute.class', AttributeDriver::class) + ->set('doctrine_mongodb.odm.metadata.xml.class', XmlDriver::class) + ->set('doctrine_mongodb.odm.mapping_dirs', []) + ->set('doctrine_mongodb.odm.xml_mapping_dirs', '%doctrine_mongodb.odm.mapping_dirs%') + ->set('doctrine_mongodb.odm.document_dirs', []) + ->set('doctrine_mongodb.odm.fixtures_dirs', []); + + $containerConfigurator->services() + + ->alias(DocumentManager::class, 'doctrine_mongodb.odm.document_manager') + + ->alias(ManagerRegistry::class, 'doctrine_mongodb') + + ->set('doctrine_mongodb.odm.connection.event_manager', ContainerAwareEventManager::class) + ->abstract(true) + ->args([ + service('service_container'), + ]) + + ->set('doctrine_mongodb.odm.container_repository_factory', ContainerRepositoryFactory::class) + ->args(['']) + + ->set('doctrine_mongodb.odm.manager_configurator.abstract', ManagerConfigurator::class) + ->abstract(true) + ->args([ + [], + ]) + + ->set('doctrine_mongodb.odm.security.user.provider', EntityUserProvider::class) + ->abstract(true) + ->args([ + service('doctrine_mongodb'), + ]) + + ->set('doctrine_mongodb', '%doctrine_mongodb.odm.class%') + ->public() + ->args([ + 'MongoDB', + '%doctrine_mongodb.odm.connections%', + '%doctrine_mongodb.odm.document_managers%', + '%doctrine_mongodb.odm.default_connection%', + '%doctrine_mongodb.odm.default_document_manager%', + GhostObjectInterface::class, + service('service_container'), + ]) + + ->set('doctrine_mongodb.odm.listeners.resolve_target_document', ResolveTargetDocumentListener::class) + + ->set('doctrine_mongodb.odm.symfony.fixtures.loader', SymfonyFixturesLoader::class) + ->args([ + service('service_container'), + ]); +}; diff --git a/Resources/config/mongodb.xml b/Resources/config/mongodb.xml index 0cf838b6..2899af3a 100644 --- a/Resources/config/mongodb.xml +++ b/Resources/config/mongodb.xml @@ -11,23 +11,8 @@ Doctrine\ODM\MongoDB\DocumentManager Doctrine\Bundle\MongoDBBundle\ManagerConfigurator Symfony\Bridge\Doctrine\ContainerAwareEventManager - Symfony\Bridge\Doctrine\Validator\DoctrineInitializer - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator Doctrine\Bundle\MongoDBBundle\ManagerRegistry Symfony\Bridge\Doctrine\Security\User\EntityUserProvider - Doctrine\Bundle\MongoDBBundle\CacheWarmer\ProxyCacheWarmer - Doctrine\Bundle\MongoDBBundle\CacheWarmer\HydratorCacheWarmer - Doctrine\Bundle\MongoDBBundle\CacheWarmer\PersistentCollectionCacheWarmer - - - Doctrine\Common\Cache\ArrayCache - Doctrine\Common\Cache\ApcCache - Doctrine\Common\Cache\ApcuCache - Doctrine\Common\Cache\MemcacheCache - localhost - 11211 - Memcache - Doctrine\Common\Cache\XcacheCache Doctrine\Persistence\Mapping\Driver\MappingDriverChain @@ -43,146 +28,16 @@ 4 - - Doctrine\ODM\MongoDB\Tools\ResolveTargetDocumentListener - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - MongoDB - %doctrine_mongodb.odm.connections% - %doctrine_mongodb.odm.document_managers% - %doctrine_mongodb.odm.default_connection% - %doctrine_mongodb.odm.default_document_manager% - ProxyManager\Proxy\GhostObjectInterface - - - - - - - diff --git a/Resources/config/profiler.php b/Resources/config/profiler.php new file mode 100644 index 00000000..af3e944a --- /dev/null +++ b/Resources/config/profiler.php @@ -0,0 +1,19 @@ +services() + ->set('doctrine_mongodb.odm.data_collector.command_logger', CommandLogger::class) + + ->set('doctrine_mongodb.odm.data_collector', CommandDataCollector::class) + ->args([ + service('doctrine_mongodb.odm.data_collector.command_logger'), + ]); +}; diff --git a/Resources/config/rest.php b/Resources/config/rest.php new file mode 100644 index 00000000..5adc493d --- /dev/null +++ b/Resources/config/rest.php @@ -0,0 +1,73 @@ +parameters() +// ->set('doctrine_mongodb.odm.connection.class', Client::class) +// ->set('doctrine_mongodb.odm.configuration.class', Configuration::class) +// ->set('doctrine_mongodb.odm.document_manager.class', DocumentManager::class) +// ->set('doctrine_mongodb.odm.manager_configurator.class', ManagerConfigurator::class) + ->set('doctrine_mongodb.odm.class', ManagerRegistry::class); +// ->set('doctrine_mongodb.odm.metadata.driver_chain.class', MappingDriverChain::class) +// ->set('doctrine_mongodb.odm.metadata.attribute.class', AttributeDriver::class) +// ->set('doctrine_mongodb.odm.metadata.xml.class', XmlDriver::class) +// ->set('doctrine_mongodb.odm.mapping_dirs', []) +// ->set('doctrine_mongodb.odm.xml_mapping_dirs', '%doctrine_mongodb.odm.mapping_dirs%') +// ->set('doctrine_mongodb.odm.document_dirs', []) +// ->set('doctrine_mongodb.odm.fixtures_dirs', []); + + $containerConfigurator->services() + + ->set('doctrine_mongodb.odm.connection.event_manager', ContainerAwareEventManager::class) + ->abstract(true) + ->args([ + service('service_container'), + ]) + + ->set('doctrine_mongodb.odm.container_repository_factory', ContainerRepositoryFactory::class) + ->args(['']) + + ->set('doctrine_mongodb.odm.manager_configurator.abstract', Configuration::class) + ->abstract(true) + ->args([ + [], + ]) + + ->set('doctrine_mongodb.odm.security.user.provider', EntityUserProvider::class) + ->abstract(true) + ->args([ + service('doctrine_mongodb'), + ]) + + ->set('doctrine_mongodb', '%doctrine_mongodb.odm.class%') + ->public() + ->args([ + 'MongoDB', + '%doctrine_mongodb.odm.connections%', + '%doctrine_mongodb.odm.document_managers%', + '%doctrine_mongodb.odm.default_connection%', + '%doctrine_mongodb.odm.default_document_manager%', + GhostObjectInterface::class, + service('service_container'), + ]) + + ->set('doctrine_mongodb.odm.listeners.resolve_target_document', ResolveTargetDocumentListener::class) + + ->set('doctrine_mongodb.odm.symfony.fixtures.loader', SymfonyFixturesLoader::class) + ->args([ + service('service_container'), + ]); +}; diff --git a/Resources/config/validator.php b/Resources/config/validator.php new file mode 100644 index 00000000..6351359b --- /dev/null +++ b/Resources/config/validator.php @@ -0,0 +1,25 @@ +services() + + ->set('doctrine_odm.mongodb.validator.unique', UniqueEntityValidator::class) + ->tag('validator.constraint_validator', ['alias' => 'doctrine_odm.mongodb.unique']) + ->args([ + service('doctrine_mongodb'), + ]) + + ->set('doctrine_odm.mongodb.validator_initializer', DoctrineInitializer::class) + ->tag('validator.initializer') + ->args([ + service('doctrine_mongodb'), + ]); +}; diff --git a/Resources/config/value_resolver.php b/Resources/config/value_resolver.php new file mode 100644 index 00000000..3ed803ea --- /dev/null +++ b/Resources/config/value_resolver.php @@ -0,0 +1,28 @@ +services(); + + $services->set('doctrine_mongodb.odm.entity_value_resolver', EntityValueResolver::class) + ->args([ + service('doctrine_mongodb'), + service('doctrine_mongodb.odm.document_value_resolver.expression_language')->ignoreOnInvalid(), + ]); + + $services->set('doctrine_mongodb.odm.document_value_resolver.expression_language', ExpressionLanguage::class); + + $services->set('doctrine_mongodb.odm.document_value_resolver', DocumentValueResolver::class) + ->tag('controller.argument_value_resolver', ['name' => DocumentValueResolver::class, 'priority' => 110]) + ->args([ + service('doctrine_mongodb.odm.entity_value_resolver'), + ]); +}; diff --git a/Resources/config/value_resolver.xml b/Resources/config/value_resolver.xml deleted file mode 100644 index 92fcd9e6..00000000 --- a/Resources/config/value_resolver.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - controller.argument_value_resolver - - - diff --git a/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php b/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php index 1376052c..c4a4ac46 100644 --- a/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php +++ b/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php @@ -22,7 +22,6 @@ use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use MongoDB\Client; use PHPUnit\Framework\AssertionFailedError; -use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -66,8 +65,6 @@ public function testDependencyInjectionConfigurationDefaults(): void $this->assertEquals(AttributeDriver::class, $container->getParameter('doctrine_mongodb.odm.metadata.attribute.class')); $this->assertEquals(XmlDriver::class, $container->getParameter('doctrine_mongodb.odm.metadata.xml.class')); - $this->assertEquals(UniqueEntityValidator::class, $container->getParameter('doctrine_odm.mongodb.validator.unique.class')); - $config = DoctrineMongoDBExtensionTest::buildConfiguration([ 'proxy_namespace' => 'MyProxies', 'auto_generate_proxy_classes' => true,