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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Describes an inclusion directive instance. |
18
|
|
|
* |
19
|
|
|
* An inclusion directive points to a filesystem pathname. |
20
|
|
|
* |
21
|
|
|
* This pathname can be a file to load at the point of the active config, or |
22
|
|
|
* a list of files in a filesystem directory, using wildcards. |
23
|
|
|
* |
24
|
|
|
* Pathname can be absolute, or relative to a filesystem path defined by another Directive, or |
25
|
|
|
* to the path of the actual configuration file, or to the prefix. |
26
|
|
|
* |
27
|
|
|
* Technically, an inclusion directive is a simple directive that acts as a block directive. |
28
|
|
|
* |
29
|
|
|
* @author James <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class InclusionDirective extends BlockDirective implements DirectiveInterface |
32
|
|
|
{ |
33
|
|
|
/** @var string the filesystem path where the web server is installed */ |
34
|
|
|
private $prefix; |
35
|
|
|
|
36
|
|
|
/** @var array file list pointed with that directive */ |
37
|
|
|
private $files = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Specific constructor for inclusion directives. |
41
|
|
|
* |
42
|
|
|
* @param string $name the name of the key/context |
43
|
|
|
* @param string $value the value of the key/context |
44
|
|
|
* @param string $prefix the filesystem path where the web server is installed |
45
|
|
|
*/ |
46
|
4 |
|
public function __construct($name, $value = '', $prefix = '') |
47
|
|
|
{ |
48
|
4 |
|
parent::__construct($name, $value); |
49
|
4 |
|
$this->prefix = $prefix; |
50
|
4 |
|
$this->setFiles(); |
51
|
4 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the detected files of the directive. |
55
|
|
|
* |
56
|
|
|
* @return array detected files |
57
|
|
|
*/ |
58
|
3 |
|
public function getFiles() |
59
|
|
|
{ |
60
|
3 |
|
return $this->files; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Detects and memoizes the included files. |
65
|
|
|
*/ |
66
|
4 |
|
public function setFiles() |
67
|
|
|
{ |
68
|
4 |
|
$this->files = []; |
69
|
4 |
|
$path = $this->getValue(); |
70
|
|
|
|
71
|
4 |
|
if (!preg_match('#^/#', $path)) { |
72
|
2 |
|
$path = $this->prefix.'/'.$path; |
73
|
2 |
|
} |
74
|
|
|
|
75
|
4 |
|
$iterator = new GlobIterator($path); |
76
|
4 |
|
foreach ($iterator as $path) { |
77
|
3 |
|
$this->files[] = $path; |
78
|
4 |
|
} |
79
|
|
|
|
80
|
4 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|