Twig::initTwig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the PHALCON-EXT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace PhalconExt\View;
13
14
use Phalcon\Mvc\View\Engine;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\View\Engine 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
/**
17
 * Twig engine for Phalcon.
18
 *
19
 * @author  Jitendra Adhikari <[email protected]>
20
 * @license MIT
21
 *
22
 * @link    https://github.com/adhocore/phalcon-ext
23
 */
24
class Twig extends Engine
25
{
26
    /** @var \Twig_Environment */
27
    protected $twig;
28
29
    /** @var array */
30
    protected $viewsDirs;
31
32
    /**
33
     * Renders a view using the twig template engine.
34
     *
35
     * @param string $path      View path
36
     * @param array  $params    View params
37
     * @param bool   $mustClean [<internal>]
38
     *
39
     * @return string Rendered content
40
     */
41
    public function render($path, $params = [], $mustClean = false)
42
    {
43
        $this->initTwig();
44
45
        $content = $this->twig->render($this->normalizePath($path), empty($params) ? [] : (array) $params);
46
47
        $this->_view->setContent($content);
48
49
        return $content;
50
    }
51
52
    /**
53
     * Renders a view block using the twig template engine.
54
     *
55
     * @param string $path   View path
56
     * @param string $block  Block name
57
     * @param array  $params View params
58
     *
59
     * @return string Rendered block content
60
     */
61
    public function renderBlock(string $path, string $block, array $params = []): string
62
    {
63
        $this->initTwig();
64
65
        return $this->twig->loadTemplate($path . '.twig')->renderBlock($block, $params) ?: '';
66
    }
67
68
    /**
69
     * Adapt path to be twig friendly.
70
     *
71
     * @param string $path
72
     *
73
     * @return string
74
     */
75
    protected function normalizePath(string $path): string
76
    {
77
        foreach ($this->viewsDirs as $dir => $len) {
78
            if (\strpos($path, $dir) === 0) {
79
                return \substr($path, $len);
80
            }
81
        }
82
83
        return $path;
84
    }
85
86
    /**
87
     * Initialize twig once and for all.
88
     *
89
     * @return void
90
     */
91
    protected function initTwig()
92
    {
93
        if ($this->twig) {
94
            return;
95
        }
96
97
        $config = $this->getDI()->get('config')->toArray()['twig'];
98
99
        $this->viewsDirs = \array_combine(
100
            $config['view_dirs'],
101
            \array_map('strlen', $config['view_dirs'])
102
        );
103
104
        $this->twig = new \Twig_Environment(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated: since Twig 2.7, use "Twig\Environment" instead ( Ignorable by Annotation )

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

104
        $this->twig = /** @scrutinizer ignore-deprecated */ new \Twig_Environment(
Loading history...
105
            new \Twig_Loader_Filesystem(\array_keys($this->viewsDirs)),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_Filesystem has been deprecated: since Twig 2.7, use "Twig\Loader\FilesystemLoader" instead ( Ignorable by Annotation )

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

105
            /** @scrutinizer ignore-deprecated */ new \Twig_Loader_Filesystem(\array_keys($this->viewsDirs)),
Loading history...
106
            $config
107
        );
108
    }
109
110
    /**
111
     * Delegate calls to twig.
112
     *
113
     * @param string $method
114
     * @param array  $args
115
     *
116
     * @return mixed
117
     */
118
    public function __call(string $method, array $args = [])
119
    {
120
        $this->initTwig();
121
122
        return $this->twig->$method(...$args);
123
    }
124
}
125