|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Site\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Exception\ConfigurationException; |
|
6
|
|
|
use Brendt\Stitcher\Site\Page; |
|
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
8
|
|
|
use Tivie\HtaccessParser\Parser; |
|
9
|
|
|
use Tivie\HtaccessParser\Token\Block; |
|
10
|
|
|
|
|
11
|
|
|
class Htaccess |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Filesystem |
|
15
|
|
|
*/ |
|
16
|
|
|
private $fs; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Parser |
|
20
|
|
|
*/ |
|
21
|
|
|
private $parser; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $contents; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Htaccess constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $path |
|
32
|
|
|
* |
|
33
|
|
|
* @throws ConfigurationException |
|
34
|
|
|
*/ |
|
35
|
|
|
function __construct(string $path) { |
|
|
|
|
|
|
36
|
|
|
$this->fs = new Filesystem(); |
|
37
|
|
|
|
|
38
|
|
|
if (!$this->fs->exists($path)) { |
|
39
|
|
|
$this->fs->dumpFile($path, __DIR__ . '/../../../../.htaccess'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->parser = new Parser(new \SplFileObject($path)); |
|
43
|
|
|
$this->parser->ignoreWhitelines(false); |
|
44
|
|
|
$this->contents = $this->parser->parse(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Parse the modified .htaccess |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function parse() : string { |
|
53
|
|
|
return (string) $this->contents; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get or create the headers block |
|
58
|
|
|
* |
|
59
|
|
|
* @return Block |
|
60
|
|
|
*/ |
|
61
|
|
|
public function &getHeaderBlock() : Block { |
|
62
|
|
|
$headerBlock = null; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->contents as $content) { |
|
65
|
|
|
if ($content instanceof Block |
|
66
|
|
|
&& strtolower($content->getName()) === 'ifmodule' |
|
67
|
|
|
&& count($content->getArguments()) |
|
68
|
|
|
&& $content->getArguments()[0] === 'mod_headers.c' |
|
69
|
|
|
) { |
|
70
|
|
|
$headerBlock = $content; |
|
71
|
|
|
|
|
72
|
|
|
break; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (!$headerBlock) { |
|
77
|
|
|
$headerBlock = new Block('ifmodule', 'mod_headers.c'); |
|
78
|
|
|
$this->contents->append($headerBlock); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $headerBlock; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get or create a page block within the headers block |
|
86
|
|
|
* |
|
87
|
|
|
* @param Page $page |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed|null|Block |
|
90
|
|
|
*/ |
|
91
|
|
|
public function &getPageBlock(Page $page) { |
|
92
|
|
|
$pageBlock = null; |
|
93
|
|
|
$headerBlock = $this->getHeaderBlock(); |
|
94
|
|
|
$pageId = trim($page->getId(), '/'); |
|
95
|
|
|
$pageName = '"^\/' . $pageId . '.html$"'; |
|
96
|
|
|
|
|
97
|
|
|
foreach ($headerBlock as $content) { |
|
98
|
|
|
if ($content instanceof Block |
|
99
|
|
|
&& strtolower($content->getName()) === 'filesmatch' |
|
100
|
|
|
&& count($content->getArguments()) |
|
101
|
|
|
&& $content->getArguments()[0] === $pageName |
|
102
|
|
|
) { |
|
103
|
|
|
$pageBlock = $content; |
|
104
|
|
|
|
|
105
|
|
|
break; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!$pageBlock) { |
|
110
|
|
|
$pageBlock = new Block('filesmatch'); |
|
111
|
|
|
$pageBlock->addArgument($pageName); |
|
112
|
|
|
$headerBlock->addChild($pageBlock); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $pageBlock; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function clearPageBlocks() { |
|
119
|
|
|
$pageBlock = null; |
|
|
|
|
|
|
120
|
|
|
$headerBlock = $this->getHeaderBlock(); |
|
121
|
|
|
|
|
122
|
|
|
foreach ($headerBlock as $content) { |
|
123
|
|
|
if ($content instanceof Block && strtolower($content->getName()) === 'filesmatch') { |
|
124
|
|
|
$headerBlock->removeChild($content); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.