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 WebHelper\Parser\Parser; |
15
|
|
|
use WebHelper\Parser\Server\ServerInterface; |
16
|
|
|
use Webmozart\Glob\Iterator\GlobIterator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Describes an inclusion directive instance. |
20
|
|
|
* |
21
|
|
|
* An inclusion directive points to a filesystem pathname. |
22
|
|
|
* |
23
|
|
|
* This pathname can be a file to load at the point of the active config, or |
24
|
|
|
* a list of files in a filesystem directory, using wildcards. |
25
|
|
|
* |
26
|
|
|
* Pathname can be absolute, or relative to a filesystem path defined by another Directive, or |
27
|
|
|
* to the path of the actual configuration file, or to the prefix. |
28
|
|
|
* |
29
|
|
|
* Technically, an inclusion directive is a simple directive that acts as a block directive. |
30
|
|
|
* |
31
|
|
|
* @author James <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class InclusionDirective extends BlockDirective implements DirectiveInterface |
34
|
|
|
{ |
35
|
|
|
/** @var \WebHelper\Parser\Parser the parser instance */ |
36
|
|
|
private $parser; |
37
|
|
|
|
38
|
|
|
/** @var array file list pointed with that directive */ |
39
|
|
|
private $files = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Specific constructor for inclusion directives. |
43
|
|
|
* |
44
|
|
|
* @param string $name the name of the key/context |
45
|
|
|
* @param string $value the value of the key/context |
46
|
|
|
* @param \WebHelper\Parser\Parser $parser the parser instance |
47
|
|
|
*/ |
48
|
4 |
|
public function __construct($name, $value, Parser $parser) |
49
|
|
|
{ |
50
|
4 |
|
parent::__construct($name, $value); |
51
|
4 |
|
$this->parser = $parser; |
52
|
4 |
|
$this->setFiles(); |
53
|
4 |
|
$this->compileFiles(); |
54
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets the detected files of the directive. |
58
|
|
|
* |
59
|
|
|
* @return array detected files |
60
|
|
|
*/ |
61
|
3 |
|
public function getFiles() |
62
|
|
|
{ |
63
|
3 |
|
return $this->files; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Detects and memoizes the included files. |
68
|
|
|
*/ |
69
|
4 |
|
public function setFiles() |
70
|
|
|
{ |
71
|
4 |
|
$this->files = []; |
72
|
4 |
|
$path = $this->getValue(); |
73
|
|
|
|
74
|
4 |
|
if (!preg_match('#^/#', $path)) { |
75
|
2 |
|
$path = $this->parser->getServer()->getPrefix().'/'.$path; |
76
|
2 |
|
} |
77
|
|
|
|
78
|
4 |
|
$iterator = new GlobIterator($path); |
79
|
4 |
|
foreach ($iterator as $path) { |
80
|
4 |
|
$this->files[] = $path; |
81
|
4 |
|
} |
82
|
|
|
|
83
|
4 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fills the block directives by compiling the memoized files. |
88
|
|
|
*/ |
89
|
4 |
|
public function compileFiles() |
90
|
|
|
{ |
91
|
4 |
|
foreach ($this->files as $file) { |
92
|
4 |
|
$activeConfig = $this->parser->setConfigFile($file)->getActiveConfig(); |
93
|
4 |
|
$this->add($activeConfig); |
94
|
4 |
|
} |
95
|
|
|
|
96
|
4 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Dumps the directive respecting a server syntax. |
101
|
|
|
* |
102
|
|
|
* @param ServerInterface $server a server instance |
103
|
|
|
* @param int $spaces the indentation spaces |
104
|
|
|
* |
105
|
|
|
* @return string the dumped directive |
106
|
|
|
*/ |
107
|
1 |
|
public function dump(ServerInterface $server, $spaces = 0) |
108
|
|
|
{ |
109
|
1 |
|
return $this->dumpSimple($server, $spaces); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|