-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[ConfigCache] Use non-local files resources #10761
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?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\Bundle\FrameworkBundle\Config\Resource\Refresher; | ||
|
||
use Symfony\Component\Config\Resource\Refresher\RefresherInterface; | ||
use Symfony\Component\Config\Resource\MutableResourceInterface; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* @author Luc Vieillescazes <luc@vieillescazes.net> | ||
*/ | ||
class ContainerAwareRefresher implements RefresherInterface | ||
{ | ||
private $container; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function refresh(MutableResourceInterface $resource) | ||
{ | ||
if ($resource instanceof ContainerAwareInterface) { | ||
$resource->setContainer($this->container); | ||
} | ||
} | ||
} |
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\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
class ConfigResourceRefresherPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('config.resource.chain_refresher')) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('config.resource.chain_refresher'); | ||
|
||
foreach ($container->findTaggedServiceIds('config.resource_refresher') as $id => $attributes) { | ||
$definition->addMethodCall('addRefresher', array(new Reference($id))); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="config.resource.chain_refresher.class">Symfony\Component\Config\Resource\Refresher\ChainRefresher</parameter> | ||
<parameter key="config.resource.container_aware_refresher.class">Symfony\Bundle\FrameworkBundle\Config\Resource\Refresher\ContainerAwareRefresher</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="config.resource.chain_refresher" class="%config.resource.chain_refresher.class%"> | ||
</service> | ||
<service id="config.resource.container_aware_refresher" class="%config.resource.container_aware_refresher.class%"> | ||
<argument type="service" id="service_container" /> | ||
<tag name="config.resource_refresher" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
namespace Symfony\Component\Config; | ||
|
||
use Symfony\Component\Config\Resource\ResourceInterface; | ||
use Symfony\Component\Config\Resource\MutableResourceInterface; | ||
use Symfony\Component\Config\Resource\Refresher\RefresherInterface; | ||
use Symfony\Component\Filesystem\Exception\IOException; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
|
@@ -58,25 +60,25 @@ public function __toString() | |
* | ||
* @return bool true if the cache is fresh, false otherwise | ||
*/ | ||
public function isFresh() | ||
public function isFresh(RefresherInterface $refresher = null) | ||
{ | ||
if (!is_file($this->file)) { | ||
return false; | ||
} | ||
|
||
if (!$this->debug) { | ||
return true; | ||
} | ||
|
||
$metadata = $this->getMetaFile(); | ||
if (!is_file($metadata)) { | ||
return false; | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is BC break isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, So in prod, before the method ended with if (!$this->debug) {
return true;
} now it ends with if (!is_file($metadata)) {
return true;
} In dev, we still check all resources like before |
||
} | ||
|
||
$time = filemtime($this->file); | ||
$meta = unserialize(file_get_contents($metadata)); | ||
$timestamp = filemtime($this->file); | ||
foreach ($meta as $resource) { | ||
if (!$resource->isFresh($time)) { | ||
if (null !== $refresher && $resource instanceof MutableResourceInterface) { | ||
$refresher->refresh($resource); | ||
} | ||
|
||
if (!$resource->isFresh($timestamp)) { | ||
return false; | ||
} | ||
} | ||
|
@@ -104,13 +106,30 @@ public function write($content, array $metadata = null) | |
// discard chmod failure (some filesystem may not support it) | ||
} | ||
|
||
if (null !== $metadata && true === $this->debug) { | ||
$filesystem->dumpFile($this->getMetaFile(), serialize($metadata), null); | ||
$mutables = array(); | ||
if (is_array($metadata)) { | ||
foreach ($metadata as $resource) { | ||
if ($resource instanceof MutableResourceInterface) { | ||
if (true === $resource->isMutable()) { | ||
$mutables[] = $resource; | ||
} | ||
} else { | ||
if (true === $this->debug) { | ||
$mutables[] = $resource; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (count($mutables) > 0) { | ||
$filesystem->dumpFile($this->getMetaFile(), serialize($mutables), null); | ||
try { | ||
$filesystem->chmod($this->getMetaFile(), $mode, $umask); | ||
} catch (IOException $e) { | ||
// discard chmod failure (some filesystem may not support it) | ||
} | ||
} else { | ||
$filesystem->remove($this->getMetaFile()); | ||
} | ||
} | ||
|
||
|
@@ -123,5 +142,4 @@ private function getMetaFile() | |
{ | ||
return $this->file.'.meta'; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Resource; | ||
|
||
/** | ||
* MutableResourceInterface is the interface that must be implemented by all Resource classes. | ||
* | ||
* @author Luc Vieillescazes <luc@vieillescazes.net> | ||
*/ | ||
interface MutableResourceInterface extends ResourceInterface | ||
{ | ||
public function isMutable(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?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\Resource\Refresher; | ||
|
||
use Symfony\Component\Config\Resource\MutableResourceInterface; | ||
|
||
/** | ||
* @author Luc Vieillescazes <luc@vieillescazes.net> | ||
*/ | ||
class ChainRefresher implements RefresherInterface | ||
{ | ||
private $refreshers = array(); | ||
|
||
public function addRefresher(RefresherInterface $refresher) | ||
{ | ||
$this->refreshers[] = $refresher; | ||
} | ||
|
||
public function refresh(MutableResourceInterface $resource) | ||
{ | ||
foreach ($this->refreshers as $refresher) { | ||
$refresher->refresh($resource); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Resource\Refresher; | ||
|
||
use Symfony\Component\Config\Resource\MutableResourceInterface; | ||
|
||
/** | ||
* @author Luc Vieillescazes <luc@vieillescazes.net> | ||
*/ | ||
interface RefresherInterface | ||
{ | ||
public function refresh(MutableResourceInterface $resource); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be kept IMO. In non-debug mode (i.e. in prod), we don't want to consider the cache resources as mutable, even if they are mutable in debug mode (i.e. in dev). This is the (only) goal of this flag and it should be preserved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, it's one of the goal of this PR. We want to refresh cache (translations/routes) easily in production.
I know that the cost is one more is_file() call....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iamluc IMO, if you define your routes in the DB, you should have a look at the extended routing system implemented by the CMF project rather than trying to hack the cache reloading in a non-BC way