Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • ConfigCache
  • FileLocator

Interfaces

  • FileLocatorInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /*
  4:  * This file is part of the Symfony package.
  5:  *
  6:  * (c) Fabien Potencier <fabien@symfony.com>
  7:  *
  8:  * For the full copyright and license information, please view the LICENSE
  9:  * file that was distributed with this source code.
 10:  */
 11: 
 12: namespace Symfony\Component\Config;
 13: 
 14: use Symfony\Component\Config\Resource\ResourceInterface;
 15: 
 16: /**
 17:  * ConfigCache manages PHP cache files.
 18:  *
 19:  * When debug is enabled, it knows when to flush the cache
 20:  * thanks to an array of ResourceInterface instances.
 21:  *
 22:  * @author Fabien Potencier <fabien@symfony.com>
 23:  */
 24: class ConfigCache
 25: {
 26:     private $debug;
 27:     private $file;
 28: 
 29:     /**
 30:      * Constructor.
 31:      *
 32:      * @param string  $file  The absolute cache path
 33:      * @param Boolean $debug Whether debugging is enabled or not
 34:      */
 35:     public function __construct($file, $debug)
 36:     {
 37:         $this->file = $file;
 38:         $this->debug = (Boolean) $debug;
 39:     }
 40: 
 41:     /**
 42:      * Gets the cache file path.
 43:      *
 44:      * @return string The cache file path
 45:      */
 46:     public function __toString()
 47:     {
 48:         return $this->file;
 49:     }
 50: 
 51:     /**
 52:      * Checks if the cache is still fresh.
 53:      *
 54:      * This method always returns true when debug is off and the
 55:      * cache file exists.
 56:      *
 57:      * @return Boolean true if the cache is fresh, false otherwise
 58:      */
 59:     public function isFresh()
 60:     {
 61:         if (!is_file($this->file)) {
 62:             return false;
 63:         }
 64: 
 65:         if (!$this->debug) {
 66:             return true;
 67:         }
 68: 
 69:         $metadata = $this->file.'.meta';
 70:         if (!is_file($metadata)) {
 71:             return false;
 72:         }
 73: 
 74:         $time = filemtime($this->file);
 75:         $meta = unserialize(file_get_contents($metadata));
 76:         foreach ($meta as $resource) {
 77:             if (!$resource->isFresh($time)) {
 78:                 return false;
 79:             }
 80:         }
 81: 
 82:         return true;
 83:     }
 84: 
 85:     /**
 86:      * Writes cache.
 87:      *
 88:      * @param string              $content  The content to write in the cache
 89:      * @param ResourceInterface[] $metadata An array of ResourceInterface instances
 90:      *
 91:      * @throws \RuntimeException When cache file can't be wrote
 92:      */
 93:     public function write($content, array $metadata = null)
 94:     {
 95:         $dir = dirname($this->file);
 96:         if (!is_dir($dir)) {
 97:             if (false === @mkdir($dir, 0777, true)) {
 98:                 throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
 99:             }
100:         } elseif (!is_writable($dir)) {
101:             throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
102:         }
103: 
104:         $tmpFile = tempnam($dir, basename($this->file));
105:         if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
106:             @chmod($this->file, 0666 & ~umask());
107:         } else {
108:             throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
109:         }
110: 
111:         if (null !== $metadata && true === $this->debug) {
112:             $file = $this->file.'.meta';
113:             $tmpFile = tempnam($dir, basename($file));
114:             if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
115:                 @chmod($file, 0666 & ~umask());
116:             }
117:         }
118:     }
119: }
120: 
php-coveralls API documentation generated by ApiGen 2.8.0