Skip to content

Commit

Permalink
Optimize cache decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
ossinkine committed Aug 31, 2020
1 parent 9549cd1 commit b66fa48
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions DependencyInjection/Compiler/PhpArrayCachePass.php
@@ -0,0 +1,44 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;

use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Removes redundant decorators such DoctrineAdapter -> DoctrineProvider -> AdapterInterface.
*/
class PhpArrayCachePass implements CompilerPassInterface
{
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('doctrine.orm.metadata_cache') as $serviceId => $tags) {
$definition = $container->getDefinition($serviceId);
$definition->clearTag('doctrine.orm.metadata_cache');

$phpArrayAdapterDefinition = $definition->getArgument(0);
$doctrineAdapterDefinition = $phpArrayAdapterDefinition->getArgument(1);
$decoratedServiceReference = $doctrineAdapterDefinition->getArgument(0);

$decoratedServiceDefinition = $container->getDefinition($decoratedServiceReference);
if (is_a($decoratedServiceDefinition->getClass(), DoctrineProvider::class, true)) {
$decoratedServiceReference = $decoratedServiceDefinition->getArgument(0);
$reference = $decoratedServiceReference;

$decoratedServiceDefinition = $container->getDefinition($decoratedServiceReference);
if (!is_a($decoratedServiceDefinition->getClass(), AdapterInterface::class, true)) {
$reference = new Definition(ProxyAdapter::class, [$reference]);
}

$phpArrayAdapterDefinition->replaceArgument(1, $reference);
}
}
}
}
1 change: 1 addition & 0 deletions DependencyInjection/DoctrineExtension.php
Expand Up @@ -927,6 +927,7 @@ private function registerMetadataPhpArrayCache(string $entityManagerName, Contai
$serviceId = $serviceId.'.php_array';
$doctrineCacheDefinition = $container->register($serviceId, DoctrineProvider::class);
$doctrineCacheDefinition->addArgument($phpArrayAdapterDefinition);
$doctrineCacheDefinition->addTag('doctrine.orm.metadata_cache');
$container->setAlias($aliasId, $serviceId);
}

Expand Down
2 changes: 2 additions & 0 deletions DoctrineBundle.php
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\PhpArrayCachePass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
use Doctrine\Common\Util\ClassUtils;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new WellKnownSchemaFilterPass());
$container->addCompilerPass(new DbalSchemaFilterPass());
$container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
$container->addCompilerPass(new PhpArrayCachePass(), PassConfig::TYPE_OPTIMIZE);
}

/**
Expand Down

0 comments on commit b66fa48

Please sign in to comment.