Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Extension;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Defines an extension (file) object.
|
Chris@17
|
7 *
|
Chris@17
|
8 * This class does not implement the Serializable interface since problems
|
Chris@17
|
9 * occurred when using the serialize method.
|
Chris@17
|
10 *
|
Chris@17
|
11 * @see https://bugs.php.net/bug.php?id=66052
|
Chris@0
|
12 */
|
Chris@17
|
13 class Extension {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The type of the extension (e.g., 'module').
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var string
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $type;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The relative pathname of the extension (e.g., 'core/modules/node/node.info.yml').
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var string
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $pathname;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The filename of the main extension file (e.g., 'node.module').
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var string|null
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $filename;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * An SplFileInfo instance for the extension's info file.
|
Chris@0
|
38 *
|
Chris@0
|
39 * Note that SplFileInfo is a PHP resource and resources cannot be serialized.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var \SplFileInfo
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $splFileInfo;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * The app root.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var string
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $root;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Constructs a new Extension object.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @param string $root
|
Chris@0
|
56 * The app root.
|
Chris@0
|
57 * @param string $type
|
Chris@0
|
58 * The type of the extension; e.g., 'module'.
|
Chris@0
|
59 * @param string $pathname
|
Chris@0
|
60 * The relative path and filename of the extension's info file; e.g.,
|
Chris@0
|
61 * 'core/modules/node/node.info.yml'.
|
Chris@0
|
62 * @param string $filename
|
Chris@0
|
63 * (optional) The filename of the main extension file; e.g., 'node.module'.
|
Chris@0
|
64 */
|
Chris@0
|
65 public function __construct($root, $type, $pathname, $filename = NULL) {
|
Chris@0
|
66 $this->root = $root;
|
Chris@0
|
67 $this->type = $type;
|
Chris@0
|
68 $this->pathname = $pathname;
|
Chris@0
|
69 $this->filename = $filename;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Returns the type of the extension.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @return string
|
Chris@0
|
76 */
|
Chris@0
|
77 public function getType() {
|
Chris@0
|
78 return $this->type;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Returns the internal name of the extension.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @return string
|
Chris@0
|
85 */
|
Chris@0
|
86 public function getName() {
|
Chris@0
|
87 return basename($this->pathname, '.info.yml');
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Returns the relative path of the extension.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @return string
|
Chris@0
|
94 */
|
Chris@0
|
95 public function getPath() {
|
Chris@0
|
96 return dirname($this->pathname);
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Returns the relative path and filename of the extension's info file.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @return string
|
Chris@0
|
103 */
|
Chris@0
|
104 public function getPathname() {
|
Chris@0
|
105 return $this->pathname;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Returns the filename of the extension's info file.
|
Chris@0
|
110 *
|
Chris@0
|
111 * @return string
|
Chris@0
|
112 */
|
Chris@0
|
113 public function getFilename() {
|
Chris@0
|
114 return basename($this->pathname);
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Returns the relative path of the main extension file, if any.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @return string|null
|
Chris@0
|
121 */
|
Chris@0
|
122 public function getExtensionPathname() {
|
Chris@0
|
123 if ($this->filename) {
|
Chris@0
|
124 return $this->getPath() . '/' . $this->filename;
|
Chris@0
|
125 }
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Returns the name of the main extension file, if any.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return string|null
|
Chris@0
|
132 */
|
Chris@0
|
133 public function getExtensionFilename() {
|
Chris@0
|
134 return $this->filename;
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Loads the main extension file, if any.
|
Chris@0
|
139 *
|
Chris@0
|
140 * @return bool
|
Chris@0
|
141 * TRUE if this extension has a main extension file, FALSE otherwise.
|
Chris@0
|
142 */
|
Chris@0
|
143 public function load() {
|
Chris@0
|
144 if ($this->filename) {
|
Chris@0
|
145 include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
|
Chris@0
|
146 return TRUE;
|
Chris@0
|
147 }
|
Chris@0
|
148 return FALSE;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Re-routes method calls to SplFileInfo.
|
Chris@0
|
153 *
|
Chris@0
|
154 * Offers all SplFileInfo methods to consumers; e.g., $extension->getMTime().
|
Chris@0
|
155 */
|
Chris@0
|
156 public function __call($method, array $args) {
|
Chris@0
|
157 if (!isset($this->splFileInfo)) {
|
Chris@0
|
158 $this->splFileInfo = new \SplFileInfo($this->pathname);
|
Chris@0
|
159 }
|
Chris@0
|
160 return call_user_func_array([$this->splFileInfo, $method], $args);
|
Chris@0
|
161 }
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@17
|
164 * Magic method implementation to serialize the extension object.
|
Chris@0
|
165 *
|
Chris@17
|
166 * @return array
|
Chris@17
|
167 * The names of all variables that should be serialized.
|
Chris@0
|
168 */
|
Chris@17
|
169 public function __sleep() {
|
Chris@17
|
170 // @todo \Drupal\Core\Extension\ThemeExtensionList is adding custom
|
Chris@17
|
171 // properties to the Extension object.
|
Chris@17
|
172 $properties = get_object_vars($this);
|
Chris@0
|
173 // Don't serialize the app root, since this could change if the install is
|
Chris@17
|
174 // moved. Don't serialize splFileInfo because it can not be.
|
Chris@17
|
175 unset($properties['splFileInfo'], $properties['root']);
|
Chris@17
|
176 return array_keys($properties);
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@17
|
180 * Magic method implementation to unserialize the extension object.
|
Chris@0
|
181 */
|
Chris@17
|
182 public function __wakeup() {
|
Chris@0
|
183 // Get the app root from the container.
|
Chris@18
|
184 $this->root = \Drupal::hasService('app.root') ? \Drupal::root() : DRUPAL_ROOT;
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 }
|