o2system /
cache
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Cache\Adapters\File; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Cache\Abstracts\AbstractAdapter; |
||
| 19 | use O2System\Cache\DataStructures\Config; |
||
| 20 | use O2System\Spl\Exceptions\Logic\InvalidArgumentException; |
||
| 21 | use O2System\Spl\Exceptions\Runtime\OverflowException; |
||
| 22 | use O2System\Spl\Info\SplDirectoryInfo; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class Adapter |
||
| 26 | * |
||
| 27 | * @package O2System\Cache\Adapters\File |
||
| 28 | */ |
||
| 29 | abstract class Adapter extends AbstractAdapter |
||
| 30 | {
|
||
| 31 | /** |
||
| 32 | * Adapter::$platform |
||
| 33 | * |
||
| 34 | * Adapter Platform Name |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $platform = 'Filesystem Cache'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Adapter::$path |
||
| 42 | * |
||
| 43 | * Adapter Temporary Path |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $path; |
||
| 48 | |||
| 49 | // ------------------------------------------------------------------------ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Adapter::__construct |
||
| 53 | * |
||
| 54 | * @param \O2System\Cache\DataStructures\Config|NULL $config |
||
| 55 | * |
||
| 56 | * @throws \O2System\Spl\Exceptions\Runtime\OverflowException |
||
| 57 | */ |
||
| 58 | public function __construct(Config $config = null) |
||
| 59 | {
|
||
| 60 | if (isset($config)) {
|
||
| 61 | $config = $config->getArrayCopy(); |
||
| 62 | } elseif (is_null($config)) {
|
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 63 | $config = []; |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->connect($config); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Adapter::connect |
||
| 71 | * |
||
| 72 | * @param array $config Cache adapter connection configuration. |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | * @throws OverflowException |
||
| 76 | */ |
||
| 77 | public function connect(array $config) |
||
| 78 | {
|
||
| 79 | if (isset($config[ 'path' ])) {
|
||
| 80 | $config[ 'path' ] = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $config[ 'path' ]); |
||
| 81 | |||
| 82 | if (is_dir($config[ 'path' ])) {
|
||
| 83 | $this->path = $config[ 'path' ]; |
||
| 84 | } elseif (defined('PATH_CACHE')) {
|
||
| 85 | if (is_dir($config[ 'path' ])) {
|
||
| 86 | $this->path = $config[ 'path' ]; |
||
| 87 | } else {
|
||
| 88 | $this->path = PATH_CACHE . str_replace(PATH_CACHE, '', $config[ 'path' ]); |
||
|
0 ignored issues
–
show
|
|||
| 89 | } |
||
| 90 | } else {
|
||
| 91 | $this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $config[ 'path' ]; |
||
| 92 | } |
||
| 93 | } elseif (defined('PATH_CACHE')) {
|
||
| 94 | $this->path = PATH_CACHE; |
||
| 95 | } else {
|
||
| 96 | $this->path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . implode( |
||
| 97 | DIRECTORY_SEPARATOR, |
||
| 98 | ['o2system', 'cache'] |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->path = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
| 103 | |||
| 104 | if ($this->isSupported() === false) {
|
||
| 105 | throw new OverflowException('CACHE_FILE_E_UNABLE_TO_WRITE', 0, [$this->path]);
|
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // ------------------------------------------------------------------------ |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Adapter::isSupported |
||
| 113 | * |
||
| 114 | * Checks if this adapter is supported on this system. |
||
| 115 | * |
||
| 116 | * @return bool Returns FALSE if not supported. |
||
| 117 | */ |
||
| 118 | public function isSupported() |
||
| 119 | {
|
||
| 120 | if ( ! is_writable($this->path)) {
|
||
| 121 | if ( ! file_exists($this->path)) {
|
||
| 122 | mkdir($this->path, 0777, true); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | return (bool)is_writable($this->path); |
||
| 127 | } |
||
| 128 | |||
| 129 | // ------------------------------------------------------------------------ |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Adapter::isConnected |
||
| 133 | * |
||
| 134 | * Checks if this adapter has a successful connection. |
||
| 135 | * |
||
| 136 | * @return bool Returns FALSE if not supported. |
||
| 137 | */ |
||
| 138 | public function isConnected() |
||
| 139 | {
|
||
| 140 | return (bool)is_writable($this->path); |
||
| 141 | } |
||
| 142 | |||
| 143 | // ------------------------------------------------------------------------ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Adapter::increment |
||
| 147 | * |
||
| 148 | * Increment a raw value offset. |
||
| 149 | * |
||
| 150 | * @param string $key Cache item key. |
||
| 151 | * @param int $step Increment step to add. |
||
| 152 | * |
||
| 153 | * @return mixed New value on success or FALSE on failure. |
||
| 154 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||
| 155 | * @throws \Psr\Cache\InvalidArgumentException |
||
| 156 | */ |
||
| 157 | public function increment($key, $step = 1) |
||
| 158 | {
|
||
| 159 | if ( ! is_string($key)) {
|
||
|
0 ignored issues
–
show
|
|||
| 160 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
|
||
| 161 | } |
||
| 162 | |||
| 163 | if ($this->hasItem($key)) {
|
||
| 164 | $item = $this->getItem($key); |
||
| 165 | $value = $item->get(); |
||
| 166 | |||
| 167 | if (is_int($value)) {
|
||
| 168 | $value += $step; |
||
| 169 | $item->set($value); |
||
| 170 | |||
| 171 | $this->save($item); |
||
| 172 | |||
| 173 | return $value; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | // ------------------------------------------------------------------------ |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adapter::decrement |
||
| 184 | * |
||
| 185 | * Decrement a raw value offset. |
||
| 186 | * |
||
| 187 | * @param string $key Cache item key. |
||
| 188 | * @param int $step Decrement step to add. |
||
| 189 | * |
||
| 190 | * @return mixed New value on success or FALSE on failure. |
||
| 191 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||
| 192 | * @throws \Psr\Cache\InvalidArgumentException |
||
| 193 | */ |
||
| 194 | public function decrement($key, $step = 1) |
||
| 195 | {
|
||
| 196 | if ( ! is_string($key)) {
|
||
|
0 ignored issues
–
show
|
|||
| 197 | throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
|
||
| 198 | } |
||
| 199 | |||
| 200 | if ($this->hasItem($key)) {
|
||
| 201 | $item = $this->getItem($key); |
||
| 202 | $value = $item->get(); |
||
| 203 | |||
| 204 | if (is_int($value)) {
|
||
| 205 | $value -= $step; |
||
| 206 | $item->set($value); |
||
| 207 | |||
| 208 | $this->save($item); |
||
| 209 | |||
| 210 | return $value; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | return false; |
||
| 215 | } |
||
| 216 | |||
| 217 | // ------------------------------------------------------------------------ |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Adapter::getInfo |
||
| 221 | * |
||
| 222 | * Gets item pool adapter info. |
||
| 223 | * |
||
| 224 | * @return mixed |
||
| 225 | */ |
||
| 226 | public function getInfo() |
||
| 227 | {
|
||
| 228 | return new SplDirectoryInfo($this->path); |
||
| 229 | } |
||
| 230 | |||
| 231 | // ------------------------------------------------------------------------ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Adapter::getInfo |
||
| 235 | * |
||
| 236 | * Gets item pool adapter stats. |
||
| 237 | * |
||
| 238 | * @return mixed |
||
| 239 | */ |
||
| 240 | public function getStats() |
||
| 241 | {
|
||
| 242 | $directory = new \RecursiveIteratorIterator( |
||
| 243 | new \RecursiveDirectoryIterator($this->path), |
||
| 244 | \RecursiveIteratorIterator::SELF_FIRST |
||
| 245 | ); |
||
| 246 | |||
| 247 | $cacheIterator = new \RegexIterator($directory, '/^.+\.cache/i', \RecursiveRegexIterator::GET_MATCH); |
||
| 248 | |||
| 249 | $stats[ 'path' ] = $this->path; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 250 | $stats[ 'files' ] = 0; |
||
| 251 | $stats[ 'size' ] = 0; |
||
| 252 | |||
| 253 | foreach ($cacheIterator as $cacheFiles) {
|
||
| 254 | foreach ($cacheFiles as $cacheFile) {
|
||
| 255 | $stats[ 'files' ]++; |
||
| 256 | $stats[ 'size' ] += filesize($cacheFile); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | return $stats; |
||
| 261 | } |
||
| 262 | } |