Passed
Push — master ( a2340d...9d7d2b )
by butschster
06:32
created

StemplerBootloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 8
eloc 42
dl 0
loc 90
ccs 48
cts 58
cp 0.8276
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A stemplerEngine() 0 11 2
A __construct() 0 3 1
A addDirective() 0 5 1
A addProcessor() 0 5 1
A init() 0 41 2
A addVisitor() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Stempler\Bootloader;
6
7
use Psr\Container\ContainerInterface;
8
use Spiral\Boot\Bootloader\Bootloader;
9
use Spiral\Config\ConfiguratorInterface;
10
use Spiral\Config\Patch\Append;
11
use Spiral\Core\Container\Autowire;
12
use Spiral\Core\Container\SingletonInterface;
13
use Spiral\Stempler\Builder;
14
use Spiral\Stempler\Config\StemplerConfig;
15
use Spiral\Stempler\Directive;
16
use Spiral\Stempler\Processor\NullLocaleProcessor;
17
use Spiral\Stempler\StemplerCache;
18
use Spiral\Stempler\StemplerEngine;
19
use Spiral\Stempler\Transform\Finalizer;
20
use Spiral\Stempler\Transform\Visitor;
21
use Spiral\Stempler\VisitorInterface;
22
use Spiral\Translator\Views\LocaleProcessor;
23
use Spiral\Views\Bootloader\ViewsBootloader;
24
use Spiral\Views\Config\ViewsConfig;
25
use Spiral\Views\Processor;
26
use Spiral\Views\ProcessorInterface;
27
28
/**
29
 * Initiates stempler engine, it's cache and directives.
30
 */
31
final class StemplerBootloader extends Bootloader implements SingletonInterface
32
{
33
    protected const SINGLETONS = [
34
        StemplerEngine::class => [self::class, 'stemplerEngine'],
35
    ];
36
37 269
    public function __construct(
38
        private readonly ConfiguratorInterface $config
39
    ) {
40 269
    }
41
42 269
    public function init(ContainerInterface $container, ViewsBootloader $views): void
43
    {
44 269
        $this->config->setDefaults(
45 269
            StemplerConfig::CONFIG,
46 269
            [
47 269
                'directives' => [
48 269
                    Directive\PHPDirective::class,
49 269
                    Directive\RouteDirective::class,
50 269
                    Directive\LoopDirective::class,
51 269
                    Directive\JsonDirective::class,
52 269
                    Directive\ConditionalDirective::class,
53 269
                    Directive\ContainerDirective::class,
54 269
                ],
55 269
                'processors' => [
56 269
                    Processor\ContextProcessor::class,
57 269
                ],
58 269
                'visitors' => [
59 269
                    Builder::STAGE_PREPARE => [
60 269
                        Visitor\DefineBlocks::class,
61 269
                        Visitor\DefineAttributes::class,
62 269
                        Visitor\DefineHidden::class,
63 269
                    ],
64 269
                    Builder::STAGE_TRANSFORM => [
65
66 269
                    ],
67 269
                    Builder::STAGE_FINALIZE => [
68 269
                        Visitor\DefineStacks::class,
69 269
                        Finalizer\StackCollector::class,
70 269
                    ],
71 269
                    Builder::STAGE_COMPILE => [
72 269
                    ],
73 269
                ],
74 269
            ]
75 269
        );
76
77 269
        $views->addEngine(StemplerEngine::class);
78
79 269
        if ($container->has(LocaleProcessor::class)) {
80 267
            $this->addProcessor(LocaleProcessor::class);
81
        } else {
82 2
            $this->addProcessor(NullLocaleProcessor::class);
83
        }
84
    }
85
86
    public function addDirective(string|Autowire|Directive\DirectiveRendererInterface $directive): void
87
    {
88
        $this->config->modify(
89
            StemplerConfig::CONFIG,
90
            new Append('directives', null, $directive)
91
        );
92
    }
93
94 269
    public function addProcessor(string|Autowire|ProcessorInterface $processor): void
95
    {
96 269
        $this->config->modify(
97 269
            StemplerConfig::CONFIG,
98 269
            new Append('processors', null, $processor)
99 269
        );
100
    }
101
102
    public function addVisitor(string|Autowire|VisitorInterface $visitor, int $stage = Builder::STAGE_COMPILE): void
103
    {
104
        $this->config->modify(
105
            StemplerConfig::CONFIG,
106
            new Append('visitors.' . $stage, null, $visitor)
107
        );
108
    }
109
110 21
    protected function stemplerEngine(
111
        ContainerInterface $container,
112
        StemplerConfig $config,
113
        ViewsConfig $viewConfig
114
    ): StemplerEngine {
115 21
        $cache = null;
116 21
        if ($viewConfig->isCacheEnabled()) {
117 2
            $cache = new StemplerCache($viewConfig->getCacheDirectory());
118
        }
119
120 21
        return new StemplerEngine($container, $config, $cache);
121
    }
122
}
123