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
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
Avoid changing the interface
  • Loading branch information
mpdude committed Sep 6, 2015
commit aae0a78e8776c2cbd60bc534549a390010640a2d
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture\TestAppKernel;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\ConfigCacheFactory;
use Symfony\Component\Config\ConfigCacheInterface;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Config\Resource\ResourceInterfaceValidator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -48,15 +48,13 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
$metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta');
// simply check that cache is warmed up
$this->assertGreaterThanOrEqual(1, count($metaFiles));
$configCacheFactory = new ConfigCacheFactory(true);
$that = $this;

foreach ($metaFiles as $file) {
$configCache = new ConfigCache(substr($file, 0, -5), true);
$this->assertTrue(
$configCache->isFresh(array(new ResourceInterfaceValidator())),
sprintf(
'Meta file "%s" is not fresh',
(string) $file
)
);
$configCacheFactory->cache(substr($file, 0, -5), function () use ($that, $file) {
$that->fail(sprintf('Meta file "%s" is not fresh', (string) $file));
});
}

// check that app kernel file present in meta file of container's cache
Expand Down
39 changes: 26 additions & 13 deletions src/Symfony/Component/Config/ConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
use Symfony\Component\Filesystem\Filesystem;

/**
* ConfigCache manages PHP cache files.
* ConfigCache caches arbitrary content in files on disk.
*
* When debug is enabled, it knows when to flush the cache
* thanks to an array of ResourceInterface instances.
* Metadata can be stored alongside the cache and can later be
* used by MetadataValidators to check if the cache is still fresh.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Matthias Pigulla <mp@webfactory.de>
*/
class ConfigCache implements ConfigCacheInterface
{
Expand Down Expand Up @@ -68,25 +69,37 @@ public function getPath()
* This method always returns true when debug is off and the
* cache file exists.
*
* @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 the cache is fresh, false otherwise
*
* @deprecated since version 2.8, to be removed in 3.0.
*/
public function isFresh(array $validators = null)
public function isFresh()
{
if (null === $validators) {
@trigger_error('ConfigCache::isFresh requires the $validators parameter as of version 2.8.', E_USER_DEPRECATED);
$validators = array(new ResourceInterfaceValidator());
@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)) {
return true;
}

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

/**
* 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.
*
* @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)
Copy link
Member

Choose a reason for hiding this comment

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

passing validators to isValid looks weird to me. Validators should be passed in the constructor IMO (which means we can also keep isFresh btw)

Copy link
Contributor

Choose a reason for hiding this comment

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

I was able to move the validators to the constructor of a new ValidatorConfigCache class. That way, we can keep the interface without deprecations.

ConfigCache is a subclass and behaves as previously, switching between dev and prod mode and validating ResourceInterface instances only.

{
if (!is_file($this->file)) {
return false;
}

if (!$this->debug) {
return true;
}

$metadata = $this->getMetaFile();
if (!is_file($metadata)) {
return false;
Expand Down
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->isFresh(array(new ResourceInterfaceValidator()))) {
if (!$cache->isValid(array(new ResourceInterfaceValidator()))) {
call_user_func($callback, $cache);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Config/ConfigCacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public function getPath();
*
* This check should take the metadata passed to the write() method into consideration.
*
* @param MetadataValidatorInterface[] $validators List of validators the metadata is checked against.
*
* @return bool Whether the cache is still fresh.
*/
public function isFresh(array $validators = null);
public function isFresh();

/**
* Writes the given content into the cache file. Metadata will be stored
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Config/MetadataValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface MetadataValidatorInterface
* Queries the MetadataValidator whether it can validate a given
* resource or not.
*
* @param $metadata The resource to be checked for freshness
* @param object $metadata The resource to be checked for freshness
*
* @return bool True if the MetadataValidator can handle this resource type, false if not
*/
Expand All @@ -36,8 +36,8 @@ public function supports($metadata);
/**
* Validates the resource.
*
* @param $metadata The resource to be validated.
* @param $timestamp The timestamp at which the cache associated with this resource was created.
* @param object $metadata The resource to be validated.
* @param int $timestamp The timestamp at which the cache associated with this resource was created.
*
* @return bool True if the resource has not changed since the given timestamp, false otherwise.
*/
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Config/Tests/ConfigCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testCacheIsNotFreshIfFileDoesNotExist()

$cache = new ConfigCache($this->cacheFile, false);

$this->assertFalse($this->callIsFreshWithResourceValidator($cache));
$this->assertFalse($this->isCacheValid($cache));
}

public function testCacheIsAlwaysFreshIfFileExistsWithDebugDisabled()
Expand All @@ -66,7 +66,7 @@ public function testCacheIsAlwaysFreshIfFileExistsWithDebugDisabled()

$cache = new ConfigCache($this->cacheFile, false);

$this->assertTrue($this->callIsFreshWithResourceValidator($cache));
$this->assertTrue($this->isCacheValid($cache));
}

public function testCacheIsNotFreshWithoutMetaFile()
Expand All @@ -75,14 +75,14 @@ public function testCacheIsNotFreshWithoutMetaFile()

$cache = new ConfigCache($this->cacheFile, true);

$this->assertFalse($this->callIsFreshWithResourceValidator($cache));
$this->assertFalse($this->isCacheValid($cache));
}

public function testCacheIsFreshIfResourceIsFresh()
{
$cache = new ConfigCache($this->cacheFile, true);

$this->assertTrue($this->callIsFreshWithResourceValidator($cache));
$this->assertTrue($this->isCacheValid($cache));
}

public function testCacheIsNotFreshIfOneOfTheResourcesIsNotFresh()
Expand All @@ -91,7 +91,7 @@ public function testCacheIsNotFreshIfOneOfTheResourcesIsNotFresh()

$cache = new ConfigCache($this->cacheFile, true);

$this->assertFalse($this->callIsFreshWithResourceValidator($cache));
$this->assertFalse($this->isCacheValid($cache));
}

public function testWriteDumpsFile()
Expand Down Expand Up @@ -122,9 +122,9 @@ public function testWriteDumpsMetaFileWithDebugEnabled()
$this->assertSame(serialize($metadata), file_get_contents($this->metaFile));
}

private function callIsFreshWithResourceValidator(ConfigCache $cache)
private function isCacheValid(ConfigCache $cache)
{
return $cache->isFresh(array(new ResourceInterfaceValidator()));
return $cache->isValid(array(new ResourceInterfaceValidator()));
}

private function makeCacheFresh()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function cache($file, $callback)
}

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

Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,12 @@ protected function initializeContainer()
$class = $this->getContainerClass();
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
$fresh = true;
if (!$cache->isFresh(array(new ResourceInterfaceValidator()))) {

/* We probably should use the ConfigCacheFactory here, but that would require
* some wrapper methods because we cannot call protected methods from closures
* prior to PHP 5.4. Let's do this change later when we can require PHP 5.4.
*/
if (!$cache->isValid(array(new ResourceInterfaceValidator()))) {
$container = $this->buildContainer();
$container->compile();
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy