1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Micro framework package. |
||
5 | * |
||
6 | * (c) Stanislau Komar <[email protected]> |
||
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 Micro\Plugin\Twig\Business\Loader; |
||
13 | |||
14 | use Micro\Plugin\Twig\Plugin\TwigTemplatePluginInterface; |
||
15 | use Twig\Environment; |
||
16 | use Twig\Error\LoaderError; |
||
17 | use Twig\Loader\FilesystemLoader; |
||
18 | |||
19 | class TemplateLoader implements LoaderInterface |
||
20 | { |
||
21 | /** |
||
22 | * @throws LoaderError |
||
23 | */ |
||
24 | 2 | public function load(Environment $environment, object $plugin): void |
|
25 | { |
||
26 | 2 | if (!($plugin instanceof TwigTemplatePluginInterface)) { |
|
27 | 2 | return; |
|
28 | } |
||
29 | |||
30 | /** @var FilesystemLoader $loader */ |
||
31 | 2 | $loader = $environment->getLoader(); |
|
32 | 2 | if (!($loader instanceof FilesystemLoader)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
33 | 1 | throw new \InvalidArgumentException(sprintf('The loader supports only %s', FilesystemLoader::class)); |
|
34 | } |
||
35 | |||
36 | 1 | $namespace = $plugin->getTwigNamespace(); |
|
37 | 1 | $paths = $plugin->getTwigTemplatePaths(); |
|
38 | |||
39 | 1 | $this->registerTemplates($loader, $paths, $namespace); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param string[] $paths |
||
44 | * |
||
45 | * @throws LoaderError |
||
46 | */ |
||
47 | 1 | protected function registerTemplates(FilesystemLoader $loader, array $paths, string $namespace = null): void |
|
48 | { |
||
49 | 1 | if (null === $namespace) { |
|
50 | 1 | $namespace = FilesystemLoader::MAIN_NAMESPACE; |
|
51 | } |
||
52 | |||
53 | 1 | foreach ($paths as $path) { |
|
54 | 1 | $loader->addPath($path, $namespace); |
|
55 | } |
||
56 | } |
||
57 | } |
||
58 |