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

/**
 * @file
 * Contains \Drupal\Core\Extension\Extension.
 */

namespace Drupal\Core\Extension;

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

  /**
   * The type of the extension (e.g., 'module').
   *
18
   * @todo Change to protected once external test dependencies are resolved.
19
   *
20 21
   * @var string
   */
22
  public $type;
23 24 25 26 27 28 29 30

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

31 32 33
  /**
   * The internal name of the extension (e.g., 'node').
   *
34
   * @todo Remove this property once external test dependencies are resolved.
35 36 37 38 39
   *
   * @var string
   */
  public $name;

40 41 42 43 44 45 46 47 48 49 50
  /**
   * The relative pathname of the main extension file (e.g., 'core/modules/node/node.module').
   *
   * @todo Remove this property and do not require .module/.profile files.
   * @see https://drupal.org/node/340723
   *
   * @var string
   */
  public $uri;

  /**
51
   * Same as $uri.
52
   *
53
   * @todo Remove this property once external test dependencies are resolved.
54
   *
55 56 57 58 59 60
   * @var string
   */
  public $filename;

  /**
   * The filename of the main extension file (e.g., 'node.module').
61
   *
62
   * @todo Rename to $filename once external test dependencies are resolved.
63
   *
64
   * @var string|null
65
   */
66
  protected $_filename;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

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

  /**
   * Constructs a new Extension object.
   *
   * @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
86
   *   (optional) The filename of the main extension file; e.g., 'node.module'.
87
   */
88
  public function __construct($type, $pathname, $filename = NULL) {
89 90
    $this->type = $type;
    $this->pathname = $pathname;
91
    $this->_filename = $filename;
92
    // Set legacy public properties.
93 94 95
    $this->name = $this->getName();
    $this->uri = $this->getPath() . '/' . $filename;
    $this->filename = $this->uri;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  }

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

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  /**
   * Returns the relative path of the main extension file, if any.
   *
   * @return string|null
   */
  public function getExtensionPathname() {
    if ($this->_filename) {
      return $this->getPath() . '/' . $this->_filename;
    }
  }

  /**
   * Returns the name of the main extension file, if any.
   *
   * @return string|null
   */
  public function getExtensionFilename() {
    return $this->_filename;
  }

  /**
   * Loads the main extension file, if any.
   *
   * @return bool
   *   TRUE if this extension has a main extension file, FALSE otherwise.
   */
  public function load() {
    if ($this->_filename) {
      include_once DRUPAL_ROOT . '/' . $this->getPath() . '/' . $this->_filename;
      return TRUE;
    }
    return FALSE;
  }

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  /**
   * 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() {
    $data = array(
      'type' => $this->type,
      'pathname' => $this->pathname,
198
      '_filename' => $this->_filename,
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    );

    // @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);
  }

  /**
   * Implements Serializable::unserialize().
   */
  public function unserialize($data) {
    $data = unserialize($data);
    $this->type = $data['type'];
    $this->pathname = $data['pathname'];
218
    $this->_filename = $data['_filename'];
219 220 221 222

    // Restore legacy public properties.
    // @todo Remove these properties and do not require .module/.profile files.
    // @see https://drupal.org/node/340723
223 224 225
    $this->name = $this->getName();
    $this->uri = $this->getPath() . '/' . $this->_filename;
    $this->filename = $this->uri;
226 227 228 229 230 231 232 233 234 235 236

    // @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;
      }
    }
  }

}