Passed
Push — main ( 902994...f9905b )
by Dimitri
03:03
created

BladeAdapter::configure()   B

Complexity

Conditions 8
Paths 27

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 12
nc 27
nop 0
dl 0
loc 22
ccs 0
cts 6
cp 0
crap 72
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\View\Adapters;
13
14
use Jenssegers\Blade\Blade;
0 ignored issues
show
Bug introduced by
The type Jenssegers\Blade\Blade 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...
15
16
class BladeAdapter extends AbstractAdapter
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    protected string $ext = 'blade.php';
22
23
    /**
24
     * Instance Blade
25
     *
26
     * @var Blade
27
     */
28
    private $engine;
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function __construct(protected array $config, $viewPathLocator = null, protected bool $debug = BLITZ_DEBUG)
34
    {
35
        parent::__construct($config, $viewPathLocator, $debug);
36
37
        $this->engine = new Blade(
38
            $this->viewPath ?: VIEW_PATH,
39
            $this->config['cache_path'] ?? VIEW_CACHE_PATH . 'blade' . DIRECTORY_SEPARATOR
40
        );
41
42
        $this->configure();
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function render(string $view, ?array $options = null, ?bool $saveData = null): string
49
    {
50
        $view = str_replace([$this->viewPath, ' '], '', $view);
51
52
        $this->renderVars['start'] = microtime(true);
53
54
        $this->renderVars['view']    = $view;
55
        $this->renderVars['options'] = $options ?? [];
56
57
        $this->renderVars['file'] = $this->getRenderedFile($options, $this->renderVars['view'], $this->ext);
58
59
        $output = $this->engine->render($this->renderVars['view'], $this->data);
60
61
        $this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']);
0 ignored issues
show
Bug introduced by
It seems like $this->renderVars['start'] can also be of type string; however, parameter $start of BlitzPHP\View\Adapters\A...apter::logPerformance() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $this->logPerformance(/** @scrutinizer ignore-type */ $this->renderVars['start'], microtime(true), $this->renderVars['view']);
Loading history...
Bug introduced by
It seems like microtime(true) can also be of type string; however, parameter $end of BlitzPHP\View\Adapters\A...apter::logPerformance() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $this->logPerformance($this->renderVars['start'], /** @scrutinizer ignore-type */ microtime(true), $this->renderVars['view']);
Loading history...
62
63
        return $this->decorate($output);
64
    }
65
66
    /**
67
     * Configure le moteur de template
68
     */
69
    private function configure(): void
70
    {
71
        if (isset($this->config['configure']) && is_callable($this->config['configure'])) {
72
            $newInstance = $this->config['configure']($this->engine);
73
            if ($newInstance instanceof Blade) {
74
                $this->engine = $newInstance;
75
            }
76
        }
77
78
        $directives = (array) ($this->config['directives'] ?? []);
79
80
        foreach ($directives as $name => $callable) {
81
            if (is_callable($callable)) {
82
                $this->engine->directive($name, $callable);
83
            }
84
        }
85
86
        $if = (array) ($this->config['if'] ?? []);
87
88
        foreach ($if as $name => $callable) {
89
            if (is_callable($callable)) {
90
                $this->engine->if($name, $callable);
91
            }
92
        }
93
    }
94
}
95