Skip to content

Commit

Permalink
Deprecate modifying class parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Dec 13, 2023
1 parent c53d147 commit 6e76da7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler;

use Doctrine\Bundle\MongoDBBundle\CacheWarmer\HydratorCacheWarmer;
use Doctrine\Bundle\MongoDBBundle\CacheWarmer\PersistentCollectionCacheWarmer;
use Doctrine\Bundle\MongoDBBundle\CacheWarmer\ProxyCacheWarmer;
use Doctrine\Bundle\MongoDBBundle\ManagerConfigurator;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\ODM\MongoDB\Configuration as MongoDBConfiguration;
use Doctrine\ODM\MongoDB\DocumentManager;
use MongoDB\Client;
use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
use Symfony\Bridge\Doctrine\Validator\DoctrineInitializer;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use function trigger_deprecation;

/** @internal */
final class DeprecateChangesClassParametersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
foreach (
[
'doctrine_mongodb.odm.connection.class' => Client::class,
'doctrine_mongodb.odm.configuration.class' => MongoDBConfiguration::class,
'doctrine_mongodb.odm.document_manager.class' => DocumentManager::class,
'doctrine_mongodb.odm.manager_configurator.class' => ManagerConfigurator::class,
'doctrine_mongodb.odm.event_manager.class' => ContainerAwareEventManager::class,
'doctrine_odm.mongodb.validator_initializer.class' => DoctrineInitializer::class,
'doctrine_odm.mongodb.validator.unique.class' => UniqueEntityValidator::class,
'doctrine_mongodb.odm.class' => ManagerRegistry::class,
'doctrine_mongodb.odm.security.user.provider.class' => EntityUserProvider::class,
'doctrine_mongodb.odm.proxy_cache_warmer.class' => ProxyCacheWarmer::class,
'doctrine_mongodb.odm.hydrator_cache_warmer.class' => HydratorCacheWarmer::class,
'doctrine_mongodb.odm.persistent_collection_cache_warmer.class' => PersistentCollectionCacheWarmer::class,
] as $parameter => $class
) {
if (! $container->hasParameter($parameter) || $container->getParameter($parameter) === $class) {
continue;
}

trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.7',
'"%s" parameter is deprecated, use a compiler pass to update the service instead.',
$parameter,
);
}
}
}
2 changes: 2 additions & 0 deletions DoctrineMongoDBBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DeprecateChangesClassParametersPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new DoctrineValidationPass('mongodb'));
$container->addCompilerPass(new ServiceRepositoryCompilerPass());
$container->addCompilerPass(new FixturesCompilerPass());
$container->addCompilerPass(new DeprecateChangesClassParametersPass());

if (! $container->hasExtension('security')) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Compiler;

use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DeprecateChangesClassParametersPass;
use PHPUnit\Framework\TestCase;
use stdClass;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class DeprecateChangesClassParametersPassTest extends TestCase
{
use ExpectDeprecationTrait;

/** @group legacy */
public function testChangeParameterClass(): void
{
$compilerPass = new DeprecateChangesClassParametersPass();

$container = new ContainerBuilder();
$container->setParameter('doctrine_mongodb.odm.connection.class', stdClass::class);

$this->expectDeprecation('Since doctrine/mongodb-odm-bundle 4.7: "doctrine_mongodb.odm.connection.class" parameter is deprecated, use a compiler pass to update the service instead.');

$compilerPass->process($container);
}
}

0 comments on commit 6e76da7

Please sign in to comment.