8000 Introduce MetadataValidators to test arbitrary metadata for freshness. by bnw · Pull Request #15692 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Introduce MetadataValidators to test arbitrary metadata for freshness. #15692

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

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
60b4089
Introduce MetadataValidators to test arbitrary metadata for freshness.
bnw Sep 3, 2015
673070b
Added trailing newline
bnw Sep 4, 2015
ab39129
Added new parameter to interface
bnw Sep 4, 2015
9546be9
Renamed ResourceValidator to ResourceInterfaceValidator
bnw Sep 4, 2015
0342b73
Added missing import
bnw Sep 4, 2015
7aee891
Correcting classname after renaming
bnw Sep 4, 2015
df526c0
Added ConfigCachePass to framework
bnw Sep 4, 2015
2555d5a
Fixed UnitTests
bnw Sep 4, 2015
4145dc0
Bugfix
bnw Sep 4, 2015
2e49fb2< 8000 /code>
Better name
bnw Sep 4, 2015
d0f1e2d
Fix test
mpdude Sep 5, 2015
3f9424f
Add docblock
mpdude Sep 5, 2015
b8bb60a
Fix tests while keeping ConfigCacheFactory unchanged (from a client p…
mpdude Sep 5, 2015
2fac8f3
Hello Fabbot
mpdude Sep 5, 2015
1b62130
Forgot import statement
mpdude Sep 5, 2015
b16d9de
The new ConfigCachePass is required
mpdude Sep 6, 2015
97a2cd0
Bump deps
mpdude Sep 6, 2015
aae0a78
Avoid changing the interface
mpdude Sep 6, 2015
024aa8c
Also validate this method in the interface
mpdude Sep 7, 2015
d617943
Rename ResourceInterfaceValidator to ResourceValidator as suggested o…
mpdude Sep 7, 2015
8907a21
Oh Fabbot
mpdude Sep 7, 2015
e471890
Add a note why the ResourceInterface::__toString method is important …
mpdude Sep 7, 2015
e9fd197
Remove totally broken test
mpdude Sep 7, 2015
53ad659
Rewrite ConfigCacheTest because it made many assumptions about how th…
mpdude Sep 7, 2015
594529c
Fix ConfigCache and improve docblock
mpdude Sep 7, 2015
32426ce
If I had to pay just $0.02 for every Fabbot complaint, Fabien would b…
mpdude Sep 7, 2015
f3470e3
Document deprecation in the CHANGELOG file
mpdude Sep 7, 2015
7739878
Some more tweaks to the ConfigCacheTest
mpdude Sep 7, 2015
f9c48a0
Remove unused import
mpdude Sep 7, 2015
c0f416d
Continue with next resource, not next validator
mpdude Sep 8, 2015
00b4efb
Add a shortcut for when we don't have any validators (might be in prod)
mpdude Sep 8, 2015
23a4464
Create a new ValidatorConfigCache class and pass metadata validators …
mpdude Sep 8, 2015
caf6bf1
Fabbot
mpdude Sep 8, 2015
37c96e1
This is not relevant for this PR
mpdude Sep 8, 2015
7bc5ba2
Tweaks as suggested on GH
mpdude Sep 8, 2015
f5add4c
Implement the plan(TM)
mpdude Sep 9, 2015
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
Prev Previous commit
Next Next commit
Create a new ValidatorConfigCache class and pass metadata validators …
…in the constructor.

That way, we can keep the isFresh() method and get away without deprecations.
  • Loading branch information
mpdude committed Sep 8, 2015
commit 23a4464fd059b56c48c7495a1e2c6f98e1de5ac6
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass(new RoutingResolverPass());
$container->addCompilerPass(new ProfilerPass());
$container->addCompilerPass(new ConfigCachePass());
// must be registered before removing private services as some might be listeners/subscribers
// but as late as possible to get resolved parameters
$container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
Expand All @@ -94,6 +93,7 @@ public function build(ContainerBuilder $container)
if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new ConfigCachePass());
}
}
}
11 changes: 0 additions & 11 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
CHANGELOG
=========

2.8.0
-----

* deprecated the `ConfigCache::isFresh()` method. Use the new `isValid()` method instead and
pass the `MetadataValidator` instances that shall be used to validate the cache. A single
`ResourceValidator` will do and perform the same validations (using `ResourceInterface`)
as `isFresh()` previously did.

Note that an even better approach would be to use the `ConfigCacheFactory`; it will
perform the freshness check for you and runs a callback if the cache needs to be updated.

2.7.0
-----

Expand Down
124 changes: 9 additions & 115 deletions src/Symfony/Component/Config/ConfigCache.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@
namespace Symfony\Component\Config;

use Symfony\Component\Config\Resource\ResourceValidator;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;

