comparison core/lib/Drupal/Core/Extension/Extension.php @ 0:4c8ae668cc8c

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