| 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 |
namespace ElementorDeps\Twig\Loader; |
| 12 |
|
| 13 |
use ElementorDeps\Twig\Error\LoaderError; |
| 14 |
use ElementorDeps\Twig\Source; |
| 15 |
/** |
| 16 |
* Loads a template from an array. |
| 17 |
* |
| 18 |
* When using this loader with a cache mechanism, you should know that a new cache |
| 19 |
* key is generated each time a template content "changes" (the cache key being the |
| 20 |
* source code of the template). If you don't want to see your cache grows out of |
| 21 |
* control, you need to take care of clearing the old cache file by yourself. |
| 22 |
* |
| 23 |
* This loader should only be used for unit testing. |
| 24 |
* |
| 25 |
* @author Fabien Potencier <fabien@symfony.com> |
| 26 |
*/ |
| 27 |
final class ArrayLoader implements LoaderInterface |
| 28 |
{ |
| 29 |
private $templates = []; |
| 30 |
/** |
| 31 |
* @param array $templates An array of templates (keys are the names, and values are the source code) |
| 32 |
*/ |
| 33 |
public function __construct(array $templates = []) |
| 34 |
{ |
| 35 |
$this->templates = $templates; |
| 36 |
} |
| 37 |
public function setTemplate(string $name, string $template) : void |
| 38 |
{ |
| 39 |
$this->templates[$name] = $template; |
| 40 |
} |
| 41 |
public function getSourceContext(string $name) : Source |
| 42 |
{ |
| 43 |
if (!isset($this->templates[$name])) { |
| 44 |
throw new LoaderError(\sprintf('Template "%s" is not defined.', $name)); |
| 45 |
} |
| 46 |
return new Source($this->templates[$name], $name); |
| 47 |
} |
| 48 |
public function exists(string $name) : bool |
| 49 |
{ |
| 50 |
return isset($this->templates[$name]); |
| 51 |
} |
| 52 |
public function getCacheKey(string $name) : string |
| 53 |
{ |
| 54 |
if (!isset($this->templates[$name])) { |
| 55 |
throw new LoaderError(\sprintf('Template "%s" is not defined.', $name)); |
| 56 |
} |
| 57 |
return $name . ':' . $this->templates[$name]; |
| 58 |
} |
| 59 |
public function isFresh(string $name, int $time) : bool |
| 60 |
{ |
| 61 |
if (!isset($this->templates[$name])) { |
| 62 |
throw new LoaderError(\sprintf('Template "%s" is not defined.', $name)); |
| 63 |
} |
| 64 |
return \true; |
| 65 |
} |
| 66 |
} |
| 67 |
|