ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
ConfigCache.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Assetic package, an OpenSky project.
5  *
6  * (c) 2010-2014 OpenSky Project Inc
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 Assetic\Cache;
13 
20 {
21  private $dir;
22 
28  public function __construct($dir)
29  {
30  $this->dir = $dir;
31  }
32 
40  public function has($resource)
41  {
42  return file_exists($this->getSourcePath($resource));
43  }
44 
51  public function set($resource, $value)
52  {
53  $path = $this->getSourcePath($resource);
54 
55  if (!is_dir($dir = dirname($path)) && false === @mkdir($dir, 0777, true)) {
56  // @codeCoverageIgnoreStart
57  throw new \RuntimeException('Unable to create directory '.$dir);
58  // @codeCoverageIgnoreEnd
59  }
60 
61  if (false === @file_put_contents($path, sprintf("<?php\n\n// $resource\nreturn %s;\n", var_export($value, true)))) {
62  // @codeCoverageIgnoreStart
63  throw new \RuntimeException('Unable to write file '.$path);
64  // @codeCoverageIgnoreEnd
65  }
66  }
67 
75  public function get($resource)
76  {
77  $path = $this->getSourcePath($resource);
78 
79  if (!file_exists($path)) {
80  throw new \RuntimeException('There is no cached value for '.$resource);
81  }
82 
83  return include $path;
84  }
85 
93  public function getTimestamp($resource)
94  {
95  $path = $this->getSourcePath($resource);
96 
97  if (!file_exists($path)) {
98  throw new \RuntimeException('There is no cached value for '.$resource);
99  }
100 
101  if (false === $mtime = @filemtime($path)) {
102  // @codeCoverageIgnoreStart
103  throw new \RuntimeException('Unable to determine file mtime for '.$path);
104  // @codeCoverageIgnoreEnd
105  }
106 
107  return $mtime;
108  }
109 
117  private function getSourcePath($resource)
118  {
119  $key = md5($resource);
120 
121  return $this->dir.'/'.$key[0].'/'.$key.'.php';
122  }
123 }
$path
Definition: aliased.php:25
has($resource)
Checks of the cache has a file.
Definition: ConfigCache.php:40
getSourcePath($resource)
Returns the path where the file corresponding to the supplied cache key can be included from...
getTimestamp($resource)
Returns a timestamp for when the cache was created.
Definition: ConfigCache.php:93
A config cache stores values using var_export() and include.
Definition: ConfigCache.php:19
__construct($dir)
Construct.
Definition: ConfigCache.php:28