-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Extension.php
232 lines (199 loc) · 5.05 KB
/
Extension.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php declare(strict_types=1);
namespace mglaman\PHPStanDrupal\Drupal;
use Symfony\Component\Yaml\Yaml;
/**
* Defines an extension (file) object.
*
* Bundled version of \Drupal\Core\Extension\Extension.
*/
class Extension
{
/**
* The type of the extension (e.g., 'module').
*
* @var string
*/
protected $type;
/**
* The relative pathname of the extension (e.g.,
* 'core/modules/node/node.info.yml').
*
* @var string
*/
protected $pathname;
/**
* The filename of the main extension file (e.g., 'node.module').
*
* @var string|null
*/
protected $filename;
/**
* An SplFileInfo instance for the extension's info file.
*
* Note that SplFileInfo is a PHP resource and resources cannot be serialized.
*
* @var ?\SplFileInfo
*/
protected $splFileInfo;
/**
* The app root.
*
* @var string
*/
protected $root;
/**
* @var string
*/
public $subpath = '';
/**
* @var string
*/
public $origin = '';
/**
* @var array|null
*/
private $info;
/**
* @var string[]|null
*/
private $dependencies;
/**
* Constructs a new Extension object.
*
* @param string $root
* The app root.
* @param string $type
* The type of the extension; e.g., 'module'.
* @param string $pathname
* The relative path and filename of the extension's info file; e.g.,
* 'core/modules/node/node.info.yml'.
* @param string $filename
* (optional) The filename of the main extension file; e.g., 'node.module'.
*/
public function __construct($root, $type, $pathname, $filename = null)
{
$this->root = $root;
$this->type = $type;
$this->pathname = $pathname;
$this->filename = $filename;
}
/**
* Returns the type of the extension.
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* Returns the internal name of the extension.
*
* @return string
*/
public function getName(): string
{
return basename($this->pathname, '.info.yml');
}
/**
* Returns the relative path of the extension.
*
* @return string
*/
public function getPath(): string
{
return dirname($this->pathname);
}
public function getAbsolutePath(): string
{
return $this->root . DIRECTORY_SEPARATOR . $this->getPath();
}
/**
* Returns the relative path and filename of the extension's info file.
*
* @return string
*/
public function getPathname(): string
{
return $this->pathname;
}
/**
* Returns the filename of the extension's info file.
*
* @return string
*/
public function getFilename(): string
{
return basename($this->pathname);
}
/**
* Returns the relative path of the main extension file, if any.
*
* @return string|null
*/
public function getExtensionPathname(): ?string
{
if ($this->filename !== null) {
return $this->getPath() . '/' . $this->filename;
}
return null;
}
/**
* Returns the name of the main extension file, if any.
*
* @return string|null
*/
public function getExtensionFilename(): ?string
{
return $this->filename;
}
/**
* Loads the main extension file, if any.
*
* @return bool
* TRUE if this extension has a main extension file, FALSE otherwise.
*/
public function load(): bool
{
if ($this->filename !== null) {
include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
return true;
}
return false;
}
/**
* @return string[]
*/
public function getDependencies(): array
{
if (\is_array($this->dependencies)) {
return $this->dependencies;
}
$info = $this->parseInfo();
$dependencies = $info['dependencies'] ?? [];
if ($dependencies === []) {
return $this->dependencies = $dependencies;
}
$this->dependencies = [];
// @see \Drupal\Core\Extension\Dependency::createFromString().
foreach ($dependencies as $dependency) {
if (\strpos($dependency, ':') !== false) {
[, $dependency] = \explode(':', $dependency);
}
$parts = \explode('(', $dependency, 2);
$this->dependencies[] = \trim($parts[0]);
}
return $this->dependencies;
}
private function parseInfo(): array
{
if (\is_array($this->info)) {
return $this->info;
}
$infoContent = \file_get_contents(\sprintf('%s/%s', $this->root, $this->getPathname()));
if (false === $infoContent) {
throw new \RuntimeException(\sprintf('Cannot read "%s', $this->getPathname()));
}
return $this->info = Yaml::parse($infoContent);
}
}