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/Component
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
..
Asset Merge branch '2.7' into 2.8 2015-08-24 09:18:02 +02:00
BrowserKit Merge branch '2.7' into 2.8 2015-09-06 10:37:06 +02:00
ClassLoader Merge branch '2.7' into 2.8 2015-08-27 08:53:13 +02:00
Config feature #15738 Implement service-based Resource (cache) validation (mpdude) 2015-09-25 08:33:07 +02:00
Console Merge branch '2.7' into 2.8 2015-09-22 15:49:41 +02:00
CssSelector Merge branch '2.7' into 2.8 2015-09-22 15:49:41 +02:00
Debug Merge branch '2.7' into 2.8 2015-09-14 16:15:17 +02:00
DependencyInjection Merge branch '2.7' into 2.8 2015-09-18 09:20:16 +02:00
DomCrawler Merge branch '2.7' into 2.8 2015-09-22 15:49:41 +02:00
EventDispatcher Merge branch '2.7' into 2.8 2015-09-22 15:49:41 +02:00
ExpressionLanguage Merge branch '2.7' into 2.8 2015-09-09 20:05:45 +02:00
Filesystem Merge branch '2.7' into 2.8 2015-09-09 20:05:45 +02:00
Finder feature #15805 [Finder] Deprecate adapters and related classes (nicolas-grekas) 2015-09-22 13:58:08 +02:00
Form minor #15798 [2.8][Form] Fix php warning on invalid FormFactory::createBuilder() argument (xelaris) 2015-09-22 18:06:19 +02:00
HttpFoundation Merge branch '2.7' into 2.8 2015-09-22 15:49:41 +02:00
HttpKernel feature #15738 Implement service-based Resource (cache) validation (mpdude) 2015-09-25 08:33:07 +02:00
Intl Merge branch '2.7' into 2.8 2015-09-09 20:05:45 +02:00
Locale Merge branch '2.7' into 2.8 2015-08-27 08:53:13 +02:00
OptionsResolver Merge branch '2.7' into 2.8 2015-08-24 09:18:02 +02:00
Process Merge branch '2.7' into 2.8 2015-09-19 21:59:50 +02:00
PropertyAccess Merge branch '2.7' into 2.8 2015-08-24 09:18:02 +02:00
Routing Merge branch '2.7' into 2.8 2015-09-14 16:15:17 +02:00
Security feature #14673 New Guard Authentication System (e.g. putting the joy back into security) (weaverryan) 2015-09-24 09:21:03 +02:00
Serializer Merge branch '2.7' into 2.8 2015-09-01 09:33:03 +02:00
Stopwatch Merge branch '2.7' into 2.8 2015-09-22 15:49:41 +02:00
Templating Merge branch '2.7' into 2.8 2015-08-31 14:33:08 +02:00
Translation feature #15738 Implement service-based Resource (cache) validation (mpdude) 2015-09-25 08:33:07 +02:00
Validator Merge branch '2.7' into 2.8 2015-09-24 11:01:09 +02:00
VarDumper [VarDumper] Add EnumStub for dumping virtual collections with casters 2015-09-20 23:07:38 +02:00
Yaml Merge branch '2.7' into 2.8 2015-09-14 16:15:17 +02:00