OrkaRenderer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 60
dl 0
loc 122
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCachedFilePath() 0 3 1
A __construct() 0 10 1
A renderFile() 0 18 4
A parseContent() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\View;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Support\Directory;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Support\Directory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Valkyrja\View\Exception\RuntimeException;
19
20
use function array_keys;
21
use function file_get_contents;
22
use function file_put_contents;
23
use function is_file;
24
use function md5;
25
use function preg_replace;
26
27
/**
28
 * Class OrkaRenderer.
29
 *
30
 * @author Melech Mizrachi
31
 */
32
class OrkaRenderer extends PhpRenderer
33
{
34
    /**
35
     * @var array<string, string>
36
     */
37
    protected static array $replace = [
38
        // @layout
39
        '/@layout\s*\(\s*(.*)\s*\)/x'              => '<?php $template->setLayout(${1}); ?>',
40
        '/@block\s*\(\s*(.*)\s*\)/x'               => '<?= $template->getBlock(${1}); ?>',
41
        '/@trimblock\s*\(\s*(.*)\s*\)/x'           => '<?= trim($template->getBlock(${1})); ?>',
42
        '/@startblock\s*\(\s*(.*)\s*\)/x'          => '<?php $template->startBlock(${1}); ?>',
43
        '/@endblock/x'                             => '<?php $template->endBlock(); ?>',
44
        '/@hasblock\s*\(\s*(.*)\s*\)/x'            => '<?php if ($template->hasBlock(${1})) : ?>',
45
        '/@elsehasblock/x'                         => '<?php else : ?>',
46
        '/@elseifhasblock\s*\(\s*(.*)\s*\)/x'      => '<?php elseif ($template->hasBlock(${1})) : ?>',
47
        '/@endhasblock/x'                          => '<?php endif ?>',
48
        '/@unlessblock\s*\(\s*(.*)\s*\)/x'         => '<?php if (! $template->hasBlock(${1})) : ?>',
49
        '/@endunlessblock/x'                       => '<?php endif ?>',
50
        '/@partial\s*\(\s*(.*)\s*\s*\)/x'          => '<?= $template->getPartial(${1}); ?>',
51
        '/@partial\s*\(\s*(.*)\s*,(.*)\s*\)/x'     => '<?= $template->getPartial(${1}, ${2}); ?>',
52
        '/@trimpartial\s*\(\s*(.*)\s*\s*\)/x'      => '<?= trim($template->getPartial(${1})); ?>',
53
        '/@trimpartial\s*\(\s*(.*)\s*,(.*)\s*\)/x' => '<?= trim($template->getPartial(${1}, ${2})); ?>',
54
        '/@setvariables\s*\(\s*(.*)\s*\s*\)/x'     => '<?php $template->setVariables(${1}); ?>',
55
        '/@setvariable\s*\(\s*(.*)\s*,(.*)\s*\)/x' => '<?php $template->setVariable(${1}, ${2}); ?>',
56
        '/@empty\s*\(\s*(.*)\s*\)/x'               => '<?php if (empty(${1})) : ?>',
57
        '/@notempty\s*\(\s*(.*)\s*\)/x'            => '<?php if (! empty(${1})) : ?>',
58
        '/@if\s*\(\s*(.*)\s*\)/x'                  => '<?php if (${1}) : ?>',
59
        '/@elseif\s*\(\s*(.*)\s*\)/x'              => '<?php elseif (${1}) : ?>',
60
        '/@else/x'                                 => '<?php else : ?>',
61
        '/@endif/x'                                => '<?php endif ?>',
62
        '/@isset\s*\(\s*(.*)\s*\)/x'               => '<?php if (isset(${1})) : ?>',
63
        '/@endisset/x'                             => '<?php endif ?>',
64
        '/@unless\s*\(\s*(.*)\s*\)/x'              => '<?php if (! (${1})) : ?>',
65
        '/@elseunless\s*\(\s*(.*)\s*\)/x'          => '<?php elseif (! (${1})) : ?>',
66
        '/@endunless/x'                            => '<?php endif ?>',
67
        '/@foreach\s*\(\s*(.*)\s*\)/x'             => '<?php foreach (${1}) : ?>',
68
        '/@endforeach/x'                           => '<?php endforeach ?>',
69
        '/@for\s*\(\s*(.*)\s*\)/x'                 => '<?php for (${1}) : ?>',
70
        '/@endfor/x'                               => '<?php endfor ?>',
71
        '/@switch\s*\(\s*(.*)\s*\)/x'              => '<?php switch (${1}) :',
72
        '/@firstcase\s*\(\s*(.*)\s*\)/x'           => 'case ${1} : ?>',
73
        '/@case\s*\(\s*(.*)\s*\)/x'                => '<?php case ${1} : ?>',
74
        '/@break/x'                                => '<?php break; ?>',
75
        '/@default/x'                              => '<?php default : ?>',
76
        '/@endswitch/x'                            => '<?php endswitch ?>',
77
        '/@dd\s*\(\s*(.*)\s*\s*\)/x'               => '<?php \Valkyrja\dd(${1}); ?>',
78
        // {{--
79
        '/\{\{\-\-/x'                              => '<?php /** ?>',
80
        // --}}
81
        '/\-\-\}\}/x'                              => '<?php */ ?>',
82
        // {{{ unescaped }}}
83
        '/\{\{\{\s*(.*?)\s*\}\}\}/x'               => '<?= ${1}; ?>',
84
        // {{ escaped }}
85
        '/\{\{\s*(.*?)\s*\}\}/x'                   => '<?= $template->escape(${1}); ?>',
86
    ];
87
88
    /**
89
     * OrkaRenderer constructor.
90
     *
91
     * @param array<string, string> $paths
92
     */
93
    public function __construct(
94
        string $dir,
95
        string $fileExtension = '.orka.phtml',
96
        array $paths = [],
97
        protected bool $debug = false,
98
    ) {
99
        parent::__construct(
100
            dir: $dir,
101
            fileExtension: $fileExtension,
102
            paths: $paths,
103
        );
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    #[Override]
110
    public function renderFile(string $name, array $variables = []): string
111
    {
112
        $cachedPath = $this->getCachedFilePath($name);
113
114
        if ($this->debug || ! is_file($cachedPath)) {
115
            $fileContents = file_get_contents($this->getFullPath($name));
116
117
            if ($fileContents === false) {
118
                throw new RuntimeException("Contents for file $name could not be retrieved");
119
            }
120
121
            $contents = $this->parseContent($fileContents);
122
123
            file_put_contents($cachedPath, $contents);
124
        }
125
126
        return $this->renderFullPath($cachedPath, $variables);
127
    }
128
129
    /**
130
     * Parse okra written content to PHP parseable.
131
     *
132
     * @param string $contents The contents to parse
133
     *
134
     * @return string
135
     */
136
    protected function parseContent(string $contents): string
137
    {
138
        /** @var non-empty-string[] $regexes */
139
        $regexes = array_keys(self::$replace);
140
141
        return preg_replace($regexes, self::$replace, $contents) ?? $contents;
142
    }
143
144
    /**
145
     * Get the cached file path.
146
     *
147
     * @param string $name The name
148
     *
149
     * @return string
150
     */
151
    protected function getCachedFilePath(string $name): string
152
    {
153
        return Directory::storagePath('views/' . md5($name));
154
    }
155
}
156