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@0
|
7 */
|
Chris@0
|
8 class Extension implements \Serializable {
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * The type of the extension (e.g., 'module').
|
Chris@0
|
12 *
|
Chris@0
|
13 * @var string
|
Chris@0
|
14 */
|
Chris@0
|
15 protected $type;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The relative pathname of the extension (e.g., 'core/modules/node/node.info.yml').
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var string
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $pathname;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The filename of the main extension file (e.g., 'node.module').
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var string|null
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $filename;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * An SplFileInfo instance for the extension's info file.
|
Chris@0
|
33 *
|
Chris@0
|
34 * Note that SplFileInfo is a PHP resource and resources cannot be serialized.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var \SplFileInfo
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $splFileInfo;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * The app root.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var string
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $root;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Constructs a new Extension object.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param string $root
|
Chris@0
|
51 * The app root.
|
Chris@0
|
52 * @param string $type
|
Chris@0
|
53 * The type of the extension; e.g., 'module'.
|
Chris@0
|
54 * @param string $pathname
|
Chris@0
|
55 * The relative path and filename of the extension's info file; e.g.,
|
Chris@0
|
56 * 'core/modules/node/node.info.yml'.
|
Chris@0
|
57 * @param string $filename
|
Chris@0
|
58 * (optional) The filename of the main extension file; e.g., 'node.module'.
|
Chris@0
|
59 */
|
Chris@0
|
60 public function __construct($root, $type, $pathname, $filename = NULL) {
|
Chris@0
|
61 $this->root = $root;
|
Chris@0
|
62 $this->type = $type;
|
Chris@0
|
63 $this->pathname = $pathname;
|
Chris@0
|
64 $this->filename = $filename;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Returns the type of the extension.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return string
|
Chris@0
|
71 */
|
Chris@0
|
72 public function getType() {
|
Chris@0
|
73 return $this->type;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Returns the internal name of the extension.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return string
|
Chris@0
|
80 */
|
Chris@0
|
81 public function getName() {
|
Chris@0
|
82 return basename($this->pathname, '.info.yml');
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Returns the relative path of the extension.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @return string
|
Chris@0
|
89 */
|
Chris@0
|
90 public function getPath() {
|
Chris@0
|
91 return dirname($this->pathname);
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Returns the relative path and filename of the extension's info file.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return string
|
Chris@0
|
98 */
|
Chris@0
|
99 public function getPathname() {
|
Chris@0
|
100 return $this->pathname;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Returns the filename of the extension's info file.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return string
|
Chris@0
|
107 */
|
Chris@0
|
108 public function getFilename() {
|
Chris@0
|
109 return basename($this->pathname);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Returns the relative path of the main extension file, if any.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @return string|null
|
Chris@0
|
116 */
|
Chris@0
|
117 public function getExtensionPathname() {
|
Chris@0
|
118 if ($this->filename) {
|
Chris@0
|
119 return $this->getPath() . '/' . $this->filename;
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Returns the name of the main extension file, if any.
|
Chris@0
|
125 *
|
Chris@0
|
126 * @return string|null
|
Chris@0
|
127 */
|
Chris@0
|
128 public function getExtensionFilename() {
|
Chris@0
|
129 return $this->filename;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Loads the main extension file, if any.
|
Chris@0
|
134 *
|
Chris@0
|
135 * @return bool
|
Chris@0
|
136 * TRUE if this extension has a main extension file, FALSE otherwise.
|
Chris@0
|
137 */
|
Chris@0
|
138 public function load() {
|
Chris@0
|
139 if ($this->filename) {
|
Chris@0
|
140 include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
|
Chris@0
|
141 return TRUE;
|
Chris@0
|
142 }
|
Chris@0
|
143 return FALSE;
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * Re-routes method calls to SplFileInfo.
|
Chris@0
|
148 *
|
Chris@0
|
149 * Offers all SplFileInfo methods to consumers; e.g., $extension->getMTime().
|
Chris@0
|
150 */
|
Chris@0
|
151 public function __call($method, array $args) {
|
Chris@0
|
152 if (!isset($this->splFileInfo)) {
|
Chris@0
|
153 $this->splFileInfo = new \SplFileInfo($this->pathname);
|
Chris@0
|
154 }
|
Chris@0
|
155 return call_user_func_array([$this->splFileInfo, $method], $args);
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * Implements Serializable::serialize().
|
Chris@0
|
160 *
|
Chris@0
|
161 * Serializes the Extension object in the most optimized way.
|
Chris@0
|
162 */
|
Chris@0
|
163 public function serialize() {
|
Chris@0
|
164 // Don't serialize the app root, since this could change if the install is
|
Chris@0
|
165 // moved.
|
Chris@0
|
166 $data = [
|
Chris@0
|
167 'type' => $this->type,
|
Chris@0
|
168 'pathname' => $this->pathname,
|
Chris@0
|
169 'filename' => $this->filename,
|
Chris@0
|
170 ];
|
Chris@0
|
171
|
Chris@0
|
172 // @todo ThemeHandler::listInfo(), ThemeHandler::rebuildThemeData(), and
|
Chris@0
|
173 // system_list() are adding custom properties to the Extension object.
|
Chris@0
|
174 $info = new \ReflectionObject($this);
|
Chris@0
|
175 foreach ($info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
|
Chris@0
|
176 $data[$property->getName()] = $property->getValue($this);
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 return serialize($data);
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 /**
|
Chris@0
|
183 * {@inheritdoc}
|
Chris@0
|
184 */
|
Chris@0
|
185 public function unserialize($data) {
|
Chris@0
|
186 $data = unserialize($data);
|
Chris@0
|
187 // Get the app root from the container.
|
Chris@0
|
188 $this->root = DRUPAL_ROOT;
|
Chris@0
|
189 $this->type = $data['type'];
|
Chris@0
|
190 $this->pathname = $data['pathname'];
|
Chris@0
|
191 $this->filename = $data['filename'];
|
Chris@0
|
192
|
Chris@0
|
193 // @todo ThemeHandler::listInfo(), ThemeHandler::rebuildThemeData(), and
|
Chris@0
|
194 // system_list() are adding custom properties to the Extension object.
|
Chris@0
|
195 foreach ($data as $property => $value) {
|
Chris@0
|
196 if (!isset($this->$property)) {
|
Chris@0
|
197 $this->$property = $value;
|
Chris@0
|
198 }
|
Chris@0
|
199 }
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 }
|