Skip to content

Commit a919e7d

Browse files
committed
feature #15738 Implement service-based Resource (cache) validation (mpdude)
This PR was squashed before being merged into the 2.8 branch (closes #15738). Discussion ---------- Implement service-based Resource (cache) validation | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #7230, #15692, #7782 | License | MIT | Doc PR | symfony/symfony-docs#5136 ### Overview 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 cache 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, `supports()` 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 unchanged 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 should 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. It 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. Commits ------- 20d3722 Implement service-based Resource (cache) validation
2 parents 7707bb8 + f03340c commit a919e7d

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Tests/TranslatorCacheTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Translation\Tests;
1313

14-
use Symfony\Component\Config\Resource\ResourceInterface;
14+
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
1515
use Symfony\Component\Translation\Loader\ArrayLoader;
1616
use Symfony\Component\Translation\Loader\LoaderInterface;
1717
use Symfony\Component\Translation\Translator;
@@ -228,7 +228,7 @@ public function testPrimaryAndFallbackCataloguesContainTheSameMessagesRegardless
228228

229229
public function testRefreshCacheWhenResourcesAreNoLongerFresh()
230230
{
231-
$resource = $this->getMock('Symfony\Component\Config\Resource\ResourceInterface');
231+
$resource = $this->getMock('Symfony\Component\Config\Resource\SelfCheckingResourceInterface');
232232
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
233233
$resource->method('isFresh')->will($this->returnValue(false));
234234
$loader
@@ -281,7 +281,7 @@ private function createFailingLoader()
281281
}
282282
}
283283

284-
class StaleResource implements ResourceInterface
284+
class StaleResource implements SelfCheckingResourceInterface
285285
{
286286
public function isFresh($timestamp)
287287
{

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"require-dev": {
2222
"symfony/phpunit-bridge": "~2.7|~3.0.0",
23-
"symfony/config": "~2.7",
23+
"symfony/config": "~2.8",
2424
"symfony/intl": "~2.4|~3.0.0",
2525
"symfony/yaml": "~2.2|~3.0.0",
2626
"psr/log": "~1.0"

0 commit comments

Comments
 (0)