Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 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\Extension; |
| 13 | |
| 14 | use Twig\NodeVisitor\NodeVisitorInterface; |
| 15 | use Twig\TokenParser\TokenParserInterface; |
| 16 | use Twig\TwigFilter; |
| 17 | use Twig\TwigFunction; |
| 18 | use Twig\TwigTest; |
| 19 | |
| 20 | /** |
| 21 | * Used by \Twig\Environment as a staging area. |
| 22 | * |
| 23 | * @author Fabien Potencier <fabien@symfony.com> |
| 24 | * |
| 25 | * @internal |
| 26 | */ |
| 27 | final class StagingExtension extends AbstractExtension |
| 28 | { |
| 29 | private $functions = []; |
| 30 | private $filters = []; |
| 31 | private $visitors = []; |
| 32 | private $tokenParsers = []; |
| 33 | private $tests = []; |
| 34 | |
| 35 | public function addFunction(TwigFunction $function): void |
| 36 | { |
| 37 | if (isset($this->functions[$function->getName()])) { |
| 38 | throw new \LogicException(sprintf('Function "%s" is already registered.', $function->getName())); |
| 39 | } |
| 40 | |
| 41 | $this->functions[$function->getName()] = $function; |
| 42 | } |
| 43 | |
| 44 | public function getFunctions(): array |
| 45 | { |
| 46 | return $this->functions; |
| 47 | } |
| 48 | |
| 49 | public function addFilter(TwigFilter $filter): void |
| 50 | { |
| 51 | if (isset($this->filters[$filter->getName()])) { |
| 52 | throw new \LogicException(sprintf('Filter "%s" is already registered.', $filter->getName())); |
| 53 | } |
| 54 | |
| 55 | $this->filters[$filter->getName()] = $filter; |
| 56 | } |
| 57 | |
| 58 | public function getFilters(): array |
| 59 | { |
| 60 | return $this->filters; |
| 61 | } |
| 62 | |
| 63 | public function addNodeVisitor(NodeVisitorInterface $visitor): void |
| 64 | { |
| 65 | $this->visitors[] = $visitor; |
| 66 | } |
| 67 | |
| 68 | public function getNodeVisitors(): array |
| 69 | { |
| 70 | return $this->visitors; |
| 71 | } |
| 72 | |
| 73 | public function addTokenParser(TokenParserInterface $parser): void |
| 74 | { |
| 75 | if (isset($this->tokenParsers[$parser->getTag()])) { |
| 76 | throw new \LogicException(sprintf('Tag "%s" is already registered.', $parser->getTag())); |
| 77 | } |
| 78 | |
| 79 | $this->tokenParsers[$parser->getTag()] = $parser; |
| 80 | } |
| 81 | |
| 82 | public function getTokenParsers(): array |
| 83 | { |
| 84 | return $this->tokenParsers; |
| 85 | } |
| 86 | |
| 87 | public function addTest(TwigTest $test): void |
| 88 | { |
| 89 | if (isset($this->tests[$test->getName()])) { |
| 90 | throw new \LogicException(sprintf('Test "%s" is already registered.', $test->getName())); |
| 91 | } |
| 92 | |
| 93 | $this->tests[$test->getName()] = $test; |
| 94 | } |
| 95 | |
| 96 | public function getTests(): array |
| 97 | { |
| 98 | return $this->tests; |
| 99 | } |
| 100 | } |