1 | <?php |
||
19 | class CachePathManager |
||
20 | { |
||
21 | /** |
||
22 | * Name of the file with cache paths |
||
23 | */ |
||
24 | const CACHE_FILE_NAME = '/_transformation.cache'; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $options = []; |
||
30 | |||
31 | /** |
||
32 | * @var \Go\Core\AspectKernel |
||
33 | */ |
||
34 | protected $kernel; |
||
35 | |||
36 | /** |
||
37 | * @var string|null |
||
38 | */ |
||
39 | protected $cacheDir; |
||
40 | |||
41 | /** |
||
42 | * File mode |
||
43 | * |
||
44 | * @var integer |
||
45 | */ |
||
46 | protected $fileMode; |
||
47 | |||
48 | /** |
||
49 | * @var string|null |
||
50 | */ |
||
51 | protected $appDir; |
||
52 | |||
53 | /** |
||
54 | * Cached metadata for transformation state for the concrete file |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $cacheState = []; |
||
59 | |||
60 | /** |
||
61 | * New metadata items, that was not present in $cacheState |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $newCacheState = []; |
||
66 | |||
67 | 7 | public function __construct(AspectKernel $kernel) |
|
68 | { |
||
69 | 7 | $this->kernel = $kernel; |
|
70 | 7 | $this->options = $kernel->getOptions(); |
|
71 | 7 | $this->appDir = $this->options['appDir']; |
|
72 | 7 | $this->cacheDir = $this->options['cacheDir']; |
|
73 | 7 | $this->fileMode = $this->options['cacheFileMode']; |
|
74 | |||
75 | 7 | if ($this->cacheDir) { |
|
76 | 6 | if (!is_dir($this->cacheDir)) { |
|
77 | $cacheRootDir = dirname($this->cacheDir); |
||
78 | if (!is_writable($cacheRootDir) || !is_dir($cacheRootDir)) { |
||
79 | throw new \InvalidArgumentException( |
||
80 | "Can not create a directory {$this->cacheDir} for the cache. |
||
81 | Parent directory {$cacheRootDir} is not writable or not exist."); |
||
82 | } |
||
83 | mkdir($this->cacheDir, $this->fileMode, true); |
||
84 | } |
||
85 | 6 | if (!$this->kernel->hasFeature(Features::PREBUILT_CACHE) && !is_writable($this->cacheDir)) { |
|
86 | throw new \InvalidArgumentException("Cache directory {$this->cacheDir} is not writable"); |
||
87 | } |
||
88 | |||
89 | 6 | if (file_exists($this->cacheDir . self::CACHE_FILE_NAME)) { |
|
90 | $this->cacheState = include $this->cacheDir . self::CACHE_FILE_NAME; |
||
91 | } |
||
92 | } |
||
93 | 7 | } |
|
94 | |||
95 | /** |
||
96 | * Returns current cache directory for aspects, can be bull |
||
97 | * |
||
98 | * @return null|string |
||
99 | */ |
||
100 | 6 | public function getCacheDir() |
|
101 | { |
||
102 | 6 | return $this->cacheDir; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Configures a new cache directory for aspects |
||
107 | * |
||
108 | * @param string $cacheDir New cache directory |
||
109 | */ |
||
110 | public function setCacheDir($cacheDir) |
||
114 | |||
115 | /** |
||
116 | * @param string $resource |
||
117 | * @return bool|string |
||
118 | */ |
||
119 | public function getCachePathForResource($resource) |
||
127 | |||
128 | /** |
||
129 | * Tries to return an information for queried resource |
||
130 | * |
||
131 | * @param string|null $resource Name of the file or null to get all information |
||
132 | * |
||
133 | * @return array|null Information or null if no record in the cache |
||
134 | */ |
||
135 | public function queryCacheState($resource = null) |
||
151 | |||
152 | /** |
||
153 | * Put a record about some resource in the cache |
||
154 | * |
||
155 | * This data will be persisted during object destruction |
||
156 | * |
||
157 | * @param string $resource Name of the file |
||
158 | * @param array $metadata Miscellaneous information about resource |
||
159 | */ |
||
160 | public function setCacheState($resource, array $metadata) |
||
164 | |||
165 | /** |
||
166 | * Automatic destructor saves all new changes into the cache |
||
167 | * |
||
168 | * This implementation is not thread-safe, so be care |
||
169 | */ |
||
170 | public function __destruct() |
||
174 | |||
175 | /** |
||
176 | * Flushes the cache state into the file |
||
177 | * |
||
178 | * @var bool $force Should be flushed regardless of its state. |
||
179 | */ |
||
180 | public function flushCacheState($force = false) |
||
203 | |||
204 | /** |
||
205 | * Clear the cache state. |
||
206 | */ |
||
207 | public function clearCacheState() |
||
214 | } |
||
215 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: