|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenForgeProject\MageForge\Service\Inspector\Cache; |
| 6 | + |
| 7 | +use Magento\Framework\View\Element\BlockInterface; |
| 8 | +use Magento\Framework\View\LayoutInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Collects performance metrics from Magento blocks for Inspector |
| 12 | + * |
| 13 | + * Measures render time and extracts cache configuration with strict type safety (PHPStan Level 8). |
| 14 | + * |
| 15 | + * @package OpenForgeProject\MageForge |
| 16 | + */ |
| 17 | +class BlockCacheCollector |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @param LayoutInterface $layout |
| 21 | + */ |
| 22 | + public function __construct( |
| 23 | + private readonly LayoutInterface $layout |
| 24 | + ) { |
| 25 | + } |
| 26 | + /** |
| 27 | + * Get cache information from block |
| 28 | + * |
| 29 | + * Safely extracts cache lifetime, key, and tags with explicit type checking |
| 30 | + * to satisfy PHPStan Level 8 requirements. |
| 31 | + * |
| 32 | + * @param BlockInterface $block |
| 33 | + * @return array{cacheable: bool, lifetime: int|null, cacheKey: string, cacheTags: array<int, string>, pageCacheable: bool} |
| 34 | + */ |
| 35 | + public function getCacheInfo(BlockInterface $block): array |
| 36 | + { |
| 37 | + $lifetime = $this->resolveCacheLifetime($block); |
| 38 | + $cacheable = $lifetime !== false; |
| 39 | + |
| 40 | + if ($cacheable && $this->isBlockScopePrivate($block)) { |
| 41 | + $cacheable = false; |
| 42 | + $lifetime = null; |
| 43 | + } |
| 44 | + |
| 45 | + $cacheKey = $this->resolveCacheKey($block); |
| 46 | + $cacheTags = $this->resolveCacheTags($block); |
| 47 | + |
| 48 | + // Check if page itself is cacheable |
| 49 | + $pageCacheable = $this->isPageCacheable(); |
| 50 | + |
| 51 | + return [ |
| 52 | + 'cacheable' => $cacheable, |
| 53 | + 'lifetime' => $lifetime === false ? null : $lifetime, |
| 54 | + 'cacheKey' => $cacheKey, |
| 55 | + 'cacheTags' => $cacheTags, |
| 56 | + 'pageCacheable' => $pageCacheable, |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Resolve cache lifetime from block |
| 62 | + * |
| 63 | + * @param BlockInterface $block |
| 64 | + * @return int|null|false False if not cacheable, null for unlimited, int for specific lifetime |
| 65 | + */ |
| 66 | + private function resolveCacheLifetime(BlockInterface $block): int|null|false |
| 67 | + { |
| 68 | + if (!method_exists($block, 'getCacheLifetime')) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + $lifetimeRaw = $block->getCacheLifetime(); |
| 73 | + |
| 74 | + // In Magento: |
| 75 | + // - false = not cacheable |
| 76 | + // - null = unlimited cache (cacheable!) |
| 77 | + // - int = specific cache lifetime in seconds (cacheable!) |
| 78 | + |
| 79 | + if ($lifetimeRaw === false) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + if (is_int($lifetimeRaw)) { |
| 84 | + return $lifetimeRaw; |
| 85 | + } |
| 86 | + |
| 87 | + if ($lifetimeRaw === null) { |
| 88 | + return null; // Unlimited |
| 89 | + } |
| 90 | + |
| 91 | + if (is_numeric($lifetimeRaw) && (int)$lifetimeRaw === 0) { |
| 92 | + return null; // Unlimited |
| 93 | + } |
| 94 | + |
| 95 | + return false; // Default fallback |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Check if block is private (customer specific) |
| 100 | + * |
| 101 | + * @param BlockInterface $block |
| 102 | + * @return bool |
| 103 | + */ |
| 104 | + private function isBlockScopePrivate(BlockInterface $block): bool |
| 105 | + { |
| 106 | + // Private blocks (like checkout, customer account) should not be cached |
| 107 | + if (method_exists($block, 'isScopePrivate')) { |
| 108 | + if ($block->isScopePrivate()) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Additional fallback: Check protected property via reflection if available |
| 114 | + if (property_exists($block, '_isScopePrivate')) { |
| 115 | + try { |
| 116 | + $reflection = new \ReflectionProperty($block, '_isScopePrivate'); |
| 117 | + $reflection->setAccessible(true); |
| 118 | + $isScopePrivate = $reflection->getValue($block); |
| 119 | + if ($isScopePrivate === true) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + } catch (\ReflectionException $e) { |
| 123 | + // If reflection fails, assume not private |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Resolve cache key from block |
| 132 | + * |
| 133 | + * @param BlockInterface $block |
| 134 | + * @return string |
| 135 | + */ |
| 136 | + private function resolveCacheKey(BlockInterface $block): string |
| 137 | + { |
| 138 | + if (method_exists($block, 'getCacheKey')) { |
| 139 | + $keyRaw = $block->getCacheKey(); |
| 140 | + return is_string($keyRaw) && $keyRaw !== '' ? $keyRaw : ''; |
| 141 | + } |
| 142 | + return ''; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Resolve cache tags from block |
| 147 | + * |
| 148 | + * @param BlockInterface $block |
| 149 | + * @return array<int, string> |
| 150 | + */ |
| 151 | + private function resolveCacheTags(BlockInterface $block): array |
| 152 | + { |
| 153 | + $cacheTags = []; |
| 154 | + if (method_exists($block, 'getCacheTags')) { |
| 155 | + $tagsRaw = $block->getCacheTags(); |
| 156 | + // Ensure string array (PHPStan strict) |
| 157 | + if (is_array($tagsRaw)) { |
| 158 | + foreach ($tagsRaw as $tag) { |
| 159 | + if (is_string($tag)) { |
| 160 | + $cacheTags[] = $tag; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return $cacheTags; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Check if current page is cacheable |
| 170 | + * |
| 171 | + * Checks layout configuration to determine if page has cacheable="false" attribute. |
| 172 | + * If ANY block on the page is marked as non-cacheable in layout XML, the entire page is non-cacheable. |
| 173 | + * |
| 174 | + * @return bool True if page is cacheable, false otherwise |
| 175 | + */ |
| 176 | + private function isPageCacheable(): bool |
| 177 | + { |
| 178 | + try { |
| 179 | + // Get all blocks from layout |
| 180 | + $allBlocks = $this->layout->getAllBlocks(); |
| 181 | + |
| 182 | + foreach ($allBlocks as $block) { |
| 183 | + // Check if block has isCacheable method (added by layout processor) |
| 184 | + if (method_exists($block, 'isCacheable')) { |
| 185 | + // @phpstan-ignore-next-line |
| 186 | + if (!$block->isCacheable()) { |
| 187 | + return false; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Check data key 'cacheable' set by layout XML |
| 192 | + if (method_exists($block, 'getData')) { |
| 193 | + // @phpstan-ignore-next-line |
| 194 | + $cacheableData = $block->getData('cacheable'); |
| 195 | + if ($cacheableData === false || $cacheableData === 'false') { |
| 196 | + return false; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return true; |
| 202 | + } catch (\Exception $e) { |
| 203 | + // If we can't determine, assume cacheable to avoid false alarms |
| 204 | + return true; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Format metrics for JSON export to frontend |
| 210 | + * |
| 211 | + * @param array{renderTimeMs: float, startTime: int, endTime: int} $renderMetrics |
| 212 | + * @param array{cacheable: bool, lifetime: int|null, cacheKey: string, cacheTags: array<int, string>, pageCacheable: bool} $cacheMetrics |
| 213 | + * @return array{performance: array{renderTime: string, timestamp: int}, cache: array{cacheable: bool, lifetime: int|null, key: string, tags: array<int, string>, pageCacheable: bool}} |
| 214 | + */ |
| 215 | + public function formatMetricsForJson(array $renderMetrics, array $cacheMetrics): array |
| 216 | + { |
| 217 | + return [ |
| 218 | + 'performance' => [ |
| 219 | + 'renderTime' => number_format($renderMetrics['renderTimeMs'], 2), |
| 220 | + 'timestamp' => (int)($renderMetrics['startTime'] / 1_000_000_000), // Convert ns to seconds |
| 221 | + ], |
| 222 | + 'cache' => [ |
| 223 | + 'cacheable' => $cacheMetrics['cacheable'], |
| 224 | + 'lifetime' => $cacheMetrics['lifetime'], |
| 225 | + 'key' => $cacheMetrics['cacheKey'], |
| 226 | + 'tags' => $cacheMetrics['cacheTags'], |
| 227 | + 'pageCacheable' => $cacheMetrics['pageCacheable'], |
| 228 | + ], |
| 229 | + ]; |
| 230 | + } |
| 231 | +} |
0 commit comments