This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle
Fabien Potencier d60428c9ca 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
2015-09-25 08:33:07 +02:00
..
CacheWarmer FIX #13919 added TranslationsCacheWarmer to generate catalogues at warmup 2015-03-31 14:07:52 +02:00
Command Merge branch '2.7' into 2.8 2015-09-24 11:01:09 +02:00
Console 2.8 changes from #15733 2015-09-09 20:09:35 +02:00
Controller fixed CS 2015-09-14 08:46:24 +02:00
DataCollector [WebProfilerBundle] Show AJAX requests in the symfony profiler toolbar 2014-09-23 17:52:02 +02:00
DependencyInjection Implement service-based Resource (cache) validation 2015-09-25 08:31:17 +02:00
EventListener [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00
Fragment Silence invasive deprecation warnings, opt-in for warnings 2015-06-08 10:37:21 +01:00
HttpCache Silence invasive deprecation warnings, opt-in for warnings 2015-06-08 10:37:21 +01:00
Resources Implement service-based Resource (cache) validation 2015-09-25 08:31:17 +02:00
Routing Merge branch '2.6' into 2.7 2015-04-18 17:11:06 +02:00
Templating Merge branch '2.3' into 2.7 2015-08-26 19:56:37 +02:00
Test fix missing links to https://symfony.com 2015-05-13 13:33:56 +02:00
Tests feature #15738 Implement service-based Resource (cache) validation (mpdude) 2015-09-25 08:33:07 +02:00
Translation [Translator][warmup][fallback locales] fixed missing cache file generation. 2015-09-09 14:57:38 +00:00
Validator [Validator] Removed legacy validator classes 2014-08-06 15:23:29 +02:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
CHANGELOG.md FIX #13919 added TranslationsCacheWarmer to generate catalogues at warmup 2015-03-31 14:07:52 +02:00
Client.php tweaked phpdocs 2015-03-23 09:23:11 +01:00
composer.json feature #15738 Implement service-based Resource (cache) validation (mpdude) 2015-09-25 08:33:07 +02:00
FrameworkBundle.php Implement service-based Resource (cache) validation 2015-09-25 08:31:17 +02:00
phpunit.xml.dist [2.3] require-dev PHPUnit bridge 2015-02-24 11:24:26 +01:00