-
Notifications
You must be signed in to change notification settings - Fork 651
/
ConfigFile.php
148 lines (119 loc) · 4.31 KB
/
ConfigFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Psalm\Internal\PluginManager;
use DOMDocument;
use DomElement;
use Psalm\Config;
use RuntimeException;
use function assert;
use function file_get_contents;
use function file_put_contents;
use function strpos;
use function substr;
class ConfigFile
{
/** @var string */
private $path;
/** @var string */
private $current_dir;
/** @var string|null */
private $psalm_header;
/** @var int|null */
private $psalm_tag_end_pos;
public function __construct(string $current_dir, ?string $explicit_path)
{
$this->current_dir = $current_dir;
if ($explicit_path) {
$this->path = $explicit_path;
} else {
$path = Config::locateConfigFile($current_dir);
if (!$path) {
throw new RuntimeException('Cannot find Psalm config');
}
$this->path = $path;
}
}
public function getConfig(): Config
{
return Config::loadFromXMLFile($this->path, $this->current_dir);
}
public function removePlugin(string $plugin_class): void
{
$config_xml = $this->readXml();
/** @var DomElement */
$psalm_root = $config_xml->getElementsByTagName('psalm')[0];
$plugins_elements = $psalm_root->getElementsByTagName('plugins');
if (!$plugins_elements->length) {
// no plugins, nothing to remove
return;
}
/** @var DomElement */
$plugins_element = $plugins_elements->item(0);
$plugin_elements = $plugins_element->getElementsByTagName('pluginClass');
foreach ($plugin_elements as $plugin_element) {
if ($plugin_element->getAttribute('class') === $plugin_class) {
$plugins_element->removeChild($plugin_element);
break;
}
}
if (!$plugin_elements->length) {
// avoid breaking old psalm binaries, whose schema did not allow empty plugins
$psalm_root->removeChild($plugins_element);
}
$this->saveXml($config_xml);
}
public function addPlugin(string $plugin_class): void
{
$config_xml = $this->readXml();
/** @var DomElement */
$psalm_root = $config_xml->getElementsByTagName('psalm')->item(0);
$plugins_elements = $psalm_root->getElementsByTagName('plugins');
if (!$plugins_elements->length) {
$plugins_element = $config_xml->createElement('plugins');
if ($plugins_element) {
$psalm_root->appendChild($plugins_element);
}
} else {
/** @var DomElement */
$plugins_element = $plugins_elements->item(0);
}
$plugin_class_element = $config_xml->createElement('pluginClass');
if ($plugin_class_element) {
$plugin_class_element->setAttribute('xmlns', Config::CONFIG_NAMESPACE);
$plugin_class_element->setAttribute('class', $plugin_class);
if ($plugins_element) {
$plugins_element->appendChild($plugin_class_element);
}
}
$this->saveXml($config_xml);
}
private function readXml(): DOMDocument
{
$doc = new DOMDocument();
$file_contents = file_get_contents($this->path);
if (($tag_start = strpos($file_contents, '<psalm')) !== false) {
$tag_end = strpos($file_contents, '>', $tag_start + 1);
if ($tag_end !== false) {
$this->psalm_tag_end_pos = $tag_end;
$this->psalm_header = substr($file_contents, 0, $tag_end);
}
}
assert($file_contents !== '');
$doc->loadXML($file_contents);
return $doc;
}
private function saveXml(DOMDocument $config_xml): void
{
$new_file_contents = $config_xml->saveXML($config_xml);
if (($tag_start = strpos($new_file_contents, '<psalm')) !== false) {
$tag_end = strpos($new_file_contents, '>', $tag_start + 1);
if ($tag_end !== false
&& ($new_file_contents[$tag_end - 1] !== '/')
&& $this->psalm_tag_end_pos
&& $this->psalm_header
) {
$new_file_contents = $this->psalm_header . substr($new_file_contents, $tag_end);
}
}
file_put_contents($this->path, $new_file_contents);
}
}