Skip to content
Closed
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
57 changes: 57 additions & 0 deletions DependencyInjection/Compiler/RemoveClassesFromCachePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Compiler pass to remove some classes from Symfony's class cache to prevent
* from redeclaration in early cache lookup phase
* (see https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/issues/242).
*/
class RemoveClassesFromCachePass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('fos_http_cache.invalidation.enabled')) {
return;
}

$frameworkExt = $container->getExtension('framework');
$classes = $frameworkExt->getClassesToCompile();

// remove classes
$classesToRemove = array(
'Symfony\\Component\\EventDispatcher\\Event',
'Symfony\\Component\\HttpKernel\\Event\\KernelEvent',
'Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent',
'Symfony\\Component\\HttpKernel\\Event\\FilterResponseEvent',
'Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent',
'Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent',
'Symfony\\Component\\HttpKernel\\Event\\GetResponseForExceptionEvent',
Copy link
Contributor

Choose a reason for hiding this comment

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

if the class cache is relevant, this might be an expensive thing. how do other bundles that have events solve this issue?

Copy link
Member

Choose a reason for hiding this comment

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

@dbu the issue of this bundle is that it has an event before reaching the AppKernel (in its AppCache). Other bundles don't

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, thanks. yeah now i get it.

);
foreach ($classesToRemove as $className) {
$index = array_search($className, $classes);
array_splice($classes, $index, 1);
}

// overwrite array for class cache via reflection (no other way)
$reflClass = new \ReflectionClass('Symfony\\Component\\HttpKernel\\DependencyInjection\\Extension');
$reflProp = $reflClass->getProperty('classes');
$reflProp->setAccessible(true);
$reflProp->setValue($frameworkExt, $classes);
}
}
1 change: 1 addition & 0 deletions DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function load(array $configs, ContainerBuilder $container)
if (!empty($config['invalidation']['rules'])) {
$this->loadInvalidatorRules($container, $config['invalidation']['rules']);
}
$container->setParameter($this->getAlias().'.invalidation.enabled', true);
}

if ($config['user_context']['enabled']) {
Expand Down
2 changes: 2 additions & 0 deletions FOSHttpCacheBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use FOS\HttpCacheBundle\DependencyInjection\Compiler\SecurityContextPass;
use FOS\HttpCacheBundle\DependencyInjection\Compiler\TagSubscriberPass;
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
use FOS\HttpCacheBundle\DependencyInjection\Compiler\RemoveClassesFromCachePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -29,5 +30,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new SecurityContextPass());
$container->addCompilerPass(new TagSubscriberPass());
$container->addCompilerPass(new HashGeneratorPass());
$container->addCompilerPass(new RemoveClassesFromCachePass());
}
}