|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Twig. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Twig\Cache; |
| 13 | + |
| 14 | +/** |
| 15 | + * Cache decorator that writes XDebug source map files alongside compiled templates. |
| 16 | + * |
| 17 | + * XDebug 3.5+ supports native path mapping via .map files in .xdebug directories. |
| 18 | + * This allows setting breakpoints in Twig templates and having XDebug map them |
| 19 | + * to the correct lines in the compiled PHP files. |
| 20 | + * |
| 21 | + * @author Fabien Potencier <fabien@symfony.com> |
| 22 | + * |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +final class XdebugSourceMapCache implements CacheInterface, RemovableCacheInterface |
| 26 | +{ |
| 27 | + private ?string $pendingTemplatePath = null; |
| 28 | + private ?array $pendingDebugInfo = null; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private DirectoryCacheInterface&CacheInterface $cache, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function setSourceMapData(string $templatePath, array $debugInfo): void |
| 36 | + { |
| 37 | + $this->pendingTemplatePath = $templatePath; |
| 38 | + $this->pendingDebugInfo = $debugInfo; |
| 39 | + } |
| 40 | + |
| 41 | + public function generateKey(string $name, string $className): string |
| 42 | + { |
| 43 | + return $this->cache->generateKey($name, $className); |
| 44 | + } |
| 45 | + |
| 46 | + public function write(string $key, string $content): void |
| 47 | + { |
| 48 | + $this->cache->write($key, $content); |
| 49 | + |
| 50 | + if ($this->pendingTemplatePath && $this->pendingDebugInfo) { |
| 51 | + try { |
| 52 | + $this->writeSourceMap($key, $this->pendingTemplatePath, $this->pendingDebugInfo); |
| 53 | + } finally { |
| 54 | + $this->pendingTemplatePath = null; |
| 55 | + $this->pendingDebugInfo = null; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function load(string $key): void |
| 61 | + { |
| 62 | + $this->cache->load($key); |
| 63 | + } |
| 64 | + |
| 65 | + public function getTimestamp(string $key): int |
| 66 | + { |
| 67 | + return $this->cache->getTimestamp($key); |
| 68 | + } |
| 69 | + |
| 70 | + public function remove(string $name, string $className): void |
| 71 | + { |
| 72 | + if ($this->cache instanceof RemovableCacheInterface) { |
| 73 | + $this->cache->remove($name, $className); |
| 74 | + } |
| 75 | + |
| 76 | + $this->removeSourceMap($this->cache->generateKey($name, $className)); |
| 77 | + } |
| 78 | + |
| 79 | + private function writeSourceMap(string $cacheKey, string $templatePath, array $debugInfo): void |
| 80 | + { |
| 81 | + $content = $this->buildMapContent($cacheKey, $templatePath, $debugInfo); |
| 82 | + $filename = pathinfo($cacheKey, \PATHINFO_FILENAME).'.map'; |
| 83 | + |
| 84 | + foreach ($this->cache->getDirectories() as $directory) { |
| 85 | + $mapPath = $directory.'/.xdebug'; |
| 86 | + |
| 87 | + if (!is_dir($mapPath)) { |
| 88 | + mkdir($mapPath, 0777, true); |
| 89 | + } |
| 90 | + |
| 91 | + $mapFile = $mapPath.'/'.$filename; |
| 92 | + $tmpFile = tempnam($mapPath, 'map'); |
| 93 | + if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $mapFile)) { |
| 94 | + @chmod($mapFile, 0666 & ~umask()); |
| 95 | + |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + throw new \RuntimeException(\sprintf('Failed to write XDebug source map file "%s".', $mapFile)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private function removeSourceMap(string $cacheKey): void |
| 104 | + { |
| 105 | + $filename = pathinfo($cacheKey, \PATHINFO_FILENAME).'.map'; |
| 106 | + |
| 107 | + foreach ($this->cache->getDirectories() as $directory) { |
| 108 | + $mapFile = $directory.'/.xdebug/'.$filename; |
| 109 | + if (is_file($mapFile)) { |
| 110 | + @unlink($mapFile); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private function buildMapContent(string $cacheKey, string $templatePath, array $debugInfo): string |
| 116 | + { |
| 117 | + $lines = "# XDebug source map for Twig template\n"; |
| 118 | + $lines .= \sprintf("remote_prefix: %s/\n", \dirname($cacheKey)); |
| 119 | + $lines .= \sprintf("local_prefix: %s/\n\n", \dirname($templatePath)); |
| 120 | + |
| 121 | + $compiledLines = array_keys($debugInfo); |
| 122 | + for ($i = 0, $count = \count($compiledLines); $i < $count; ++$i) { |
| 123 | + $startLine = $compiledLines[$i]; |
| 124 | + // End line is exclusive, so use next start line minus 1 |
| 125 | + // Use a reasonable max for last range (XDebug can't handle PHP_INT_MAX) |
| 126 | + $endLine = isset($compiledLines[$i + 1]) ? $compiledLines[$i + 1] - 1 : 999999; |
| 127 | + $lines .= \sprintf( |
| 128 | + "%s:%d-%d = %s:%d\n", |
| 129 | + basename($cacheKey), |
| 130 | + $startLine, |
| 131 | + $endLine, |
| 132 | + basename($templatePath), |
| 133 | + $debugInfo[$startLine], |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return $lines; |
| 138 | + } |
| 139 | +} |
0 commit comments