Extension.php 5.05 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\Core\Extension;

/**
 * Defines an extension (file) object.
7 8 9 10 11
 *
 * This class does not implement the Serializable interface since problems
 * occurred when using the serialize method.
 *
 * @see https://bugs.php.net/bug.php?id=66052
12
 */
13
class Extension {
14 15 16 17 18 19

  /**
   * The type of the extension (e.g., 'module').
   *
   * @var string
   */
20
  protected $type;
21 22 23 24 25 26 27 28

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

29 30
  /**
   * The filename of the main extension file (e.g., 'node.module').
31
   *
32
   * @var string|null
33
   */
34
  protected $filename;
35 36 37 38 39 40 41 42 43 44

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

45 46 47 48 49 50 51
  /**
   * The app root.
   *
   * @var string
   */
  protected $root;

52 53 54
  /**
   * Constructs a new Extension object.
   *
55 56
   * @param string $root
   *   The app root.
57 58 59 60 61 62
   * @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
63
   *   (optional) The filename of the main extension file; e.g., 'node.module'.
64
   */
65
  public function __construct($root, $type, $pathname, $filename = NULL) {
66 67
    // @see \Drupal\Core\Theme\ThemeInitialization::getActiveThemeByName()
    assert($pathname === 'core/core.info.yml' || ($pathname[0] !== '/' && file_exists($root . '/' . $pathname)), sprintf('The file specified by the given app root, relative path and file name (%s) do not exist.', $root . '/' . $pathname));
68
    $this->root = $root;
69 70
    $this->type = $type;
    $this->pathname = $pathname;
71
    $this->filename = $filename;
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 112 113 114 115 116 117 118
  }

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

119 120 121 122 123 124
  /**
   * Returns the relative path of the main extension file, if any.
   *
   * @return string|null
   */
  public function getExtensionPathname() {
125 126
    if ($this->filename) {
      return $this->getPath() . '/' . $this->filename;
127 128 129 130 131 132 133 134 135
    }
  }

  /**
   * Returns the name of the main extension file, if any.
   *
   * @return string|null
   */
  public function getExtensionFilename() {
136
    return $this->filename;
137 138 139 140 141 142 143 144 145
  }

  /**
   * Loads the main extension file, if any.
   *
   * @return bool
   *   TRUE if this extension has a main extension file, FALSE otherwise.
   */
  public function load() {
146
    if ($this->filename) {
147
      include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
148 149 150 151 152
      return TRUE;
    }
    return FALSE;
  }

153 154 155 156 157 158 159
  /**
   * 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)) {
160
      $this->splFileInfo = new \SplFileInfo($this->root . '/' . $this->pathname);
161
    }
162
    return call_user_func_array([$this->splFileInfo, $method], $args);
163 164 165
  }

  /**
166
   * Magic method implementation to serialize the extension object.
167
   *
168 169
   * @return array
   *   The names of all variables that should be serialized.
170
   */
171
  public function __sleep() {
172 173
    // @todo \Drupal\Core\Extension\ThemeExtensionList is adding custom
    //   properties to the Extension object.
174 175 176 177 178
    $properties = get_object_vars($this);
    // Don't serialize the app root, since this could change if the install is
    // moved. Don't serialize splFileInfo because it can not be.
    unset($properties['splFileInfo'], $properties['root']);
    return array_keys($properties);
179 180 181
  }

  /**
182
   * Magic method implementation to unserialize the extension object.
183
   */
184
  public function __wakeup() {
185 186 187 188 189 190 191 192
    // Get the app root from the container. While compiling the container we
    // have to discover all the extension service files in
    // \Drupal\Core\DrupalKernel::initializeServiceProviders(). This results in
    // creating extension objects before the container has the kernel.
    // Specifically, this occurs during the call to
    // \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory().
    $container = \Drupal::hasContainer() ? \Drupal::getContainer() : FALSE;
    $this->root = $container && $container->hasParameter('app.root') ? $container->getParameter('app.root') : DRUPAL_ROOT;
193 194 195
  }

}