Skip to content

Commit

Permalink
Migrate config to PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Dec 13, 2023
1 parent f190d18 commit 969dcc6
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 233 deletions.
22 changes: 13 additions & 9 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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;
Expand Down Expand Up @@ -62,13 +62,18 @@ 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_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']);
Expand Down Expand Up @@ -143,7 +148,7 @@ public function load(array $configs, ContainerBuilder $container): void
]);
});

$this->loadMessengerServices($container);
$this->loadMessengerServices($container, $loader);

$this->loadEntityValueResolverServices($container, $loader, $config);
}
Expand Down Expand Up @@ -385,21 +390,20 @@ 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<string, mixed> $config */
private function loadEntityValueResolverServices(ContainerBuilder $container, FileLoader $loader, array $config): void
{
$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');
Expand Down
32 changes: 32 additions & 0 deletions Resources/config/cache_warmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

use Doctrine\Bundle\MongoDBBundle\CacheWarmer\HydratorCacheWarmer;
use Doctrine\Bundle\MongoDBBundle\CacheWarmer\PersistentCollectionCacheWarmer;
use Doctrine\Bundle\MongoDBBundle\CacheWarmer\ProxyCacheWarmer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->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'),
]);
};
57 changes: 57 additions & 0 deletions Resources/config/command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

use Doctrine\Bundle\MongoDBBundle\Command\ClearMetadataCacheDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\CreateSchemaDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\DropSchemaDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\GenerateHydratorsDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\GenerateProxiesDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\InfoDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\QueryDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\ShardDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Command\UpdateSchemaDoctrineODMCommand;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->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']);
};
25 changes: 25 additions & 0 deletions Resources/config/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Doctrine\Bundle\MongoDBBundle\Form\DoctrineMongoDBTypeGuesser;
use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->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'),
]);
};
34 changes: 34 additions & 0 deletions Resources/config/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use Doctrine\Bundle\MongoDBBundle\APM\CommandLoggerRegistry;
use Doctrine\Bundle\MongoDBBundle\APM\PSRCommandLogger;
use Doctrine\Bundle\MongoDBBundle\APM\StopwatchCommandLogger;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->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');
};
18 changes: 18 additions & 0 deletions Resources/config/messenger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set('doctrine_mongodb.messenger.event_subscriber.doctrine_clear_document_manager', DoctrineClearEntityManagerWorkerSubscriber::class)
->tag('kernel.event_subscriber')
->args([
service('doctrine_mongodb'),
]);
};
13 changes: 0 additions & 13 deletions Resources/config/messenger.xml

This file was deleted.

91 changes: 91 additions & 0 deletions Resources/config/mongodb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoader;
use Doctrine\Bundle\MongoDBBundle\ManagerConfigurator;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Bundle\MongoDBBundle\Mapping\Driver\XmlDriver;
use Doctrine\Bundle\MongoDBBundle\Repository\ContainerRepositoryFactory;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use Doctrine\ODM\MongoDB\Tools\ResolveTargetDocumentListener;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use MongoDB\Client;
use ProxyManager\Proxy\GhostObjectInterface;
use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->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')
->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'),
]);
};
Loading

0 comments on commit 969dcc6

Please sign in to comment.