-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Implement service-based Resource (cache) validation #15738
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
Changes from 1 commit
535131d
64b9224
6d81c69
ab13ef4
d1cb1fd
a845b3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Currently, any metadata passed to ConfigCache (namely implementations of ResourceInterface) is serialized to disk. When the ConfigCache is validated, the metadata is unserialized and queried through ResourceInterface::isFresh() to determine whether the caceh is fresh. That way, ResourceInterface implementations cannot interact with services, for example a database connection. This PR introduces the new concept of ResourceCheckers. Services implementing ResourceCheckerInterface can be tagged as "config_cache.resource_checker" with an optional priority. Clients that wish to use ConfigCache can then obtain an instance from the config_cache_factory service (which implements ConfigCacheFactoryInterface). The factory will take care of injecting resource checkers into the ConfigCache instance so that they can be used for cache validation. Checking cache metadata is easy for ResourceCheckers: - First, the ResourceCheckerInterface::supports() implementation is passed the metadata object in question. If the checker cannot handle the type of resource passed, support() should return false. - Otherwise, the ResourceCheckerInterface::isFresh() method will be called and given the resource as well as the timestamp at which the cache was initialized. If that method returns false, the cache is considered stale. If it returns true, the resource is considered valid and will *not* be passed to any additional checkers. BC and migration path: This PR does not (intend to) break BC but it comes with deprecations. The main reason is that ResourceInterface contains an isFresh() method that does not make sense in the general case of resources. Thus, ResourceInterface::isFresh() is marked as deprecated and shall be removed in Symfony 3.0. Resource implementations that can (or wish to) be validated in that simple manner can implement the SelfCheckingResourceInterface sub-interface that still contains (and will keep) the isFresh() method. The change should be as simple as changing the "extends" list. Apart from that, ResourceInterface will be kept as the base interface for resource implementations. Note that ResourceInterface is used in several @api interfaces and thus cannot easily be substituted. For the Symfony 2.x series, a BCResourceInterfaceChecker will be kept that performs validation through ResourceInterface::isFresh() but will trigger a deprecation warning. The remedy is to either implement a custom ResourceChecker with a priority higher than -1000; or to switch to the aforementioned SelfCheckingResourceInterface which is used at a priority of -990 (without deprecation warning). The ConfigCache and ConfigCacheFactory classes can be used as previously but do not feature checker-based cache validation. Outlook and closing remarks: This PR supersedes #7230, #15692 and works at least in parts towards the goal of #7176. The ResourceCheckerInterface, ...ConfigCache and ...ConfigCacheFactory no longer need to be aware of the "debug" flag. The different validation rules applied previously are now just a matter of ResourceChecker configuration (i. e. "no checkers" in prod). It might be possible to remove the "debug" flag from Symfony's Router and/or Translator classes in the future as well because it was only passed on to the ConfigCache there.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Adds services tagged config_cache.resource_checker to the config_cache_factory service, ordering them by priority. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
* @author Benjamin Klotz <bk@webfactory.de> | ||
*/ | ||
class ConfigCachePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$resourceCheckers = array(); | ||
foreach ($container->findTaggedServiceIds('config_cache.resource_checker') as $id => $attributes) { | ||
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; | ||
$resourceCheckers[$priority][] = new Reference($id); | ||
} | ||
|
||
// sort by priority and flatten | ||
krsort($resourceCheckers); | ||
$resourceCheckers = call_user_func_array('array_merge', $resourceCheckers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break in case there is no resource checker (array_merge requires at least 1 argument) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed (a few lines above) |
||
|
||
$container->getDefinition('config_cache_factory')->addMethodCall('setResourceCheckers', array($resourceCheckers)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
<parameter key="file_locator.class">Symfony\Component\HttpKernel\Config\FileLocator</parameter> | ||
<parameter key="uri_signer.class">Symfony\Component\HttpKernel\UriSigner</parameter> | ||
<parameter key="request_stack.class">Symfony\Component\HttpFoundation\RequestStack</parameter> | ||
<parameter key="config_cache_factory.class">Symfony\Component\Config\ResourceCheckerConfigCacheFactory</parameter> | ||
<parameter key="config_cache_default_resource_checker.class">Symfony\Component\Config\Resource\DefaultResourceChecker</parameter> | ||
<parameter key="config_cache_bc_resource_interface_checker.class">Symfony\Component\Config\Resource\BCResourceInterfaceChecker</parameter> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please don't introduce new class parameters. such practice is deprecated |
||
</parameters> | ||
|
||
<services> | ||
|
@@ -63,5 +66,19 @@ | |
<service id="uri_signer" class="%uri_signer.class%"> | ||
<argument>%kernel.secret%</argument> | ||
</service> | ||
|
||
<service id="config_cache_factory" class="%config_cache_factory.class%" /> | ||
|
||
<service id="config_cache_default_resource_checker" class="%config_cache_default_resource_checker.class%"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be a private service (all checkers should be) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and I would not use top-level ids for such inner services |
||
<tag name="config_cache.resource_checker" priority="-990" /> | ||
</service> | ||
|
||
<!-- | ||
This service is deprecated and will be removed in 3.0. | ||
--> | ||
<service id="config_cache_bc_resource_interface_checker" class="%config_cache_bc_resource_interface_checker.class%"> | ||
<tag name="config_cache.resource_checker" priority="-1000" /> | ||
</service> | ||
|
||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass; | ||
|
||
class ConfigCachePassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testThatCheckersAreProcessedInPriorityOrder() | ||
{ | ||
$services = array( | ||
'checker_2' => array(0 => array('priority' => 100)), | ||
'checker_1' => array(0 => array('priority' => 200)), | ||
'checker_3' => array(), | ||
); | ||
|
||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); | ||
$container = $this->getMock( | ||
'Symfony\Component\DependencyInjection\ContainerBuilder', | ||
array('findTaggedServiceIds', 'getDefinition', 'hasDefinition') | ||
); | ||
|
||
$container->expects($this->atLeastOnce()) | ||
->method('findTaggedServiceIds') | ||
->will($this->returnValue($services)); | ||
$container->expects($this->atLeastOnce()) | ||
->method('getDefinition') | ||
->with('config_cache_factory') | ||
->will($this->returnValue($definition)); | ||
|
||
$definition->expects($this->once()) | ||
->method('addMethodCall') | ||
->with('setResourceCheckers', array( | ||
array( | ||
new Reference('checker_1'), | ||
new Reference('checker_2'), | ||
new Reference('checker_3'), | ||
), | ||
)); | ||
|
||
$pass = new ConfigCachePass(); | ||
$pass->process($container); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,34 +104,6 @@ public function testTransWithCachingWithInvalidLocale() | |
$translator->trans('foo'); | ||
} | ||
|
||
public function testLoadResourcesWithCaching() | ||
{ | ||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); | ||
$resourceFiles = array( | ||
'fr' => array( | ||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml', | ||
), | ||
); | ||
|
||
// prime the cache | ||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles), 'yml'); | ||
$translator->setLocale('fr'); | ||
|
||
$this->assertEquals('répertoire', $translator->trans('folder')); | ||
|
||
// do it another time as the cache is primed now | ||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), 'yml'); | ||
$translator->setLocale('fr'); | ||
|
||
$this->assertEquals('répertoire', $translator->trans('folder')); | ||
|
||
// refresh cache when resources is changed in debug mode. | ||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir, 'debug' => true), 'yml'); | ||
$translator->setLocale('fr'); | ||
|
||
$this->assertEquals('folder', $translator->trans('folder')); | ||
} | ||
|
||
public function testLoadResourcesWithoutCaching() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason why this test fails ? $translator->setConfigCacheFactory(new \Symfony\Component\Config\ResourceCheckerConfigCacheFactory([
new \Symfony\Component\Config\Resource\SelfCheckingResourceChecker(),
new \Symfony\Component\Config\Resource\BCResourceInterfaceChecker()
])); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know, but maybe you want to use the default ConfigCacheFactory in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used |
||
{ | ||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rename this variable.
$attributes
is actually an array of tags here, not of attributes