-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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 all commits
535131d
64b9224
6d81c69
ab13ef4
d1cb1fd
a845b3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
* @author Benjamin Klotz <[email protected]> | ||
*/ | ||
class ConfigCachePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$resourceCheckers = array(); | ||
|
||
foreach ($container->findTaggedServiceIds('config_cache.resource_checker') as $id => $tags) { | ||
$priority = isset($tags[0]['priority']) ? $tags[0]['priority'] : 0; | ||
$resourceCheckers[$priority][] = new Reference($id); | ||
} | ||
|
||
if (empty($resourceCheckers)) { | ||
return; | ||
} | ||
|
||
// sort by priority and flatten | ||
krsort($resourceCheckers); | ||
$resourceCheckers = call_user_func_array('array_merge', $resourceCheckers); | ||
|
||
$container->getDefinition('config_cache_factory')->replaceArgument(0, $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. Shouldn't this 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. Well, that service is defined and not an alias. Do you think it should be one? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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('replaceArgument') | ||
->with(0, array( | ||
new Reference('checker_1'), | ||
new Reference('checker_2'), | ||
new Reference('checker_3'), | ||
)); | ||
|
||
$pass = new ConfigCachePass(); | ||
$pass->process($container); | ||
} | ||
|
||
public function testThatCheckersCanBeMissing() | ||
{ | ||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); | ||
$container = $this->getMock( | ||
'Symfony\Component\DependencyInjection\ContainerBuilder', | ||
array('findTaggedServiceIds') | ||
); | ||
|
||
$container->expects($this->atLeastOnce()) | ||
->method('findTaggedServiceIds') | ||
->will($this->returnValue(array())); | ||
|
||
$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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed (a few lines above)