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 ;
2012-11-01 15:08:59 +00:00
use Symfony\Component\Config\Resource\ResourceInterface ;
2014-03-31 10:35:42 +01:00
use Symfony\Component\Filesystem\Exception\IOException ;
2013-04-21 08:24:34 +01:00
use Symfony\Component\Filesystem\Filesystem ;
2012-11-01 15:08:59 +00:00
2011-02-10 15:15:51 +00:00
/**
* 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
*/
2015-04-02 14:33:59 +01:00
class ConfigCache implements ConfigCacheInterface
2011-02-10 15:15:51 +00:00
{
2011-03-23 18:34:00 +00:00
private $debug ;
private $file ;
2011-02-10 15:15:51 +00:00
/**
2014-11-30 13:33:44 +00:00
* @ param string $file The absolute cache path
* @ param bool $debug Whether debugging is enabled or not
2011-02-10 15:15:51 +00:00
*/
2011-03-14 17:37:25 +00:00
public function __construct ( $file , $debug )
2011-02-10 15:15:51 +00:00
{
$this -> file = $file ;
2014-04-12 18:44:00 +01:00
$this -> debug = ( bool ) $debug ;
2011-02-10 15:15:51 +00:00
}
/**
* Gets the cache file path .
*
* @ return string The cache file path
2015-08-24 07:53:33 +01:00
*
2015-04-02 14:33:59 +01:00
* @ deprecated since 2.7 , to be removed in 3.0 . Use getPath () instead .
2011-02-10 15:15:51 +00:00
*/
public function __toString ()
2015-04-02 14:33:59 +01:00
{
2015-06-07 07:33:05 +01:00
@ trigger_error ( 'ConfigCache::__toString() is deprecated since version 2.7 and will be removed in 3.0. Use the getPath() method instead.' , E_USER_DEPRECATED );
2015-04-02 14:33:59 +01:00
return $this -> file ;
}
/**
* Gets the cache file path .
*
* @ return string The cache file path
*/
public function getPath ()
2011-02-10 15:15:51 +00:00
{
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
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the cache is fresh , false otherwise
2011-02-10 15:15:51 +00:00
*/
public function isFresh ()
{
2011-08-29 14:28:03 +01:00
if ( ! is_file ( $this -> file )) {
2011-02-10 15:15:51 +00:00
return false ;
}
if ( ! $this -> debug ) {
return true ;
}
2013-04-21 08:24:34 +01:00
$metadata = $this -> getMetaFile ();
2011-08-29 14:28:03 +01:00
if ( ! is_file ( $metadata )) {
2011-02-10 15:15:51 +00:00
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 .
*
2012-11-01 15:08:59 +00:00
* @ param string $content The content to write in the cache
* @ param ResourceInterface [] $metadata An array of ResourceInterface instances
2011-02-10 15:15:51 +00:00
*
2014-11-18 13:29:43 +00:00
* @ throws \RuntimeException When cache file can ' t be written
2011-02-10 15:15:51 +00:00
*/
public function write ( $content , array $metadata = null )
{
2014-03-31 10:35:42 +01:00
$mode = 0666 ;
$umask = umask ();
2013-04-21 08:24:34 +01:00
$filesystem = new Filesystem ();
2014-03-19 15:50:37 +00:00
$filesystem -> dumpFile ( $this -> file , $content , null );
2014-03-31 10:35:42 +01:00
try {
$filesystem -> chmod ( $this -> file , $mode , $umask );
} catch ( IOException $e ) {
// discard chmod failure (some filesystem may not support it)
}
2011-02-10 15:15:51 +00:00
if ( null !== $metadata && true === $this -> debug ) {
2014-03-19 15:50:37 +00:00
$filesystem -> dumpFile ( $this -> getMetaFile (), serialize ( $metadata ), null );
2014-03-31 10:35:42 +01:00
try {
$filesystem -> chmod ( $this -> getMetaFile (), $mode , $umask );
} catch ( IOException $e ) {
// discard chmod failure (some filesystem may not support it)
}
2011-02-10 15:15:51 +00:00
}
}
2013-04-21 08:24:34 +01:00
/**
* Gets the meta file path .
*
* @ return string The meta file path
*/
private function getMetaFile ()
{
return $this -> file . '.meta' ;
}
2011-02-10 15:15:51 +00:00
}