|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Storage\Adapter; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Common\Assert; |
|
28
|
|
|
use DirectoryIterator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* FilesystemAdapter |
|
32
|
|
|
* |
|
33
|
|
|
* Provides caching operations based on filesystem. |
|
34
|
|
|
* @author Celestino Diaz <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class FilesystemAdapter implements Adapter { |
|
37
|
|
|
|
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
const LIFETIME_FORMAT = "YmdHis"; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
private $cacheDirectory; |
|
43
|
|
|
|
|
44
|
|
|
/** @var boolean */ |
|
45
|
|
|
private $serializeCacheContent; |
|
46
|
|
|
|
|
47
|
|
|
/** @var string */ |
|
48
|
|
|
private $cacheFileNameSuffix; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Class constructor. |
|
52
|
|
|
* @param string $cacheDirectory the directory used for the cache operations |
|
53
|
|
|
* @param boolean $serializeCacheContent flag to serialize the content cached |
|
54
|
|
|
* @param string $cacheFileNameSuffix the suffix to add to caching file names |
|
55
|
|
|
* @throws \InvalidArgumentException if an argument is not valid |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function __construct($cacheDirectory, $serializeCacheContent = true, $cacheFileNameSuffix = ".cache") { |
|
58
|
1 |
|
Assert::isString($cacheDirectory); |
|
59
|
1 |
|
Assert::isBoolean($serializeCacheContent); |
|
60
|
1 |
|
Assert::isString($cacheFileNameSuffix); |
|
61
|
|
|
|
|
62
|
1 |
|
$this->cacheDirectory = rtrim($cacheDirectory, "\\/").DIRECTORY_SEPARATOR; |
|
63
|
1 |
|
$this->serializeCacheContent = $serializeCacheContent; |
|
64
|
1 |
|
$this->cacheFileNameSuffix = $cacheFileNameSuffix; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** {@inheritDoc} */ |
|
68
|
2 |
|
public function get($identifier) { |
|
69
|
2 |
|
Assert::isString($identifier); |
|
70
|
2 |
|
$timestampBytesLength = strlen(date(self::LIFETIME_FORMAT)); |
|
71
|
2 |
|
$cacheFilePath = $this->getCacheFilePath($identifier); |
|
72
|
|
|
|
|
73
|
2 |
|
$file = fopen($cacheFilePath, "r"); |
|
74
|
2 |
|
$expirationDate = fread($file, $timestampBytesLength); |
|
75
|
|
|
|
|
76
|
2 |
|
if (strtotime($expirationDate) < time()) { |
|
77
|
1 |
|
fclose($file); |
|
78
|
1 |
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$cachedContent = fread($file, filesize($cacheFilePath) - $timestampBytesLength); |
|
82
|
1 |
|
fclose($file); |
|
83
|
|
|
|
|
84
|
1 |
|
return ($this->serializeCacheContent ? unserialize($cachedContent) : $cachedContent); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** {@inheritDoc} */ |
|
88
|
1 |
|
public function set($identifier, $content, $lifetime) { |
|
89
|
1 |
|
Assert::isString($identifier); |
|
90
|
1 |
|
Assert::isInteger($lifetime); |
|
91
|
|
|
|
|
92
|
1 |
|
if ($this->serializeCacheContent) { |
|
93
|
1 |
|
$content = serialize($content); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$file = fopen($this->getCacheFilePath($identifier), "w"); |
|
97
|
1 |
|
fwrite($file, date(self::LIFETIME_FORMAT, (time() + $lifetime)).$content); |
|
98
|
1 |
|
fclose($file); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** {@inheritDoc} */ |
|
104
|
1 |
|
public function delete($identifier) { |
|
105
|
1 |
|
Assert::isString($identifier); |
|
106
|
1 |
|
if (file_exists(($fileName = $this->getCacheFilePath($identifier)))) { |
|
107
|
1 |
|
unlink($fileName); |
|
108
|
1 |
|
} |
|
109
|
1 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** {@inheritDoc} */ |
|
113
|
1 |
|
public function flush() { |
|
114
|
1 |
|
$directoryIterator = new DirectoryIterator($this->cacheDirectory); |
|
115
|
1 |
|
foreach ($directoryIterator as $fileInfo) { |
|
116
|
1 |
|
if ($fileInfo->isFile() |
|
117
|
1 |
|
&& preg_match(sprintf("~%s$~", $this->cacheFileNameSuffix), $fileInfo->getFilename())) { |
|
118
|
1 |
|
unlink($fileInfo->getPathname()); |
|
119
|
1 |
|
} |
|
120
|
1 |
|
} |
|
121
|
1 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** {@inheritDoc} */ |
|
125
|
1 |
|
public function isReady() { |
|
126
|
1 |
|
return (is_writable($this->cacheDirectory) && is_readable($this->cacheDirectory)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns the cache file path for a unique identifier. |
|
131
|
|
|
* @param string $identifier the cache identifier. |
|
132
|
|
|
* @return string the cache file path |
|
133
|
|
|
*/ |
|
134
|
1 |
|
private function getCacheFilePath($identifier) { |
|
135
|
1 |
|
return $this->cacheDirectory.$identifier.$this->cacheFileNameSuffix; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|