|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bnomei; |
| 4 | + |
| 5 | +use Kirby\Filesystem\Dir; |
| 6 | +use Kirby\Toolkit\A; |
| 7 | +use ReflectionClass; |
| 8 | + |
| 9 | +class BoostDirInventory |
| 10 | +{ |
| 11 | + private ?array $data; |
| 12 | + |
| 13 | + private bool $enabled; |
| 14 | + |
| 15 | + private bool $isDirty; |
| 16 | + |
| 17 | + public function __construct(array $options = []) |
| 18 | + { |
| 19 | + $this->enabled = A::get($options, 'enabled', true); |
| 20 | + $this->isDirty = false; |
| 21 | + $this->data = []; |
| 22 | + |
| 23 | + if ($this->enabled) { |
| 24 | + self::patchDirClass(); |
| 25 | + |
| 26 | + if (file_exists($this->file())) { |
| 27 | + // $this->data = file_exists($this->file()) ? json_decode(file_get_contents($this->file()), true) : []; |
| 28 | + $this->data = include $this->file(); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public function __destruct() |
| 34 | + { |
| 35 | + if (! $this->isDirty || ! $this->enabled) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // file_put_contents($this->file(), json_encode($this->data, JSON_PRETTY_PRINT)); |
| 40 | + file_put_contents( |
| 41 | + $this->file(), |
| 42 | + '<?php'.PHP_EOL.' return '.var_export($this->data, true).';', |
| 43 | + LOCK_EX |
| 44 | + ) !== false; |
| 45 | + if (function_exists('opcache_invalidate')) { |
| 46 | + opcache_invalidate($this->file()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function file(): string |
| 51 | + { |
| 52 | + $cache = BoostCache::singleton(); |
| 53 | + if ($cache && method_exists($cache, 'root')) { |
| 54 | + return $cache->root().'/boost-dir-inventory.cache.php'; |
| 55 | + } |
| 56 | + |
| 57 | + return __DIR__.'/../boost-dir-inventory.cache.php'; |
| 58 | + } |
| 59 | + |
| 60 | + public function get($key): ?array |
| 61 | + { |
| 62 | + if (! $this->enabled) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $key = $this->key($key); |
| 67 | + |
| 68 | + return A::get($this->data, $key); |
| 69 | + } |
| 70 | + |
| 71 | + public function set($key, ?array $input = null): void |
| 72 | + { |
| 73 | + if (! $this->enabled) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $this->isDirty = true; |
| 78 | + $key = $this->key($key); |
| 79 | + $this->data[$key] = $input; |
| 80 | + } |
| 81 | + |
| 82 | + public static function flush(): void |
| 83 | + { |
| 84 | + $instance = static::singleton(); |
| 85 | + if (file_exists($instance->file())) { |
| 86 | + unlink($instance->file()); |
| 87 | + } |
| 88 | + |
| 89 | + $instance->data = []; |
| 90 | + $instance->isDirty = true; |
| 91 | + } |
| 92 | + |
| 93 | + public static function singleton(array $options = []): self |
| 94 | + { |
| 95 | + static $instance; |
| 96 | + |
| 97 | + return $instance ?: $instance = new self($options); |
| 98 | + } |
| 99 | + |
| 100 | + private function key($key): string |
| 101 | + { |
| 102 | + return is_array($key) ? hash('xxh3', print_r($key, true)) : $key; |
| 103 | + } |
| 104 | + |
| 105 | + public static function patchDirClass(): void |
| 106 | + { |
| 107 | + $reflection = new ReflectionClass(Dir::class); |
| 108 | + $file = $reflection->getFileName(); |
| 109 | + |
| 110 | + $content = file_get_contents($file); |
| 111 | + $head = <<<'CODE' |
| 112 | +$items = static::read($dir, $contentIgnore); |
| 113 | +CODE; |
| 114 | + |
| 115 | + $head_new = <<<'CODE' |
| 116 | +$cacheKey = func_get_args(); |
| 117 | + if ($cache = \Bnomei\BoostDirInventory::singleton()->get($cacheKey)) { |
| 118 | + return $cache; |
| 119 | + } |
| 120 | + $items = static::read($dir, $contentIgnore); |
| 121 | +CODE; |
| 122 | + $foot = <<<'CODE' |
| 123 | +return $inventory; |
| 124 | + } |
| 125 | +CODE; |
| 126 | + $foot_new = <<<'CODE' |
| 127 | +\Bnomei\BoostDirInventory::singleton()->set($cacheKey, $inventory); |
| 128 | +
|
| 129 | + return $inventory; |
| 130 | + } |
| 131 | +CODE; |
| 132 | + if (strpos($content, $head_new) !== false) { |
| 133 | + return; |
| 134 | + } |
| 135 | + $content = str_replace($head, $head_new, $content); |
| 136 | + $content = str_replace($foot, $foot_new, $content); |
| 137 | + file_put_contents($file, $content); |
| 138 | + |
| 139 | + if (function_exists('opcache_invalidate')) { |
| 140 | + opcache_invalidate($file); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments