Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate config to PHP #810

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Doctrine\Common\Cache\RedisCache;
use Doctrine\Common\DataFixtures\Loader as DataFixturesLoader;
use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\Configuration as ODMConfiguration;
use Doctrine\ODM\MongoDB\DocumentManager;
use InvalidArgumentException;
use MongoDB\Client;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
Expand All @@ -31,7 +33,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 +64,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 +150,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 @@ -224,7 +231,7 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
$configurationId = sprintf('doctrine_mongodb.odm.%s_configuration', $documentManager['name']);
$defaultDatabase = $documentManager['database'] ?? $defaultDB;

$odmConfigDef = new Definition('%doctrine_mongodb.odm.configuration.class%');
$odmConfigDef = new Definition(ODMConfiguration::class);
$odmConfigDef->addTag(self::CONFIGURATION_TAG);
$container->setDefinition(
$configurationId,
Expand Down Expand Up @@ -260,13 +267,6 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
$methods['setPersistentCollectionFactory'] = new Reference($documentManager['persistent_collection_factory']);
}

$container->getAlias('doctrine_mongodb.odm.command_logger')
->setDeprecated(
'doctrine/mongodb-odm-bundle',
'4.4',
'The service %alias_id% is deprecated and will be dropped in DoctrineMongoDBBundle 5.0. Use "doctrine_mongodb.odm.psr_command_logger" instead.',
);

// logging
if ($container->getParameterBag()->resolveValue($documentManager['logging'])) {
$container->getDefinition('doctrine_mongodb.odm.psr_command_logger')
Expand Down Expand Up @@ -320,8 +320,8 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
// Document managers will share their connection's event manager
new Reference(sprintf('doctrine_mongodb.odm.%s_connection.event_manager', $connectionName)),
];
$odmDmDef = new Definition('%doctrine_mongodb.odm.document_manager.class%', $odmDmArgs);
$odmDmDef->setFactory(['%doctrine_mongodb.odm.document_manager.class%', 'create']);
$odmDmDef = new Definition(DocumentManager::class, $odmDmArgs);
$odmDmDef->setFactory([DocumentManager::class, 'create']);
$odmDmDef->addTag('doctrine_mongodb.odm.document_manager');
$odmDmDef->setPublic(true);

Expand Down Expand Up @@ -365,7 +365,7 @@ protected function loadConnections(array $connections, ContainerBuilder $contain
$configurationId = sprintf('doctrine_mongodb.odm.%s_configuration', $name);
$container->setDefinition(
$configurationId,
new Definition('%doctrine_mongodb.odm.configuration.class%'),
new Definition(ODMConfiguration::class),
);

$odmConnArgs = [
Expand All @@ -375,7 +375,7 @@ protected function loadConnections(array $connections, ContainerBuilder $contain
$this->normalizeDriverOptions($connection),
];

$odmConnDef = new Definition('%doctrine_mongodb.odm.connection.class%', $odmConnArgs);
$odmConnDef = new Definition(Client::class, $odmConnArgs);
$odmConnDef->setPublic(true);
$id = sprintf('doctrine_mongodb.odm.%s_connection', $name);
$container->setDefinition($id, $odmConnDef);
Expand All @@ -385,21 +385,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'),
]);
};
32 changes: 32 additions & 0 deletions Resources/config/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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(),
]);
};
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.

80 changes: 80 additions & 0 deletions Resources/config/mongodb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use Doctrine\ODM\MongoDB\Tools\ResolveTargetDocumentListener;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
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\abstract_arg;
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.metadata.driver_chain.class', MappingDriverChain::class)
->set('doctrine_mongodb.odm.metadata.attribute.class', AttributeDriver::class)
->set('doctrine_mongodb.odm.metadata.xml.class', XmlDriver::class);
Comment on lines +24 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #811, the parameters should be deprecated here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@GromNaN GromNaN Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we stop using them, we have to keep and deprecate them in 2.x, as they could be used in applications.

Copy link
Contributor Author

@franmomu franmomu Dec 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that since these parameters seem to be used for internal configuration, maybe Symfony can try to fetch them first as build parameters (prefixing the parameter with a ".") and fallback to "normal", I'll see if I have time tomorrow to open a PR there for that.

Update: Symfony always calls an abstract method that is implemented in our side to get the parameter name, so the build parameters can be implemented in our side I think.

Update2: We can do it for metadata parameters, but for cache parameters I think need to be done in the AbstractDoctrineExtension, I'll take a look tomorrow if I find some time.


$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()
->args([
service('service_container'),
])

->set('doctrine_mongodb.odm.container_repository_factory', ContainerRepositoryFactory::class)
->args([
abstract_arg('service locator'),
])

->set('doctrine_mongodb.odm.manager_configurator.abstract', ManagerConfigurator::class)
->abstract()
->args([
abstract_arg('enabled filters'),
])

->set('doctrine_mongodb.odm.security.user.provider', EntityUserProvider::class)
->abstract()
->args([
service('doctrine_mongodb'),
])

->set('doctrine_mongodb', ManagerRegistry::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);
};
Loading