vendor/symfony/src/Symfony/Component/Config/Resource/DirectoryResource.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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\Resource;
       
    13 
       
    14 /**
       
    15  * DirectoryResource represents a resources stored in a subdirectory tree.
       
    16  *
       
    17  * @author Fabien Potencier <fabien@symfony.com>
       
    18  */
       
    19 class DirectoryResource implements ResourceInterface
       
    20 {
       
    21     private $resource;
       
    22     private $pattern;
       
    23 
       
    24     /**
       
    25      * Constructor.
       
    26      *
       
    27      * @param string $resource The file path to the resource
       
    28      * @param string $pattern  A pattern to restrict monitored files
       
    29      */
       
    30     public function __construct($resource, $pattern = null)
       
    31     {
       
    32         $this->resource = $resource;
       
    33         $this->pattern = $pattern;
       
    34     }
       
    35 
       
    36     /**
       
    37      * Returns a string representation of the Resource.
       
    38      *
       
    39      * @return string A string representation of the Resource
       
    40      */
       
    41     public function __toString()
       
    42     {
       
    43         return (string) $this->resource;
       
    44     }
       
    45 
       
    46     /**
       
    47      * Returns the resource tied to this Resource.
       
    48      *
       
    49      * @return mixed The resource
       
    50      */
       
    51     public function getResource()
       
    52     {
       
    53         return $this->resource;
       
    54     }
       
    55 
       
    56     /**
       
    57      * Returns true if the resource has not been updated since the given timestamp.
       
    58      *
       
    59      * @param integer $timestamp The last time the resource was loaded
       
    60      *
       
    61      * @return Boolean true if the resource has not been updated, false otherwise
       
    62      */
       
    63     public function isFresh($timestamp)
       
    64     {
       
    65         if (!file_exists($this->resource)) {
       
    66             return false;
       
    67         }
       
    68 
       
    69         $newestMTime = filemtime($this->resource);
       
    70         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
       
    71             // if regex filtering is enabled only check matching files
       
    72             if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
       
    73                 continue;
       
    74             }
       
    75 
       
    76             // always monitor directories for changes, except the .. entries
       
    77             // (otherwise deleted files wouldn't get detected)
       
    78             if ($file->isDir() && '/..' === substr($file, -3)) {
       
    79                 continue;
       
    80             }
       
    81 
       
    82             $newestMTime = max($file->getMTime(), $newestMTime);
       
    83         }
       
    84 
       
    85         return $newestMTime < $timestamp;
       
    86     }
       
    87 }