Skip to content

[Config] Delegate creation of ConfigCache instances to a factory. #14178

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 24 commits into from
Closed

[Config] Delegate creation of ConfigCache instances to a factory. #14178

wants to merge 24 commits into from

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Apr 2, 2015

Q A
Bug fix? no
New feature? yes (refactoring, new flex point)
BC breaks? no
Deprecations? yes
Tests pass? we'll see :-)
Fixed tickets n/a
License MIT
Doc PR symfony/symfony-docs#5136

In the Routing/Router and Translation/Translator, delegate creation of ConfigCache instances to a factory. The factory can be setter-injected but will default to a BC implementation.

The ConfigCacheFactoryInterface is designed in a way that captures the common $cache = new ...; if (!$cache->isFresh()) { ... do sth } pattern. But more importantly, this design allows factory implementations to take additional measures to avoid race conditions before actually filling the cache.

By using an exchangeable ConfigCache factory it becomes possible to implement different resource (freshness) checking strategies, especially service-based ones.

The goal is to be able to validate Translators and Routers generated by database-based loaders. It might also help with symfony/AsseticBundle#168. This PR only contains the minimum changes needed, so the rest could be implemented in a bundle outside the core (at least for the beginning).

Component/HttpKernel/Kernel::initializeContainer still uses the ConfigCache implementation directly as there is no sensible way of getting/injecting a factory service (chicken-egg).

This is a pick off #7230. It replaces #7781 which was against the master branch. Also see #7781 for additional comments/explanations.

Todo

  • Allow symfony/config ~3.0.0 in composer.json for the HttpKernel and Translator component as well as TwigBundle once this PR has been merged into the master branch (fail deps=high tests for the time being).

@@ -44,6 +44,7 @@ public function __construct($file, $debug)
* Gets the cache file path.
*
* @return string The cache file path
* @deprecated since 2.7, to be removed in 3.0. Use getFilePath() instead.
Copy link
Member

Choose a reason for hiding this comment

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

GetFilePath or getPath (see the method name below)?

@mpdude
Copy link
Contributor Author

mpdude commented Apr 2, 2015

@fabpot Would it make sense to explicitly inject the (default) factory via the DIC in FrameworkBundle, so the factory is there as a service and can be changed easily?

That would be easier than having to change the router/translator service definitions.

@mpdude mpdude changed the title Delegate creation of ConfigCache instances to a factory. [Config][Routing][Translation] Delegate creation of ConfigCache instances to a factory. Apr 2, 2015
@mpdude mpdude changed the title [Config][Routing][Translation] Delegate creation of ConfigCache instances to a factory. [Config] Delegate creation of ConfigCache instances to a factory. Apr 2, 2015
*
* @param bool $debug The debug flag to pass to ConfigCache.
*/
public function __construct($debug)
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest to use false as default value for $debug

*
* @return ConfigCacheInterface $configCache The cache instance
*/
public function cache($file, $callable);
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to change file argument name here and to make it more generic.

Copy link
Member

Choose a reason for hiding this comment

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

what about $resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know... I did not intend to change the way ConfigCache works with this PR, so...?

@aitboudad
Copy link
Contributor

@mpdude I attempt to implement this feature into #13986

@@ -36,6 +36,7 @@
"symfony/phpunit-bridge": "~2.7|~3.0.0",
"symfony/browser-kit": "~2.4|~3.0.0",
"symfony/console": "~2.6|~3.0.0",
"symfony/config": "~2.7|~3.0.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fabpot is it right to add these? Otherwise a too low version of symfony/config is installed.

mpdude added 11 commits April 6, 2015 20:18
…ake tests pass.

