|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Twig Component Handle Loader |
| 5 | + * |
| 6 | + * @copyright 2022 Dennis Morhardt <[email protected]> |
| 7 | + * @license MIT License; see LICENSE file for details. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Gglnx\TwigComponentHandleLoader\Loader; |
| 11 | + |
| 12 | +use RecursiveCallbackFilterIterator; |
| 13 | +use RecursiveDirectoryIterator; |
| 14 | +use RecursiveIteratorIterator; |
| 15 | +use Twig\Error\LoaderError; |
| 16 | +use Twig\Loader\LoaderInterface; |
| 17 | +use Twig\Source; |
| 18 | + |
| 19 | +/** |
| 20 | + * Add support for loading templates with '@component' handle syntax (like Fractal). |
| 21 | + * |
| 22 | + * @author Dennis Morhardt <[email protected]> |
| 23 | + * @package Gglnx\TwigComponentHandleLoader\Loader |
| 24 | + */ |
| 25 | +class ComponentHandleLoader implements LoaderInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * A path where to look for components. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $pathToComponents; |
| 33 | + |
| 34 | + /** |
| 35 | + * List of all components |
| 36 | + * |
| 37 | + * @var array |
| 38 | + */ |
| 39 | + private $components = []; |
| 40 | + |
| 41 | + /** |
| 42 | + * Inits this loader |
| 43 | + * |
| 44 | + * @param string $pathToComponents |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function __construct(string $pathToComponents) |
| 48 | + { |
| 49 | + $this->pathToComponents = $pathToComponents; |
| 50 | + $this->components = $this->getComponents(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritdoc |
| 55 | + */ |
| 56 | + public function getSourceContext($name) |
| 57 | + { |
| 58 | + $path = $this->findTemplate($name); |
| 59 | + |
| 60 | + return new Source(file_get_contents($path, true), $name, $path); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritdoc |
| 65 | + */ |
| 66 | + public function getCacheKey($name) |
| 67 | + { |
| 68 | + $path = $this->findTemplate($name); |
| 69 | + $len = strlen($this->pathToComponents); |
| 70 | + |
| 71 | + if (strncmp($this->pathToComponents, $path, $len) === 0) { |
| 72 | + return substr($path, $len); |
| 73 | + } |
| 74 | + |
| 75 | + return $path; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @inheritdoc |
| 80 | + */ |
| 81 | + public function isFresh($name, $time) |
| 82 | + { |
| 83 | + $path = $this->findTemplate($name); |
| 84 | + |
| 85 | + return filemtime($path) < $time; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @inheritdoc |
| 90 | + */ |
| 91 | + public function exists($name) |
| 92 | + { |
| 93 | + return $this->findTemplate($name, false) !== null; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Finds a template by at-name |
| 98 | + * |
| 99 | + * @param string $name The template logical name. |
| 100 | + * @param bool $throw Throw on errors. |
| 101 | + * @throws LoaderError When $name is not found or doesn't start with @. |
| 102 | + * @return string|null |
| 103 | + */ |
| 104 | + protected function findTemplate($name, $throw = true) |
| 105 | + { |
| 106 | + try { |
| 107 | + // Throw error if name doesn't start with '@' |
| 108 | + if ($name[0] !== '@') { |
| 109 | + throw new LoaderError(sprintf( |
| 110 | + 'Template name "%s" does not support with @ as needed for component handle.', |
| 111 | + $name |
| 112 | + )); |
| 113 | + } |
| 114 | + |
| 115 | + // Throw error if name includes a slash (we don't support namespaces) |
| 116 | + if (strpos($name, '/') !== false) { |
| 117 | + throw new LoaderError(sprintf( |
| 118 | + 'Template name "%s" includes an /, but namespaced are not supported.', |
| 119 | + $name |
| 120 | + )); |
| 121 | + } |
| 122 | + |
| 123 | + // Try to find component |
| 124 | + if (!isset($this->components[$name])) { |
| 125 | + throw new LoaderError(sprintf( |
| 126 | + 'Unable to find component "%s" (looked into: %s).', |
| 127 | + $name, |
| 128 | + $this->pathToComponents |
| 129 | + )); |
| 130 | + } |
| 131 | + } catch (LoaderError $exception) { |
| 132 | + if (!$throw) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + throw $exception; |
| 137 | + } |
| 138 | + |
| 139 | + // Get path to component |
| 140 | + return $this->components[$name]; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Loads all component paths |
| 145 | + * |
| 146 | + * @throws LoaderError When path to components doesn't exits. |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + private function getComponents() |
| 150 | + { |
| 151 | + // Check path to components |
| 152 | + if (!is_dir($this->pathToComponents)) { |
| 153 | + throw new LoaderError(sprintf( |
| 154 | + 'The "%s" directory does not exist.', |
| 155 | + $this->pathToComponents |
| 156 | + )); |
| 157 | + } |
| 158 | + |
| 159 | + // Parse through all component folders |
| 160 | + $directory = new RecursiveDirectoryIterator($this->pathToComponents); |
| 161 | + |
| 162 | + // Build filter for 'twig' files |
| 163 | + $files = new RecursiveCallbackFilterIterator( |
| 164 | + $directory, |
| 165 | + function ($current, $key, $iterator) { |
| 166 | + if ($iterator->hasChildren()) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + if (!$current->isFile()) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + if (pathinfo($key, PATHINFO_EXTENSION) !== 'twig') { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + return true; |
| 179 | + } |
| 180 | + ); |
| 181 | + |
| 182 | + // Get all matches ending with 'twig' |
| 183 | + $matches = array_map( |
| 184 | + function ($match) { |
| 185 | + return sprintf('@%s', pathinfo($match->getFilename(), PATHINFO_FILENAME)); |
| 186 | + }, |
| 187 | + iterator_to_array(new RecursiveIteratorIterator($files)) |
| 188 | + ); |
| 189 | + |
| 190 | + // Return all components |
| 191 | + return array_flip($matches); |
| 192 | + } |
| 193 | +} |
0 commit comments