diff --git a/classes/Blueprints/Blueprint.php b/classes/Blueprints/Blueprint.php index 98fc305..d46b4f9 100644 --- a/classes/Blueprints/Blueprint.php +++ b/classes/Blueprints/Blueprint.php @@ -38,21 +38,40 @@ public function __toString(): string return Yaml::encode($this->toArray()); } + public function blueprintCacheKeyFromModel(): string + { + $ref = new ReflectionClass($this->modelClass); + + if ($ref->isSubclassOf(\Kirby\Cms\Page::class)) { + return 'pages/'.strtolower(substr($this->modelClass, 0, -4)); + } elseif ($ref->isSubclassOf(\Kirby\Cms\File::class)) { + return 'files/'.strtolower(substr($this->modelClass, 0, -4)); + } elseif ($ref->isSubclassOf(\Kirby\Cms\User::class)) { + return 'users/'.strtolower(substr($this->modelClass, 0, -4)); + } + + return $this->modelClass; + } + public function toArray(): array { - $blueprint = $this->cache ? BlueprintCache::get($this->modelClass, null, $this->cache) : null; + $key = $this->blueprintCacheKeyFromModel(); + $blueprint = $this->cache ? BlueprintCache::get($key, null, $this->cache) : null; if ($blueprint) { - return $blueprint; + return [$key => $blueprint]; } $blueprint = self::getBlueprintUsingReflection($this->modelClass); + // get first value of array, the key of array matched the $key + $blueprint = reset($blueprint); // some might not be cacheable like when they are class based and have dynamic fields - if ($this->cache) { - BlueprintCache::set($this->modelClass, $blueprint); + // only set here now if they will not be written on __destruct by trait + if ($this->cache && ! method_exists($this->modelClass, 'blueprintCacheKey')) { + BlueprintCache::set($key, $blueprint); } - return $blueprint; + return [$key => $blueprint]; } public static function getBlueprintFieldsFromReflection(string $class): array diff --git a/classes/Blueprints/BlueprintCache.php b/classes/Blueprints/BlueprintCache.php index b39d1a8..a97d798 100644 --- a/classes/Blueprints/BlueprintCache.php +++ b/classes/Blueprints/BlueprintCache.php @@ -31,11 +31,8 @@ public static function cacheDir(): ?string protected static function cacheFile(string $key): ?string { - $hash = hash('xxh3', $key); - $hash = substr($hash, 0, 2).'/'.substr($hash, 2); - return static::cacheDir() ? - static::cacheDir().'/'.$hash.'.cache' + static::cacheDir().'/'.$key.'.cache' : null; } @@ -64,7 +61,14 @@ public static function exists(string $key, $expire = null): bool public static function get(string $key, $default = null, $expire = null): ?array { + /* + if ($loaded = \Kirby\Toolkit\A::get(\Kirby\Cms\Blueprint::$loaded, $key)) { + return $loaded; + } + */ + $file = static::cacheFile($key); + if ($file && static::exists($key, $expire)) { return Json::read($file); } @@ -84,28 +88,22 @@ public static function getKey(): string return 'bnomei.blueprints.cache.dir'; } + /* public static function preloadCachedBlueprints(): void { - $kirby = kirby(); - $preload = $kirby->option('bnomei.blueprints.preload'); - if ($preload === false) { - return; - } - - $preloaded = []; - foreach ($preload as $type) { - $blueprints = $kirby->blueprints($type); - foreach ($blueprints as $name) { - $key = $type.'/'.$name; - $blueprint = static::get($key); - if ($blueprint === null) { + foreach (Dir::dirs(static::cacheDir(), [], true) as $dir) { + foreach (Dir::files($dir, [], true) as $file) { + if (! \Kirby\Toolkit\Str::endsWith($file, '.cache')) { continue; } - $preloaded[$key] = $blueprint; + $key = str_replace([static::cacheDir().'/', '.cache'], ['', ''], $file); + $blueprint = Json::read($file); + \Kirby\Cms\Blueprint::$loaded[$key] = $blueprint; } } - $kirby->extend([ - 'blueprints' => $preloaded, - ]); + // ray('preloaded', \Kirby\Cms\Blueprint::$loaded); + + return count(\Kirby\Cms\Blueprint::$loaded); } + */ } diff --git a/classes/Blueprints/HasBlueprintCache.php b/classes/Blueprints/HasBlueprintCache.php index 2c25446..9fbe2b1 100644 --- a/classes/Blueprints/HasBlueprintCache.php +++ b/classes/Blueprints/HasBlueprintCache.php @@ -17,9 +17,9 @@ public function blueprintCacheKey(): string PageBlueprint::class => 'pages', FileBlueprint::class => 'files', UserBlueprint::class => 'users', - }; + }.'/'; - return $type.'/'.$blueprint->name(); + return $type.str_replace($type, '', $blueprint->name()); } public function __destruct() @@ -34,12 +34,16 @@ public function __destruct() if (BlueprintCache::exists($key) === false) { $blueprint = $this->blueprint(); $data = $blueprint->toArray(); + $copy = $data; if (isset(static::$blueprintCacheResolve)) { - foreach ($blueprint->toArray()['tabs'] as $tabKey => $tab) { + foreach ($copy['tabs'] as $tabKey => $tab) { foreach ($tab['columns'] as $columnKey => $column) { foreach ($column['sections'] as $sectionKey => $section) { $section = $blueprint->section($sectionKey); - $data['tabs'][$tabKey]['columns'][$columnKey]['sections'][$sectionKey]['fields'] = $section->toArray()['fields']; + $path = $data['tabs'][$tabKey]['columns'][$columnKey]['sections'][$sectionKey]; + if (array_key_exists('fields', $path)) { + $path['fields'] = $section->toArray()['fields']; + } } } } diff --git a/composer.json b/composer.json index 3f2db3e..fc6fdec 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "bnomei/kirby-blueprints", "type": "kirby-plugin", - "version": "4.4.0", + "version": "4.4.1", "license": "MIT", "homepage": "https://github.com/bnomei/kirby3-blueprints", "description": "PHP Class-based Blueprints for Kirby CMS for better type safety and code completion", @@ -51,7 +51,7 @@ "getkirby/composer-installer": "^1.2" }, "require-dev": { - "bnomei/autoloader-for-kirby": "^4.2.1", + "bnomei/autoloader-for-kirby": "^4.2", "getkirby/cms": "^4.0.0-beta.2", "larastan/larastan": "^2.9", "laravel/pint": "^1.11", diff --git a/composer.lock b/composer.lock index 1eb9df2..dedf293 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "248497de343d2ce0df04a00863816c68", + "content-hash": "5c652755ddaa253b77e8fdf98868e313", "packages": [ { "name": "getkirby/composer-installer", diff --git a/index.php b/index.php index 3a952ed..0d676d1 100644 --- a/index.php +++ b/index.php @@ -15,7 +15,6 @@ // and use it on next request \Bnomei\Blueprints\BlueprintCache::rememberCacheDir(); \Bnomei\Blueprints\Blueprint::loadPluginsAfter(); - \Bnomei\Blueprints\BlueprintCache::preloadCachedBlueprints(); }, ], ]); diff --git a/tests/content/example-2/example.txt b/tests/content/example-2/caching.txt similarity index 100% rename from tests/content/example-2/example.txt rename to tests/content/example-2/caching.txt diff --git a/tests/index.php b/tests/index.php index 91ea7dc..11f34b2 100644 --- a/tests/index.php +++ b/tests/index.php @@ -6,7 +6,9 @@ require __DIR__.'/../vendor/autoload.php'; $micotime = microtime(true); -$render = (new Kirby())->render(); +//\Bnomei\Blueprints\BlueprintCache::preloadFromLoadMap(); +$kirby = new Kirby(); +$render = $kirby->render(); $micotime = microtime(true) - $micotime; header('X-Render-Time: '.number_format($micotime, 3, '.', '').'s'); echo $render; diff --git a/tests/site/models/caching.php b/tests/site/models/caching.php new file mode 100644 index 0000000..ea5d136 --- /dev/null +++ b/tests/site/models/caching.php @@ -0,0 +1,7 @@ +toArray(); } } diff --git a/tests/site/plugins/test/models/ElephantPage.php b/tests/site/plugins/test/models/ElephantPage.php index c6d3692..aecc3c6 100644 --- a/tests/site/plugins/test/models/ElephantPage.php +++ b/tests/site/plugins/test/models/ElephantPage.php @@ -25,7 +25,7 @@ class ElephantPage extends Page public Field $rightEar; #[ - Blueprint(cache: 10) + Blueprint(cache: 500) ] public static function elephantsBlueprint(): array { diff --git a/tests/site/plugins/test/models/ProductPage.php b/tests/site/plugins/test/models/ProductPage.php index 653d664..4d8baf9 100644 --- a/tests/site/plugins/test/models/ProductPage.php +++ b/tests/site/plugins/test/models/ProductPage.php @@ -38,7 +38,7 @@ class ProductPage extends \Kirby\Cms\Page public Kirby\Content\Field $email; #[ - Blueprint(cache: 10) + Blueprint(cache: 600) ] public static function nameOfThisMethodDoesNotMatterOnlyTheAttribute(): array { diff --git a/tests/site/templates/caching.php b/tests/site/templates/caching.php new file mode 100644 index 0000000..d015cfe --- /dev/null +++ b/tests/site/templates/caching.php @@ -0,0 +1,3 @@ + array( 'name' => 'bnomei/kirby-blueprints', - 'pretty_version' => '4.4.0', - 'version' => '4.4.0.0', + 'pretty_version' => '4.4.1', + 'version' => '4.4.1.0', 'reference' => null, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', @@ -11,8 +11,8 @@ ), 'versions' => array( 'bnomei/kirby-blueprints' => array( - 'pretty_version' => '4.4.0', - 'version' => '4.4.0.0', + 'pretty_version' => '4.4.1', + 'version' => '4.4.1.0', 'reference' => null, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../',