|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of Blitz PHP framework. |
| 5 | + * |
| 6 | + * (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace BlitzPHP\Autoloader; |
| 13 | + |
| 14 | +use Psr\SimpleCache\CacheInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Localisateur de fichiers avec cache |
| 18 | + * |
| 19 | + * @credit <a href="https://codeigniter.com">CodeIgniter4 - CodeIgniter\Autoloader\FileLocatorCached</a> |
| 20 | + */ |
| 21 | +final class LocatorCached implements LocatorInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Donnees mise en cach |
| 25 | + * |
| 26 | + * [method => data] |
| 27 | + * E.g., |
| 28 | + * [ |
| 29 | + * 'search' => [$path => $foundPaths], |
| 30 | + * ] |
| 31 | + */ |
| 32 | + private array $cache = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * Le cache est-il mis à jour ? |
| 36 | + */ |
| 37 | + private bool $cacheUpdated = false; |
| 38 | + |
| 39 | + private string $cacheKey = 'FileLocatorCache'; |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructor |
| 43 | + */ |
| 44 | + public function __construct(private Locator $locator, private CacheInterface $cacheHandler) |
| 45 | + { |
| 46 | + $this->loadCache(); |
| 47 | + } |
| 48 | + |
| 49 | + private function loadCache(): void |
| 50 | + { |
| 51 | + $data = $this->cacheHandler->get($this->cacheKey); |
| 52 | + |
| 53 | + if (is_array($data)) { |
| 54 | + $this->cache = $data; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function __destruct() |
| 59 | + { |
| 60 | + $this->saveCache(); |
| 61 | + } |
| 62 | + |
| 63 | + private function saveCache(): void |
| 64 | + { |
| 65 | + if ($this->cacheUpdated) { |
| 66 | + $this->cacheHandler->set($this->cacheKey, $this->cache, 3600 * 24); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Supprime les donnees de du cache |
| 72 | + */ |
| 73 | + public function deleteCache(): void |
| 74 | + { |
| 75 | + $this->cacheHandler->delete($this->cacheKey); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + */ |
| 81 | + public function findQualifiedNameFromPath(string $path) |
| 82 | + { |
| 83 | + if (isset($this->cache['findQualifiedNameFromPath'][$path])) { |
| 84 | + return $this->cache['findQualifiedNameFromPath'][$path]; |
| 85 | + } |
| 86 | + |
| 87 | + $classname = $this->locator->findQualifiedNameFromPath($path); |
| 88 | + |
| 89 | + $this->cache['findQualifiedNameFromPath'][$path] = $classname; |
| 90 | + $this->cacheUpdated = true; |
| 91 | + |
| 92 | + return $classname; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritDoc} |
| 97 | + */ |
| 98 | + public function getClassname(string $file): string |
| 99 | + { |
| 100 | + if (isset($this->cache['getClassname'][$file])) { |
| 101 | + return $this->cache['getClassname'][$file]; |
| 102 | + } |
| 103 | + |
| 104 | + $classname = $this->locator->getClassname($file); |
| 105 | + |
| 106 | + $this->cache['getClassname'][$file] = $classname; |
| 107 | + $this->cacheUpdated = true; |
| 108 | + |
| 109 | + return $classname; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * {@inheritDoc} |
| 114 | + */ |
| 115 | + public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array |
| 116 | + { |
| 117 | + if (isset($this->cache['search'][$path][$ext][$prioritizeApp])) { |
| 118 | + return $this->cache['search'][$path][$ext][$prioritizeApp]; |
| 119 | + } |
| 120 | + |
| 121 | + $foundPaths = $this->locator->search($path, $ext, $prioritizeApp); |
| 122 | + |
| 123 | + $this->cache['search'][$path][$ext][$prioritizeApp] = $foundPaths; |
| 124 | + $this->cacheUpdated = true; |
| 125 | + |
| 126 | + return $foundPaths; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * {@inheritDoc} |
| 131 | + */ |
| 132 | + public function listFiles(string $path): array |
| 133 | + { |
| 134 | + if (isset($this->cache['listFiles'][$path])) { |
| 135 | + return $this->cache['listFiles'][$path]; |
| 136 | + } |
| 137 | + |
| 138 | + $files = $this->locator->listFiles($path); |
| 139 | + |
| 140 | + $this->cache['listFiles'][$path] = $files; |
| 141 | + $this->cacheUpdated = true; |
| 142 | + |
| 143 | + return $files; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * {@inheritDoc} |
| 148 | + */ |
| 149 | + public function listNamespaceFiles(string $prefix, string $path): array |
| 150 | + { |
| 151 | + if (isset($this->cache['listNamespaceFiles'][$prefix][$path])) { |
| 152 | + return $this->cache['listNamespaceFiles'][$prefix][$path]; |
| 153 | + } |
| 154 | + |
| 155 | + $files = $this->locator->listNamespaceFiles($prefix, $path); |
| 156 | + |
| 157 | + $this->cache['listNamespaceFiles'][$prefix][$path] = $files; |
| 158 | + $this->cacheUpdated = true; |
| 159 | + |
| 160 | + return $files; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * {@inheritDoc} |
| 165 | + */ |
| 166 | + public function locateFile(string $file, ?string $folder = null, string $ext = 'php'): false|string |
| 167 | + { |
| 168 | + if (isset($this->cache['locateFile'][$file][$folder][$ext])) { |
| 169 | + return $this->cache['locateFile'][$file][$folder][$ext]; |
| 170 | + } |
| 171 | + |
| 172 | + $files = $this->locator->locateFile($file, $folder, $ext); |
| 173 | + |
| 174 | + $this->cache['locateFile'][$file][$folder][$ext] = $files; |
| 175 | + $this->cacheUpdated = true; |
| 176 | + |
| 177 | + return $files; |
| 178 | + } |
| 179 | +} |
0 commit comments