See for example https://travis-ci.org/symfony/symfony/jobs/57160322 - wrong (too low) versions of symfony/config are installed.
The symfony/http-kernel ~2.7 requires symonfy/config ~2.7 to work. This dependency is not picked up correctly in the Travis builds, thus it is enforced this way.
That should resolve the other dependency problems as well.
@@ -40,6 +40,9 @@
"symfony/translation": "~2.0,>=2.0.5|~3.0.0",
"symfony/var-dumper": "~2.6|~3.0.0"
},
"conflict": {
"symfony/config": "<2.7"
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this line? The requirement should been enough, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The require-dev does not work if symfony/config is followed as a transitive dependency during dep=... builds. So conflict avoids making symfony/config a hard requirement, but makes sure we get at least 2.7 if we get it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fabpot Do you prefer a PR with tests "all green", or shall I keep changes to a minimum at the cost of having dep= tests pass only once the change has been merged up to the master branch?

I think this is a special case here because changes are made across components that are then tested independently.

Copy link
Member

Choose a reason for hiding this comment

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

ok, I thought config was a hard requirement, makes sense then.

@fabpot
Copy link
Member

fabpot commented Apr 7, 2015

👍

@fabpot
Copy link
Member

fabpot commented Apr 7, 2015

@aitboudad I think we all agree that #13986 and this PR and not related. Can you remove your - 1?

@aitboudad
Copy link
Contributor

👍 ok :)

@mpdude
Copy link
Contributor Author

mpdude commented Apr 7, 2015

Travis failure on PHP 5.5 is due to a volatile test. dep=high fails because changes to symfony/config are not yet visible in dev-master.


/*
* Old cache returns only the catalogue, without resourcesHash
* Gracefully handle the case that the cached catalogue is in an "old" format, without a resourcesHash
Copy link
Contributor

Choose a reason for hiding this comment

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

that -> when

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cordoval fixed, thanks

@fabpot
Copy link
Member

fabpot commented Apr 8, 2015

Thank you @mpdude.

@fabpot fabpot closed this Apr 8, 2015
fabpot added a commit that referenced this pull request Apr 8, 2015
… a factory. (mpdude)

This PR was squashed before being merged into the 2.7 branch (closes #14178).

Discussion
----------

[Config] Delegate creation of ConfigCache instances to a factory.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes (refactoring, new flex point)
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | we'll see :-)
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | symfony/symfony-docs#5136

In the Routing/Router and Translation/Translator, delegate creation of ConfigCache instances to a factory. The factory can be setter-injected but will default to a BC implementation.

The ```ConfigCacheFactoryInterface``` is designed in a way that captures the common ```$cache = new ...; if (!$cache->isFresh()) { ... do sth }``` pattern. But more importantly, this design allows factory implementations to take additional measures to avoid race conditions before actually filling the cache.

By using an exchangeable ConfigCache factory it becomes possible to implement different resource (freshness) checking strategies, especially service-based ones.

The goal is to be able to validate Translators and Routers generated by database-based loaders. It might also help with symfony/AsseticBundle#168. This PR only contains the minimum changes needed, so the rest could be implemented in a bundle outside the core (at least for the beginning).

Component/HttpKernel/Kernel::initializeContainer still uses the ConfigCache implementation directly as there is no sensible way of getting/injecting a factory service (chicken-egg).

This is a pick off #7230. It replaces #7781 which was against the master branch. Also see #7781 for additional comments/explanations.

## Todo

* [ ] Allow `symfony/config` `~3.0.0` in `composer.json` for the HttpKernel and Translator component as well as TwigBundle once this PR has been merged into the master branch (fail deps=high tests for the time being).

Commits
-------

6fbe9b1 [Config] Delegate creation of ConfigCache instances to a factory.
@mpdude
Copy link
Contributor Author

mpdude commented Apr 8, 2015

Thank you for your patience with this over the last 2 years @fabpot :-)

@fabpot
Copy link
Member

fabpot commented Apr 8, 2015

@mpdude Thanks for your tenacity. Looking forward to your next PR :)

fabpot added a commit that referenced this pull request Apr 10, 2015
… (aitboudad)

This PR was merged into the 2.7 branch.

Discussion
----------

[Config][cache factory] check type of callback argument.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets  | #14178
| Tests pass?   | yes
| License       | MIT

/cc @mpdude

Commits
-------

11f798c [Config][cache factory] check type of callback argument.
@mpdude mpdude deleted the 7781-on-2.7 branch May 3, 2015 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants