Extension.php 4.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace Drupal\Core\Extension;

/**
 * Defines an extension (file) object.
 */
class Extension implements \Serializable {

  /**
   * The type of the extension (e.g., 'module').
   *
   * @var string
   */
15
  protected $type;
16 17 18 19 20 21 22 23

  /**
   * The relative pathname of the extension (e.g., 'core/modules/node/node.info.yml').
   *
   * @var string
   */
  protected $pathname;

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 33 34 35 36 37 38 39

  /**
   * 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;

40 41 42 43 44 45 46
  /**
   * The app root.
   *
   * @var string
   */
  protected $root;

47 48 49
  /**
   * Constructs a new Extension object.
   *
50 51
   * @param string $root
   *   The app root.
52 53 54 55 56 57
   * @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
58
   *   (optional) The filename of the main extension file; e.g., 'node.module'.
59
   */
60 61
  public function __construct($root, $type, $pathname, $filename = NULL) {
    $this->root = $root;
62 63
    $this->type = $type;
    $this->pathname = $pathname;
64
    $this->filename = $filename;
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
  }

  /**
   * Returns the type of the extension.
   *
   * @return string
   */
  public function getType() {
    return $this->type;
  }

  /**
   * Returns the internal name of the extension.
   *
   * @return string
   */
  public function getName() {
    return basename($this->pathname, '.info.yml');
  }

  /**
   * Returns the relative path of the extension.
   *
   * @return string
   */
  public function getPath() {
    return dirname($this->pathname);
  }

  /**
   * Returns the relative path and filename of the extension's info file.
   *
   * @return string
   */
  public function getPathname() {
    return $this->pathname;
  }

  /**
   * Returns the filename of the extension's info file.
   *
   * @return string
   */
  public function getFilename() {
    return basename($this->pathname);
  }

112 113 114 115 116 117
  /**
   * Returns the relative path of the main extension file, if any.
   *
   * @return string|null
   */
  public function getExtensionPathname() {
118 119
    if ($this->filename) {
      return $this->getPath() . '/' . $this->filename;
120 121 122 123 124 125 126 127 128
    }
  }

  /**
   * Returns the name of the main extension file, if any.
   *
   * @return string|null
   */
  public function getExtensionFilename() {
129
    return $this->filename;
130 131 132 133 134 135 136 137 138
  }

  /**
   * Loads the main extension file, if any.
   *
   * @return bool
   *   TRUE if this extension has a main extension file, FALSE otherwise.
   */
  public function load() {
139
    if ($this->filename) {
140
      include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
141 142 143 144 145
      return TRUE;
    }
    return FALSE;
  }

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  /**
   * Re-routes method calls to SplFileInfo.
   *
   * Offers all SplFileInfo methods to consumers; e.g., $extension->getMTime().
   */
  public function __call($method, array $args) {
    if (!isset($this->splFileInfo)) {
      $this->splFileInfo = new \SplFileInfo($this->pathname);
    }
    return call_user_func_array(array($this->splFileInfo, $method), $args);
  }

  /**
   * Implements Serializable::serialize().
   *
   * Serializes the Extension object in the most optimized way.
   */
  public function serialize() {
164 165
    // Don't serialize the app root, since this could change if the install is
    // moved.
166 167 168
    $data = array(
      'type' => $this->type,
      'pathname' => $this->pathname,
169
      'filename' => $this->filename,
170 171 172 173 174 175 176 177 178 179 180 181 182
    );

    // @todo ThemeHandler::listInfo(), ThemeHandler::rebuildThemeData(), and
    //   system_list() are adding custom properties to the Extension object.
    $info = new \ReflectionObject($this);
    foreach ($info->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
      $data[$property->getName()] = $property->getValue($this);
    }

    return serialize($data);
  }

  /**
183
   * {@inheritdoc}
184 185 186
   */
  public function unserialize($data) {
    $data = unserialize($data);
187 188
    // Get the app root from the container.
    $this->root = DRUPAL_ROOT;
189 190
    $this->type = $data['type'];
    $this->pathname = $data['pathname'];
191
    $this->filename = $data['filename'];
192 193 194 195 196 197 198 199 200 201 202

    // @todo ThemeHandler::listInfo(), ThemeHandler::rebuildThemeData(), and
    //   system_list() are adding custom properties to the Extension object.
    foreach ($data as $property => $value) {
      if (!isset($this->$property)) {
        $this->$property = $value;
      }
    }
  }

}