|
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; |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
105
|
|
|
new \Twig_Loader_Filesystem(\array_keys($this->viewsDirs)), |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths