Passed
Push — master ( b292e6...c5f4fa )
by butschster
07:03
created

StemplerBootloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 8
eloc 40
dl 0
loc 88
ccs 14
cts 20
cp 0.7
rs 10
c 0
b 0
f 0

6 Methods

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