|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* spiral |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Spiral\Views\Loaders; |
|
9
|
|
|
|
|
10
|
|
|
use Spiral\Core\Component; |
|
11
|
|
|
use Spiral\Debug\Traits\BenchmarkTrait; |
|
12
|
|
|
use Spiral\Views\EnvironmentInterface; |
|
13
|
|
|
use Spiral\Views\LoaderInterface; |
|
14
|
|
|
use Spiral\Views\ViewSource; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provides ability to wrap another loader in a given enviroment and process all view sources |
|
18
|
|
|
* before giving them to engine. |
|
19
|
|
|
* |
|
20
|
|
|
* Modifiers executed on template source BEFORE providing it to engine. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ModifiableLoader extends Component implements LoaderInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use BenchmarkTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Loaded to be used for file resolution. |
|
28
|
|
|
* |
|
29
|
|
|
* @var LoaderInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $parent = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Required in order to give processors some context. |
|
35
|
|
|
* |
|
36
|
|
|
* @var EnvironmentInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $environment = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \Spiral\Views\ProcessorInterface[] |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $modifiers = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* ProcessableLoader constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param EnvironmentInterface $environment |
|
49
|
|
|
* @param LoaderInterface $loader |
|
50
|
|
|
* @param array $modifiers |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
EnvironmentInterface $environment, |
|
54
|
|
|
LoaderInterface $loader, |
|
55
|
|
|
array $modifiers = [] |
|
56
|
|
|
) { |
|
57
|
|
|
$this->environment = $environment; |
|
58
|
|
|
$this->parent = $loader; |
|
59
|
|
|
$this->modifiers = $modifiers; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getSource(string $path): ViewSource |
|
66
|
|
|
{ |
|
67
|
|
|
$source = $this->parent->getSource($path); |
|
68
|
|
|
|
|
69
|
|
|
foreach ($this->modifiers as $modifier) { |
|
70
|
|
|
$benchmark = $this->benchmark('process', $path . '@' . get_class($modifier)); |
|
71
|
|
|
try { |
|
72
|
|
|
$source = $source->withCode( |
|
73
|
|
|
$modifier->modify($this->environment, $source, $source->getCode()) |
|
74
|
|
|
); |
|
75
|
|
|
} finally { |
|
76
|
|
|
$this->benchmark($benchmark); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $source; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function exists(string $path): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->parent->exists($path); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getNamespaces(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->parent->getNamespaces(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function withExtension(string $extension = null): LoaderInterface |
|
103
|
|
|
{ |
|
104
|
|
|
$wrapper = clone $this; |
|
105
|
|
|
$wrapper->parent = $wrapper->parent->withExtension($extension); |
|
106
|
|
|
|
|
107
|
|
|
return $wrapper; |
|
108
|
|
|
} |
|
109
|
|
|
} |