Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #14178 [Config] Delegate creation of ConfigCache instances to…
… 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.
- Loading branch information
Showing
13 changed files
with
347 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config; | ||
|
||
/** | ||
* Basic implementation for ConfigCacheFactoryInterface | ||
* that will simply create an instance of ConfigCache. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
class ConfigCacheFactory implements ConfigCacheFactoryInterface | ||
{ | ||
/** | ||
* @var bool Debug flag passed to the ConfigCache | ||
*/ | ||
private $debug; | ||
|
||
/** | ||
* @param bool $debug The debug flag to pass to ConfigCache | ||
*/ | ||
public function __construct($debug) | ||
{ | ||
$this->debug = $debug; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cache($file, $callback) | ||
{ | ||
$cache = new ConfigCache($file, $this->debug); | ||
|
||
if (!$cache->isFresh()) { | ||
call_user_func($callback, $cache); | ||
} | ||
|
||
return $cache; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Config/ConfigCacheFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config; | ||
|
||
/** | ||
* Interface for a ConfigCache factory. This factory creates | ||
* an instance of ConfigCacheInterface and initializes the | ||
* cache if necessary. | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
interface ConfigCacheFactoryInterface | ||
{ | ||
/** | ||
* Creates a cache instance and (re-)initializes it if necessary. | ||
* | ||
* @param string $file The absolute cache file path | ||
* @param callable $callable The callable to be executed when the cache needs to be filled (i. e. is not fresh). The cache will be passed as the only parameter to this callback | ||
* | ||
* @return ConfigCacheInterface $configCache The cache instance | ||
*/ | ||
public function cache($file, $callable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config; | ||
|
||
use Symfony\Component\Config\Resource\ResourceInterface; | ||
|
||
/** | ||
* Interface for ConfigCache | ||
* | ||
* @author Matthias Pigulla <mp@webfactory.de> | ||
*/ | ||
interface ConfigCacheInterface | ||
{ | ||
/** | ||
* Gets the cache file path. | ||
* | ||
* @return string The cache file path | ||
*/ | ||
public function getPath(); | ||
|
||
/** | ||
* Checks if the cache is still fresh. | ||
* | ||
* This check should take the metadata passed to the write() method into consideration. | ||
* | ||
* @return bool Whether the cache is still fresh. | ||
*/ | ||
public function isFresh(); | ||
|
||
/** | ||
* Writes the given content into the cache file. Metadata will be stored | ||
* independently and can be used to check cache freshness at a later time. | ||
* | ||
* @param string $content The content to write into the cache | ||
* @param ResourceInterface[]|null $metadata An array of ResourceInterface instances | ||
* | ||
* @throws \RuntimeException When the cache file cannot be written | ||
*/ | ||
public function write($content, array $metadata = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.