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\HtaccessContainer; |
9
|
|
|
use Tivie\HtaccessParser\Parser; |
10
|
|
|
use Tivie\HtaccessParser\Token\Block; |
11
|
|
|
|
12
|
|
|
class Htaccess |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Filesystem |
16
|
|
|
*/ |
17
|
|
|
private $fs; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Parser |
21
|
|
|
*/ |
22
|
|
|
private $parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \ArrayAccess|HtaccessContainer |
26
|
|
|
*/ |
27
|
|
|
private $contents; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Htaccess constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $path |
33
|
|
|
* |
34
|
|
|
* @throws ConfigurationException |
35
|
|
|
*/ |
36
|
|
|
public function __construct(string $path) { |
37
|
|
|
$this->fs = new Filesystem(); |
38
|
|
|
|
39
|
|
|
if (!$this->fs->exists($path)) { |
40
|
|
|
$this->fs->dumpFile($path, file_get_contents(__DIR__ . '/../../../../.htaccess')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->parser = new Parser(new \SplFileObject($path)); |
44
|
|
|
$this->parser->ignoreWhitelines(false); |
45
|
|
|
$this->contents = $this->parser->parse(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Parse the modified .htaccess |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function parse() : string { |
54
|
|
|
return (string) $this->contents; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get or create the headers block |
59
|
|
|
* |
60
|
|
|
* @return Block |
61
|
|
|
*/ |
62
|
|
|
public function &getHeaderBlock() : Block { |
63
|
|
|
$headerBlock = $this->findHeaderBlockByModName('mod_headers.c'); |
64
|
|
|
|
65
|
|
|
if (!$headerBlock) { |
66
|
|
|
$headerBlock = new Block('ifmodule'); |
67
|
|
|
$headerBlock->addArgument('mod_headers.c'); |
68
|
|
|
$this->contents->append($headerBlock); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $headerBlock; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get or create a page block within the headers block |
76
|
|
|
* |
77
|
|
|
* @param Page $page |
78
|
|
|
* |
79
|
|
|
* @return Block |
80
|
|
|
*/ |
81
|
|
|
public function &getPageBlock(Page $page) : Block { |
82
|
|
|
$headerBlock = $this->getHeaderBlock(); |
83
|
|
|
$pageId = trim($page->getId(), '/') ?? 'index'; |
84
|
|
|
$pageId = pathinfo($pageId !== '' ? "{$pageId}" : 'index', PATHINFO_BASENAME); |
85
|
|
|
$pageName = '"^' . $pageId . '\.html$"'; |
86
|
|
|
|
87
|
|
|
$pageBlock = $this->findPageBlockByParentAndName($headerBlock, $pageName); |
88
|
|
|
|
89
|
|
|
if (!$pageBlock) { |
90
|
|
|
$pageBlock = new Block('filesmatch'); |
91
|
|
|
$pageBlock->addArgument($pageName); |
92
|
|
|
$headerBlock->addChild($pageBlock); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $pageBlock; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Clear all page header blocks |
100
|
|
|
*/ |
101
|
|
|
public function clearPageBlocks() : void { |
102
|
|
|
$headerBlock = $this->getHeaderBlock(); |
103
|
|
|
|
104
|
|
|
foreach ($headerBlock as $content) { |
105
|
|
|
if ($content instanceof Block && strtolower($content->getName()) === 'filesmatch') { |
106
|
|
|
$headerBlock->removeChild($content); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Block $headerBlock |
113
|
|
|
* @param string $pageName |
114
|
|
|
* |
115
|
|
|
* @return null|Block |
116
|
|
|
*/ |
117
|
|
|
private function findPageBlockByParentAndName(Block $headerBlock, string $pageName) : ?Block { |
118
|
|
|
foreach ($headerBlock as $content) { |
119
|
|
|
if ($content instanceof Block |
120
|
|
|
&& strtolower($content->getName()) === 'filesmatch' |
121
|
|
|
&& count($content->getArguments()) |
122
|
|
|
&& $content->getArguments()[0] === $pageName |
123
|
|
|
) { |
124
|
|
|
return $content; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $modName |
133
|
|
|
* |
134
|
|
|
* @return null|Block |
135
|
|
|
*/ |
136
|
|
|
private function findHeaderBlockByModName(string $modName) : ?Block { |
137
|
|
|
foreach ($this->contents as $content) { |
138
|
|
|
if ($content instanceof Block |
139
|
|
|
&& strtolower($content->getName()) === 'ifmodule' |
140
|
|
|
&& count($content->getArguments()) |
141
|
|
|
&& $content->getArguments()[0] === $modName |
142
|
|
|
) { |
143
|
|
|
return $content; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.