|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is licensed under MIT License. |
| 5 | + * |
| 6 | + * Copyright (c) 2026-present WebFiori Framework |
| 7 | + * |
| 8 | + * For more information on the license, please visit: |
| 9 | + * https://github.com/WebFiori/.github/blob/main/LICENSE |
| 10 | + * |
| 11 | + */ |
| 12 | +namespace WebFiori\Framework\Router; |
| 13 | + |
| 14 | +use WebFiori\Cache\Cache; |
| 15 | + |
| 16 | +/** |
| 17 | + * Caches route definitions to avoid discovery/registration overhead in production. |
| 18 | + * |
| 19 | + * @author Ibrahim |
| 20 | + */ |
| 21 | +class RouteCache { |
| 22 | + /** |
| 23 | + * @var Cache The cache instance. |
| 24 | + */ |
| 25 | + private Cache $cache; |
| 26 | + /** |
| 27 | + * @var string Cache key for route data. |
| 28 | + */ |
| 29 | + private string $cacheKey; |
| 30 | + /** |
| 31 | + * @var bool Whether caching is enabled. |
| 32 | + */ |
| 33 | + private bool $enabled; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates new instance. |
| 37 | + * |
| 38 | + * @param Cache $cache The cache instance to use. |
| 39 | + * @param bool $enabled Whether route caching is enabled. |
| 40 | + * @param string $cacheKey The cache key for route data. |
| 41 | + */ |
| 42 | + public function __construct(Cache $cache, bool $enabled = false, string $cacheKey = 'wf_routes_cache') { |
| 43 | + $this->cache = $cache; |
| 44 | + $this->enabled = $enabled; |
| 45 | + $this->cacheKey = $cacheKey; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns whether route caching is enabled. |
| 50 | + * |
| 51 | + * @return bool |
| 52 | + */ |
| 53 | + public function isEnabled(): bool { |
| 54 | + return $this->enabled; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Sets whether route caching is enabled. |
| 59 | + * |
| 60 | + * @param bool $enabled |
| 61 | + */ |
| 62 | + public function setEnabled(bool $enabled): void { |
| 63 | + $this->enabled = $enabled; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Checks if a route cache exists. |
| 68 | + * |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + public function isCached(): bool { |
| 72 | + return $this->cache->get($this->cacheKey) !== null; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Load cached routes into the Router. |
| 77 | + * |
| 78 | + * @return bool True if cache was loaded, false if no cache exists. |
| 79 | + */ |
| 80 | + public function load(): bool { |
| 81 | + if (!$this->enabled) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + $data = $this->cache->get($this->cacheKey); |
| 86 | + |
| 87 | + if ($data === null) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + $discovered = $data['discovered'] ?? []; |
| 92 | + |
| 93 | + if (!empty($discovered)) { |
| 94 | + ServiceRouter::setDiscovered($discovered); |
| 95 | + } |
| 96 | + |
| 97 | + // Re-register routes from the cached service map |
| 98 | + $configs = $data['configs'] ?? []; |
| 99 | + |
| 100 | + foreach ($configs as $config) { |
| 101 | + ServiceRouter::discover( |
| 102 | + $config['namespace'], |
| 103 | + $config['basePath'], |
| 104 | + $config['options'] ?? [], |
| 105 | + $config['directory'] ?? null, |
| 106 | + $config['recursive'] ?? false |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Build route cache from current ServiceRouter state. |
| 115 | + * |
| 116 | + * @param array $discoverConfigs Array of discover() call configs to replay on load. |
| 117 | + * |
| 118 | + * @return int Number of discovered services cached. |
| 119 | + */ |
| 120 | + public function build(array $discoverConfigs = []): int { |
| 121 | + $discovered = ServiceRouter::getDiscovered(); |
| 122 | + |
| 123 | + $data = [ |
| 124 | + 'discovered' => $discovered, |
| 125 | + 'configs' => $discoverConfigs, |
| 126 | + 'built_at' => date('c'), |
| 127 | + 'total' => count($discovered), |
| 128 | + ]; |
| 129 | + |
| 130 | + $this->cache->set($this->cacheKey, $data, 31536000, true); |
| 131 | + |
| 132 | + return count($discovered); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Clear the route cache. |
| 137 | + */ |
| 138 | + public function clear(): void { |
| 139 | + $this->cache->delete($this->cacheKey); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns the cache key. |
| 144 | + * |
| 145 | + * @return string |
| 146 | + */ |
| 147 | + public function getCacheKey(): string { |
| 148 | + return $this->cacheKey; |
| 149 | + } |
| 150 | +} |
0 commit comments