8000 Refactor behaviour triggered by a flag into separate classes. by mpdude · Pull Request #7782 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Refactor behaviour triggered by a flag into separate classes. #7782

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 13 additions & 50 deletions src/Symfony/Component/Config/ConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
use Symfony\Component\Filesystem\Filesystem;

/**
* ConfigCache manages PHP cache files.
*
* When debug is enabled, it knows when to flush the cache
* thanks to an array of ResourceInterface instances.
* ConfigCache is a backwards-compatible way of using the
* cache implementation classes.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Matthias Pigulla <mp@webfactory.de>
*/
class ConfigCache
{
private $debug;
private $file;
private $cacheImplementation;

/**
* Constructor.
Expand All @@ -35,8 +33,11 @@ class ConfigCache
*/
public function __construct($file, $debug)
{
$this->file = $file;
$this->debug = (Boolean) $debug;
if ($debug) {
$this->cacheImplementation = new ResourceValidatingCache($file);
} else {
$this->cacheImplementation = new NonvalidatingCache($file);
}
}

/**
Expand All @@ -46,7 +47,7 @@ public function __construct($file, $debug)
*/
public function __toString()
{
return $this->file;
return $this->cacheImplementation->__toString();
}

/**
Expand All @@ -59,28 +60,7 @@ public function __toString()
*/
public function isFresh()
{
if (!is_file($this->file)) {
return false;
}

if (!$this->debug) {
return true;
}

$metadata = $this->getMetaFile();
if (!is_file($metadata)) {
return false;
}

$time = filemtime($this->file);
$meta = unserialize(file_get_contents($metadata));
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
return false;
}
}

return true;
return $this->cacheImplementation->isFresh();
}

/**
Expand All @@ -89,27 +69,10 @@ public function isFresh()
* @param string $content The content to write in the cache
* @param ResourceInterface[] $metadata An array of ResourceInterface instances
*
* @throws \RuntimeException When cache file can't be wrote
* @throws \RuntimeException When the cache file cannot be written.
*/
public function write($content, array $metadata = null)
{
$mode = 0666 & ~umask();
$filesystem = new Filesystem();
$filesystem->dumpFile($this->file, $content, $mode);

if (null !== $metadata && true === $this->debug) {
$filesystem->dumpFile($this->getMetaFile(), serialize($metadata), $mode);
}
$this->cacheImplementation->write($content, $metadata);
}

/**
* Gets the meta file path.
*
* @return string The meta file path
*/
private function getMetaFile()
{
return $this->file.'.meta';
}

}
69 changes: 69 additions & 0 deletions src/Symfony/Component/Config/NonvalidatingCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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;
use Symfony\Component\Filesystem\Filesystem;

/**
* A cache that never expires once it has been written.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Matthias Pigulla <mp@webfactory.de>
*/
class NonvalidatingCache
{
protected $file;

/**
* Constructor.
*
* @param string $file The absolute cache path
*/
public function __construct($file)
{
$this->file = $file;
}

/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->file;
}

/**
* By design, this method always returns true once the cache
* has been initialized.
*
* {@inheritdoc}
*/
public function isFresh()
{
if (!is_file($this->file)) {
return false;
}

return true;
}

/**
* This implementation ignores the metadata.
* {@inheritdoc}
*/
public function write($content, array $metadata = null)
{
$filesystem = new Filesystem();
$filesystem->dumpFile($this->file, $content, 0666 & ~umask());
}
}
86 changes: 86 additions & 0 deletions src/Symfony/Component/Config/ResourceValidatingCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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;
use Symfony\Component\Filesystem\Filesystem;

/**
* ConfigCache manages PHP cache files.
*
* When debug is enabled, it knows when to flush the cache
* thanks to an array of ResourceInterface instances.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ResourceValidatingCache extends NonvalidatingCache
{

/**
* Checks if the cache is still fresh.
*
* This method evaluates the resources/metadata passed to the
* write() method.
*
* @return Boolean true if the cache is fresh, false otherwise
*/
public function isFresh()
{
if (!parent::isFresh()) {
return false;
}

$metadata = $this->getMetaFile();
if (!is_file($metadata)) {
return false;
}

$time = filemtime($this->file);
$meta = unserialize(file_get_contents($metadata));
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
return false;
}
}

return true;
}

/**
* Writes cache.
*
* @param string $content The content to write in the cache
* @param ResourceInterface[] $metadata An array of ResourceInterface instances
*
* @throws \RuntimeException When cache file can't be wrote
*/
public function write($content, array $metadata = null)
{
if (null !== $metadata) {
$filesystem = new Filesystem();
$filesystem->dumpFile($this->getMetaFile(), serialize($metadata), 0666 & ~umask());
}

parent::write($content);
}

/**
* Gets the meta file path.
*
* @return string The meta file path
*/
private function getMetaFile()
{
return $this->file.'.meta';
}

}
0