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
233
234
235
236
237
|
<?php
namespace Drupal\Core\Extension;
/**
* Defines an extension (file) object.
*
* This class does not implement the Serializable interface since problems
* occurred when using the serialize method.
*
* @see https://bugs.php.net/bug.php?id=66052
*/
#[\AllowDynamicProperties]
class Extension {
/**
* The type of the extension (e.g., 'module').
*
* @var string
*/
protected $type;
/**
* The relative pathname of the extension.
*
* An example relative pathname is '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;
/**
* The extension info array.
*/
public array $info;
/**
* 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) {
// @see \Drupal\Core\Theme\ThemeInitialization::getActiveThemeByName()
assert($pathname === 'core/core.info.yml' || ($pathname[0] !== '/' && file_exists($root . '/' . $pathname)), sprintf('The file specified by the given app root, relative path and file name (%s) do not exist.', $root . '/' . $pathname));
$this->root = $root;
$this->type = $type;
$this->pathname = $pathname;
$this->filename = $filename;
}
/**
* Returns the type of the extension.
*
* @return string
* The extension type. This is usually 'module' or 'theme'.
*/
public function getType() {
return $this->type;
}
/**
* Returns the internal name of the extension.
*
* @return string
* The machine name of the extension.
*/
public function getName() {
return basename($this->pathname, '.info.yml');
}
/**
* Returns the relative path of the extension.
*
* @return string
* The relative path of the extension.
*/
public function getPath() {
return dirname($this->pathname);
}
/**
* Returns the relative path and filename of the extension's info file.
*
* @return string
* The relative path and filename of the extension's .info file.
*/
public function getPathname() {
return $this->pathname;
}
/**
* Returns the filename of the extension's info file.
*
* @return string
* The base name of the extension .info file.
*/
public function getFilename() {
return basename($this->pathname);
}
/**
* Returns the relative path of the main extension file, if any.
*
* @return string|null
* The relative path for the main extension file, usually the *.module file.
*/
public function getExtensionPathname() {
if ($this->filename) {
return $this->getPath() . '/' . $this->filename;
}
}
/**
* Returns the name of the main extension file, if any.
*
* @return string|null
* The filename of the main extension file, usually the *.module file.
*/
public function getExtensionFilename() {
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() {
if ($this->filename) {
include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
return TRUE;
}
return FALSE;
}
/**
* Returns SplFileInfo instance for the extension's info file.
*
* @return \SplFileInfo
* The object to access a file information of info file.
*
* @see https://www.php.net/manual/class.splfileinfo.php
*/
public function getFileInfo(): \SplFileInfo {
if (!isset($this->splFileInfo)) {
$this->splFileInfo = new \SplFileInfo($this->root . '/' . $this->pathname);
}
return $this->splFileInfo;
}
/**
* Magic method implementation to serialize the extension object.
*
* @return array
* The names of all variables that should be serialized.
*/
public function __sleep(): array {
// @todo \Drupal\Core\Extension\ThemeExtensionList is adding custom
// properties to the Extension object.
$properties = get_object_vars($this);
// Don't serialize the app root, since this could change if the install is
// moved. Don't serialize splFileInfo because it can not be.
unset($properties['splFileInfo'], $properties['root']);
return array_keys($properties);
}
/**
* Magic method implementation to unserialize the extension object.
*/
public function __wakeup(): void {
// Get the app root from the container. While compiling the container we
// have to discover all the extension service files in
// \Drupal\Core\DrupalKernel::initializeServiceProviders(). This results in
// creating extension objects before the container has the kernel.
// Specifically, this occurs during the call to
// \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory().
$container = \Drupal::hasContainer() ? \Drupal::getContainer() : FALSE;
$this->root = $container && $container->hasParameter('app.root') ? $container->getParameter('app.root') : DRUPAL_ROOT;
}
/**
* Checks if an extension is marked as experimental.
*
* @return bool
* TRUE if an extension is marked as experimental, FALSE otherwise.
*/
public function isExperimental(): bool {
return (isset($this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER])
&& $this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::EXPERIMENTAL);
}
/**
* Checks if an extension is marked as obsolete.
*
* @return bool
* TRUE if an extension is marked as obsolete, FALSE otherwise.
*/
public function isObsolete(): bool {
// This function checks for 'lifecycle: obsolete' to determine if an
// extension is marked as obsolete.
return (isset($this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER])
&& $this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::OBSOLETE);
}
}
|