Complex classes like Directory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Directory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Directory extends AbstractCache |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Constructor. |
||
| 17 | * |
||
| 18 | * @param array $options Array of options. |
||
| 19 | */ |
||
| 20 | 289 | public function __construct(array $options=null) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Initialize cache directories (create them) |
||
| 32 | */ |
||
| 33 | 289 | protected function initDirectories() |
|
| 34 | { |
||
| 35 | 289 | $this->getBasePath('key'); |
|
| 36 | 289 | $this->getBasePath('tag'); |
|
| 37 | 289 | $this->getBasePath('ttl'); |
|
| 38 | 289 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Get the base path, and ensure they are created |
||
| 42 | * |
||
| 43 | * @param string $type The path type (key, ttl, tag) |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | 289 | protected function getBasePath($type) |
|
| 47 | { |
||
| 48 | 289 | $path = rtrim($this->getOption('directory'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
| 49 | |||
| 50 | switch ($type) { |
||
| 51 | 289 | case 'ttl': |
|
| 52 | 289 | $path .= 'ttl' . DIRECTORY_SEPARATOR; |
|
| 53 | 289 | break; |
|
| 54 | 289 | case 'tags': |
|
| 55 | 289 | case 'tag': |
|
| 56 | 289 | $path .= 'tag' . DIRECTORY_SEPARATOR; |
|
| 57 | 289 | break; |
|
| 58 | } |
||
| 59 | 289 | $this->buildPath($path); |
|
| 60 | |||
| 61 | 289 | return $path; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the file data. |
||
| 66 | * If enable, lock file to preserve atomicity |
||
| 67 | * |
||
| 68 | * @param string $path The file path |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | 119 | protected function readFile($path) |
|
| 72 | { |
||
| 73 | 119 | $handle = fopen($path, 'rb'); |
|
| 74 | 119 | if ($this->getOption('locking')) { |
|
| 75 | 119 | flock($handle, LOCK_SH); |
|
| 76 | 119 | } |
|
| 77 | 119 | $data = stream_get_contents($handle); |
|
| 78 | 119 | if ($this->getOption('locking')) { |
|
| 79 | 119 | flock($handle, LOCK_UN); |
|
| 80 | 119 | } |
|
| 81 | 119 | fclose($handle); |
|
| 82 | |||
| 83 | 119 | return $data; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get the path of a cached data |
||
| 88 | * |
||
| 89 | * @param string $key The cache key |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 272 | protected function getKeyPath($key) |
|
| 93 | { |
||
| 94 | 272 | $dir = $this->getBasePath('key'); |
|
| 95 | 272 | $baseKey = base64_encode($key); |
|
| 96 | 272 | $sep = DIRECTORY_SEPARATOR; |
|
| 97 | 272 | $path = $dir . preg_replace('/^(.)(.)(.).+$/', '$1' . $sep . '$2' . $sep . '$3' . $sep . '$0', $baseKey); |
|
| 98 | |||
| 99 | 272 | return $path; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the path of the expiration file for a key |
||
| 104 | * |
||
| 105 | * @param string $key The cache key |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 238 | protected function getTtlPath($key) |
|
| 109 | { |
||
| 110 | 238 | $baseKey = base64_encode($key); |
|
| 111 | 238 | $path = $this->getBasePath('ttl') . substr($baseKey, 0, 4); |
|
| 112 | |||
| 113 | 238 | return $path; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the expiration data of a key |
||
| 118 | * |
||
| 119 | * @param string $key The cache key |
||
| 120 | * @return bool|int |
||
| 121 | */ |
||
| 122 | 51 | protected function loadExpire($key) |
|
| 123 | { |
||
| 124 | 51 | $path = $this->getTtlPath($key); |
|
| 125 | |||
| 126 | 51 | if (!is_file($path)) { |
|
| 127 | 17 | return false; |
|
| 128 | } |
||
| 129 | |||
| 130 | 51 | $expires = json_decode($this->readFile($path), true); |
|
| 131 | |||
| 132 | 51 | if (!array_key_exists(base64_encode($key), $expires)) { |
|
| 133 | 17 | return false; |
|
| 134 | } |
||
| 135 | |||
| 136 | 34 | return $expires[base64_encode($key)]; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the path of a tag file |
||
| 141 | * |
||
| 142 | * @param string $tag The tag name |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 136 | protected function getTagPath($tag) |
|
| 146 | { |
||
| 147 | 136 | $baseTag = base64_encode($tag); |
|
| 148 | 136 | $path = $this->getBasePath('tag') . $baseTag; |
|
| 149 | |||
| 150 | 136 | return $path; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Build and return the path of a directory |
||
| 155 | * |
||
| 156 | * @param string $path The directory path to build |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | 289 | protected function buildPath($path) |
|
| 160 | { |
||
| 161 | 289 | if (!is_dir($path)) { |
|
| 162 | 289 | mkdir($path, 0755, true); |
|
| 163 | 289 | } |
|
| 164 | 289 | return $path; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Save a tag |
||
| 169 | * |
||
| 170 | * @param string $name The tag name |
||
| 171 | * @param string[] $ids The list of cache keys associated to the tag |
||
| 172 | */ |
||
| 173 | 119 | protected function saveTag($name, $ids) |
|
| 174 | { |
||
| 175 | 119 | $ids = array_unique($ids); |
|
| 176 | 119 | array_walk($ids, function(&$item) { $item = base64_encode($item); }); |
|
| 177 | |||
| 178 | 119 | $path = $this->getTagPath($this->mapTag($name)); |
|
| 179 | 119 | $this->buildPath(dirname($path)); |
|
| 180 | 119 | file_put_contents($path, implode(PHP_EOL, $ids), $this->getOption('locking') ? LOCK_EX : null); |
|
| 181 | 119 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Save the expiration time of a cache |
||
| 185 | * |
||
| 186 | * @param string $key the cache key |
||
| 187 | * @param false|int $ttl The TTL of the cache |
||
| 188 | */ |
||
| 189 | 238 | protected function saveExpire($key, $ttl) |
|
| 190 | { |
||
| 191 | 238 | $baseKey = base64_encode($key); |
|
| 192 | |||
| 193 | 238 | $path = $this->getTtlPath($key); |
|
| 194 | 238 | $this->buildPath(dirname($path)); |
|
| 195 | |||
| 196 | 238 | $expires = array(); |
|
| 197 | 238 | if (file_exists($path) && is_file($path)) { |
|
| 198 | 17 | $expires = json_decode($this->readFile($path), true); |
|
| 199 | 17 | } |
|
| 200 | |||
| 201 | 238 | if ($ttl === false) { |
|
| 202 | 221 | if (array_key_exists($baseKey, $expires)) { |
|
| 203 | 17 | unset($expires[$baseKey]); |
|
| 204 | 17 | } else { |
|
| 205 | 204 | return; |
|
| 206 | } |
||
| 207 | 17 | } else { |
|
| 208 | 51 | $expires[$baseKey] = time() + $ttl; |
|
| 209 | } |
||
| 210 | |||
| 211 | 51 | file_put_contents($path, json_encode($expires), $this->getOption('locking') ? LOCK_EX : null); |
|
| 212 | 51 | } |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return the list of all existing tags |
||
| 216 | * |
||
| 217 | * @return string[] |
||
| 218 | */ |
||
| 219 | 68 | protected function getAllTags() |
|
| 220 | { |
||
| 221 | 68 | $basePath = $this->getBasePath('tag'); |
|
| 222 | 68 | $baseTags = scandir($basePath); |
|
| 223 | |||
| 224 | 68 | $tags = array(); |
|
| 225 | |||
| 226 | 68 | foreach ($baseTags as $baseTag) { |
|
| 227 | 68 | if (substr($baseTag, 0, 1) === '.') { |
|
| 228 | 68 | continue; |
|
| 229 | } |
||
| 230 | |||
| 231 | 51 | $tags[] = $this->removePrefixTag(base64_decode($baseTag)); |
|
| 232 | 68 | } |
|
| 233 | |||
| 234 | 68 | return $tags; |
|
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Retrieves the cache content for the given key. |
||
| 239 | * |
||
| 240 | * @param string $key The cache key to retrieve. |
||
| 241 | * @return mixed|null Returns the cached data or null. |
||
| 242 | */ |
||
| 243 | 136 | public function loadKey($key) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Retrieves the cache keys for the given tag. |
||
| 257 | * |
||
| 258 | * @param string $tag The cache tag to retrieve. |
||
| 259 | * @return array|null Returns an array of cache keys or null. |
||
| 260 | */ |
||
| 261 | 136 | public function loadTag($tag) |
|
| 262 | { |
||
| 263 | 136 | if (!$this->getOption('tag_enable')) { |
|
| 264 | 34 | return null; |
|
| 265 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Saves data to the cache. |
||
| 287 | * |
||
| 288 | * @param mixed $data The data to cache. |
||
| 289 | * @param string $key The cache id to save. |
||
| 290 | * @param array $tags The cache tags for this cache entry. |
||
| 291 | * @param int $ttl The time-to-live in seconds, if set to null the |
||
| 292 | * cache is valid forever. |
||
| 293 | * @return boolean Returns True on success or False on failure. |
||
| 294 | */ |
||
| 295 | 238 | public function save($data, $key, array $tags = null, $ttl = null) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Deletes the specified cache record. |
||
| 322 | * |
||
| 323 | * @param string $key The cache id to remove. |
||
| 324 | * @return boolean Returns True on success or False on failure. |
||
| 325 | */ |
||
| 326 | 85 | public function delete($key) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Removes all the cached entries associated with the given tag names. |
||
| 355 | * |
||
| 356 | * @param array $tags The array of tags to remove. |
||
| 357 | * @return boolean Returns True on success or False on failure. |
||
| 358 | */ |
||
| 359 | 17 | public function clean(array $tags) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Flush all the cached entries. |
||
| 378 | * |
||
| 379 | * @param boolean $all Wether to flush the whole database, or (preferably) |
||
| 380 | * the entries prefixed with prefix_key and prefix_tag. |
||
| 381 | * @return boolean Returns True on success or False on failure. |
||
| 382 | */ |
||
| 383 | 289 | public function flush($all = false) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Remove a directory |
||
| 391 | * |
||
| 392 | * @param string $dir The path of the directory to remove |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | 289 | public function delTree($dir) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Returns the time-to-live (in seconds) for the given key. |
||
| 406 | * |
||
| 407 | * @param string $key The name of the key. |
||
| 408 | * @return int|false Returns the number of seconds left, 0 if valid |
||
| 409 | * forever or False if the key is non-existant. |
||
| 410 | */ |
||
| 411 | 51 | public function getTtl($key) |
|
| 428 | } |