/**
* ConfigCache caches arbitrary content in files on disk.
*
* Metadata can be stored alongside the cache and can later be
* used by MetadataValidators to check if the cache is still fresh.
* When in debug mode, those metadata resources that implement
* \Symfony\Component\Config\Resource\ResourceInterface will
* be used to check cache freshness.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Matthias Pigulla <mp@webfactory.de>
*/
class ConfigCache implements ConfigCacheInterface
class ConfigCache extends ValidatorConfigCache
{
private $debug;
private $file;

/**
* @param string $file The absolute cache path
* @param bool $debug Whether debugging is enabled or not
*/
public function __construct($file, $debug)
{
$this->file = $file;
parent::__construct($file, array(new ResourceValidator()));
$this->debug = (bool) $debug;
}

Expand All @@ -50,127 +48,23 @@ public function __toString()
{
@trigger_error('ConfigCache::__toString() is deprecated since version 2.7 and will be removed in 3.0. Use the getPath() method instead.', E_USER_DEPRECATED);

return $this->file;
}

/**
* Gets the cache file path.
*
* @return string The cache file path
*/
public function getPath()
{
return $this->file;
return $this->getPath();
}

/**
* Checks if the cache is still fresh.
*
* This method always returns true when debug is off and the
* This implementation always returns true when debug is off and the
* cache file exists.
*
* @return bool true if the cache is fresh, false otherwise
*
* @deprecated since version 2.8, to be removed in 3.0.
*/
public function isFresh()
{
@trigger_error(__NAMESPACE__.'\ConfigCache::isFresh() is deprecated since version 2.8 and will be removed in 3.0. Use the isValid() method instead and pass the appropriate MetadataValidators, or even better use a \Symfony\Component\Config\ConfigCacheFactoryInterface implementation to create and validate the cache.', E_USER_DEPRECATED);

if (!$this->debug && is_file($this->file)) {
if (!$this->debug && is_file($this->getPath())) {
return true;
}

return $this->isValid(array(new ResourceValidator()));
}

/**
* Use MetadataValidators to check if the cache is still valid.
*
* The first MetadataValidator that supports a given resource is considered authoritative.
* Resources with no matching MetadataValidators will silently be ignored and considered fresh.
*
* This method <em>does not</em> take the debug flag into consideration: Whether or not a cache
* should be checked in production mode and/or which validators need to be applied is a decision
* left to the client of this method.
*
* @param MetadataValidatorInterface[] $validators List of validators the metadata is checked against.
* The first validator that supports a resource is considered authoritative.
*
* @return bool True if all supported resources and valid, false otherwise
*/
public function isValid(array $validators = null)
{
if (!is_file($this->file)) {
return false;
}

if (!$validators) {
return true; // shortcut - if we don't have any validators we don't need to bother with the meta file at all
}

$metadata = $this->getMetaFile();
if (!is_file($metadata)) {
return true;
}

$time = filemtime($this->file);
$meta = unserialize(file_get_contents($metadata));

foreach ($meta as $resource) {
foreach ($validators as $validator) {
if (!$validator->supports($resource)) {
continue; // next validator
}
if ($validator->isFresh($resource, $time)) {
break; // no need to further check this resource
} else {
return false; // cache is stale
}
}
// no suitable validator found, ignore this resource
}

return true;
}

/**
* Writes cache.
*
* @param string $content The content to write in the cache
* @param array $metadata An array of metadata
*
* @throws \RuntimeException When cache file can't be written
*/
public function write($content, array $metadata = null)
{
$mode = 0666;
$umask = umask();
$filesystem = new Filesystem();
$filesystem->dumpFile($this->file, $content, null);
try {
$filesystem->chmod($this->file, $mode, $umask);
} catch (IOException $e) {
// discard chmod failure (some filesystem may not support it)
}

if (null !== $metadata) {
$filesystem->dumpFile($this->getMetaFile(), serialize($metadata), null);
try {
$filesystem->chmod($this->getMetaFile(), $mode, $umask);
} catch (IOException $e) {
// discard chmod failure (some filesystem may not support it)
}
}
}

/**
* Gets the meta file path.
*
* @return string The meta file path
*/
private function getMetaFile()
{
return $this->file.'.meta';
return parent::isFresh();
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/ConfigCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function cache($file, $callback)
}

$cache = new ConfigCache($file, $this->debug);
if (!$cache->isValid(array(new ResourceValidator()))) {
if (!$cache->isFresh()) {
call_user_func($callback, $cache);
}

Expand Down
4 changes: 0 additions & 4 deletions src/Symfony/Component/Config/ConfigCacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function getPath();
* This check should take the metadata passed to the write() method into consideration.
*
* @return bool Whether the cache is still fresh.
*
* @deprecated since 2.8, to be removed in 3.0.
* Use Symfony\Component\Config\ConfigCacheFactory instead, it will take care of
* validating the cache.
*/
public function isFresh();

Expand Down
Loading
0