3 namespace Drupal\Core\Extension;
6 * Defines an extension (file) object.
8 class Extension implements \Serializable {
11 * The type of the extension (e.g., 'module').
18 * The relative pathname of the extension (e.g., 'core/modules/node/node.info.yml').
25 * The filename of the main extension file (e.g., 'node.module').
32 * An SplFileInfo instance for the extension's info file.
34 * Note that SplFileInfo is a PHP resource and resources cannot be serialized.
38 protected $splFileInfo;
48 * Constructs a new Extension object.
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'.
60 public function __construct($root, $type, $pathname, $filename = NULL) {
63 $this->pathname = $pathname;
64 $this->filename = $filename;
68 * Returns the type of the extension.
72 public function getType() {
77 * Returns the internal name of the extension.
81 public function getName() {
82 return basename($this->pathname, '.info.yml');
86 * Returns the relative path of the extension.
90 public function getPath() {
91 return dirname($this->pathname);
95 * Returns the relative path and filename of the extension's info file.
99 public function getPathname() {
100 return $this->pathname;
104 * Returns the filename of the extension's info file.
108 public function getFilename() {
109 return basename($this->pathname);
113 * Returns the relative path of the main extension file, if any.
115 * @return string|null
117 public function getExtensionPathname() {
118 if ($this->filename) {
119 return $this->getPath() . '/' . $this->filename;
124 * Returns the name of the main extension file, if any.
126 * @return string|null
128 public function getExtensionFilename() {
129 return $this->filename;
133 * Loads the main extension file, if any.
136 * TRUE if this extension has a main extension file, FALSE otherwise.
138 public function load() {
139 if ($this->filename) {
140 include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
147 * Re-routes method calls to SplFileInfo.
149 * Offers all SplFileInfo methods to consumers; e.g., $extension->getMTime().
151 public function __call($method, array $args) {
152 if (!isset($this->splFileInfo)) {
153 $this->splFileInfo = new \SplFileInfo($this->pathname);
155 return call_user_func_array([$this->splFileInfo, $method], $args);
159 * Implements Serializable::serialize().
161 * Serializes the Extension object in the most optimized way.
163 public function serialize() {
164 // Don't serialize the app root, since this could change if the install is
167 'type' => $this->type,
168 'pathname' => $this->pathname,
169 'filename' => $this->filename,
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);
179 return serialize($data);
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'];
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;