1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of WebHelper Parser. |
5
|
|
|
* |
6
|
|
|
* (c) James <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WebHelper\Parser\Directive; |
13
|
|
|
|
14
|
|
|
use Webmozart\Glob\Iterator\GlobIterator; |
15
|
|
|
use WebHelper\Parser\Compiler; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Describes an inclusion directive instance. |
19
|
|
|
* |
20
|
|
|
* An inclusion directive points to a filesystem pathname. |
21
|
|
|
* |
22
|
|
|
* This pathname can be a file to load at the point of the active config, or |
23
|
|
|
* a list of files in a filesystem directory, using wildcards. |
24
|
|
|
* |
25
|
|
|
* Pathname can be absolute, or relative to a filesystem path defined by another Directive, or |
26
|
|
|
* to the path of the actual configuration file, or to the prefix. |
27
|
|
|
* |
28
|
|
|
* Technically, an inclusion directive is a simple directive that acts as a block directive. |
29
|
|
|
* |
30
|
|
|
* @author James <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class InclusionDirective extends BlockDirective implements DirectiveInterface |
33
|
|
|
{ |
34
|
|
|
/** @var \WebHelper\Parser\Compiler the compiler instance */ |
35
|
|
|
private $compiler; |
36
|
|
|
|
37
|
|
|
/** @var array file list pointed with that directive */ |
38
|
|
|
private $files = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Specific constructor for inclusion directives. |
42
|
|
|
* |
43
|
|
|
* @param string $name the name of the key/context |
44
|
|
|
* @param string $value the value of the key/context |
45
|
|
|
* @param \WebHelper\Parser\Compiler $compiler the compiler instance |
46
|
|
|
*/ |
47
|
4 |
|
public function __construct($name, $value, Compiler $compiler) |
48
|
|
|
{ |
49
|
4 |
|
parent::__construct($name, $value); |
50
|
4 |
|
$this->compiler = $compiler; |
51
|
4 |
|
$this->setFiles(); |
52
|
4 |
|
$this->compileFiles(); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets the detected files of the directive. |
57
|
|
|
* |
58
|
|
|
* @return array detected files |
59
|
|
|
*/ |
60
|
3 |
|
public function getFiles() |
61
|
|
|
{ |
62
|
3 |
|
return $this->files; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Detects and memoizes the included files. |
67
|
|
|
*/ |
68
|
4 |
|
public function setFiles() |
69
|
|
|
{ |
70
|
4 |
|
$this->files = []; |
71
|
4 |
|
$path = $this->getValue(); |
72
|
|
|
|
73
|
4 |
|
if (!preg_match('#^/#', $path)) { |
74
|
2 |
|
$path = $this->compiler->getPrefix().'/'.$path; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
4 |
|
$iterator = new GlobIterator($path); |
78
|
4 |
|
foreach ($iterator as $path) { |
79
|
3 |
|
$this->files[] = $path; |
80
|
4 |
|
} |
81
|
|
|
|
82
|
4 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
public function compileFiles() |
86
|
|
|
{ |
87
|
4 |
|
foreach ($this->files as $file) { |
88
|
3 |
|
$directive = $this->compiler->doCompile([], $this->getName(), $file); |
89
|
3 |
|
$this->add($directive); |
90
|
4 |
|
} |
91
|
4 |
|
} |
92
|
|
|
} |
93
|
|
|
|