2011-02-10 15:15:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-10 15:15:51 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ConfigCache manages PHP cache files.
|
|
|
|
*
|
|
|
|
* When debug is enabled, it knows when to flush the cache
|
|
|
|
* thanks to an array of ResourceInterface instances.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-02-10 15:15:51 +00:00
|
|
|
*/
|
|
|
|
class ConfigCache
|
|
|
|
{
|
2011-03-23 18:34:00 +00:00
|
|
|
private $debug;
|
|
|
|
private $file;
|
2011-02-10 15:15:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-03-14 17:37:25 +00:00
|
|
|
* @param string $file The absolute cache path
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param Boolean $debug Whether debugging is enabled or not
|
|
|
|
*/
|
2011-03-14 17:37:25 +00:00
|
|
|
public function __construct($file, $debug)
|
2011-02-10 15:15:51 +00:00
|
|
|
{
|
|
|
|
$this->file = $file;
|
|
|
|
$this->debug = (Boolean) $debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the cache file path.
|
|
|
|
*
|
|
|
|
* @return string The cache file path
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2011-03-14 17:37:25 +00:00
|
|
|
return $this->file;
|
2011-02-10 15:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the cache is still fresh.
|
|
|
|
*
|
2011-05-19 07:05:27 +01:00
|
|
|
* This method always returns true when debug is off and the
|
|
|
|
* cache file exists.
|
2011-02-10 15:15:51 +00:00
|
|
|
*
|
|
|
|
* @return Boolean true if the cache is fresh, false otherwise
|
|
|
|
*/
|
|
|
|
public function isFresh()
|
|
|
|
{
|
2011-03-14 17:37:25 +00:00
|
|
|
if (!file_exists($this->file)) {
|
2011-02-10 15:15:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->debug) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-14 17:37:25 +00:00
|
|
|
$metadata = $this->file.'.meta';
|
2011-02-10 15:15:51 +00:00
|
|
|
if (!file_exists($metadata)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-14 17:37:25 +00:00
|
|
|
$time = filemtime($this->file);
|
2011-02-10 15:15:51 +00:00
|
|
|
$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 array $metadata An array of ResourceInterface instances
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException When cache file can't be wrote
|
|
|
|
*/
|
|
|
|
public function write($content, array $metadata = null)
|
|
|
|
{
|
2011-03-14 17:37:25 +00:00
|
|
|
$dir = dirname($this->file);
|
2011-02-10 15:15:51 +00:00
|
|
|
if (!is_dir($dir)) {
|
|
|
|
if (false === @mkdir($dir, 0777, true)) {
|
|
|
|
throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
|
|
|
|
}
|
|
|
|
} elseif (!is_writable($dir)) {
|
|
|
|
throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
|
|
|
|
}
|
|
|
|
|
2011-03-14 17:37:25 +00:00
|
|
|
$tmpFile = tempnam(dirname($this->file), basename($this->file));
|
|
|
|
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
|
|
|
|
chmod($this->file, 0666);
|
2011-02-10 15:15:51 +00:00
|
|
|
} else {
|
|
|
|
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $metadata && true === $this->debug) {
|
2011-03-14 17:37:25 +00:00
|
|
|
$file = $this->file.'.meta';
|
2011-02-10 15:15:51 +00:00
|
|
|
$tmpFile = tempnam(dirname($file), basename($file));
|
|
|
|
if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
|
|
|
|
chmod($file, 0666);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|