class Extension

Same name in this branch
  1. 11.x core/modules/system/src/Plugin/migrate/source/Extension.php \Drupal\system\Plugin\migrate\source\Extension
Same name and namespace in other branches
  1. 9 core/modules/system/src/Plugin/migrate/source/Extension.php \Drupal\system\Plugin\migrate\source\Extension
  2. 9 core/lib/Drupal/Core/Extension/Extension.php \Drupal\Core\Extension\Extension
  3. 8.9.x core/modules/system/src/Plugin/migrate/source/Extension.php \Drupal\system\Plugin\migrate\source\Extension
  4. 8.9.x core/lib/Drupal/Core/Extension/Extension.php \Drupal\Core\Extension\Extension
  5. 10 core/modules/system/src/Plugin/migrate/source/Extension.php \Drupal\system\Plugin\migrate\source\Extension
  6. 10 core/lib/Drupal/Core/Extension/Extension.php \Drupal\Core\Extension\Extension

Defines an extension (file) object.

This class does not implement the Serializable interface since problems occurred when using the serialize method.

Hierarchy

Expanded class hierarchy of Extension

See also

https://bugs.php.net/bug.php?id=66052

43 files declare their use of Extension
advisory_feed_test.module in core/modules/system/tests/modules/advisory_feed_test/advisory_feed_test.module
Module for testing the display of security advisories.
ComponentNegotiator.php in core/modules/sdc/src/ComponentNegotiator.php
ComponentNegotiator.php in core/lib/Drupal/Core/Theme/ComponentNegotiator.php
config_translation_test.module in core/modules/config_translation/tests/modules/config_translation_test/config_translation_test.module
Configuration Translation Test module.
dependency_version_test.module in core/modules/system/tests/modules/dependency_version_test/dependency_version_test.module
Module for testing the dependency version comparisons.

... See full list

8 string references to 'Extension'
ConvertImageEffect::submitConfigurationForm in core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php
custom_test_db.yml in core/modules/migrate/tests/modules/message_test/migrations/custom_test_db.yml
core/modules/migrate/tests/modules/message_test/migrations/custom_test_db.yml
d6_language_content_menu_settings.yml in core/modules/language/migrations/d6_language_content_menu_settings.yml
core/modules/language/migrations/d6_language_content_menu_settings.yml
d7_language_content_menu_settings.yml in core/modules/language/migrations/d7_language_content_menu_settings.yml
core/modules/language/migrations/d7_language_content_menu_settings.yml
FileViewsData::getViewsData in core/modules/file/src/FileViewsData.php
Returns views data for the entity type.

... See full list

File

core/lib/Drupal/Core/Extension/Extension.php, line 13

Namespace

Drupal\Core\Extension
View source
class Extension {
    
    /**
     * The type of the extension (e.g., 'module').
     *
     * @var string
     */
    protected $type;
    
    /**
     * The relative pathname of the extension (e.g., 'core/modules/node/node.info.yml').
     *
     * @var string
     */
    protected $pathname;
    
    /**
     * The filename of the main extension file (e.g., 'node.module').
     *
     * @var string|null
     */
    protected $filename;
    
    /**
     * 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;
    
    /**
     * The app root.
     *
     * @var string
     */
    protected $root;
    
    /**
     * The extension info array.
     */
    public array $info;
    
    /**
     * Constructs a new Extension object.
     *
     * @param string $root
     *   The app root.
     * @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
     *   (optional) The filename of the main extension file; e.g., 'node.module'.
     */
    public function __construct($root, $type, $pathname, $filename = NULL) {
        // @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));
        $this->root = $root;
        $this->type = $type;
        $this->pathname = $pathname;
        $this->filename = $filename;
    }
    
    /**
     * 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);
    }
    
    /**
     * 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 $this->root . '/' . $this->getPath() . '/' . $this->filename;
            return TRUE;
        }
        return FALSE;
    }
    
    /**
     * Returns SplFileInfo instance for the extension's info file.
     *
     * @return \SplFileInfo
     *   The object to access a file information of info file.
     *
     * @see https://www.php.net/manual/class.splfileinfo.php
     */
    public function getFileInfo() : \SplFileInfo {
        if (!isset($this->splFileInfo)) {
            $this->splFileInfo = new \SplFileInfo($this->root . '/' . $this->pathname);
        }
        return $this->splFileInfo;
    }
    
    /**
     * Magic method implementation to serialize the extension object.
     *
     * @return array
     *   The names of all variables that should be serialized.
     */
    public function __sleep() : array {
        // @todo \Drupal\Core\Extension\ThemeExtensionList is adding custom
        //   properties to the Extension object.
        $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);
    }
    
    /**
     * Magic method implementation to unserialize the extension object.
     */
    public function __wakeup() : void {
        // 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;
    }
    
    /**
     * Checks if an extension is marked as experimental.
     *
     * @return bool
     *   TRUE if an extension is marked as experimental, FALSE otherwise.
     */
    public function isExperimental() : bool {
        return isset($this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]) && $this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::EXPERIMENTAL;
    }
    
    /**
     * Checks if an extension is marked as obsolete.
     *
     * @return bool
     *   TRUE if an extension is marked as obsolete, FALSE otherwise.
     */
    public function isObsolete() : bool {
        // This function checks for 'lifecycle: obsolete' to determine if an
        // extension is marked as obsolete.
        return isset($this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER]) && $this->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::OBSOLETE;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
Extension::$filename protected property The filename of the main extension file (e.g., 'node.module').
Extension::$info public property The extension info array.
Extension::$pathname protected property The relative pathname of the extension (e.g., 'core/modules/node/node.info.yml').
Extension::$root protected property The app root.
Extension::$splFileInfo protected property An SplFileInfo instance for the extension's info file.
Extension::$type protected property The type of the extension (e.g., 'module').
Extension::getExtensionFilename public function Returns the name of the main extension file, if any. 1
Extension::getExtensionPathname public function Returns the relative path of the main extension file, if any. 1
Extension::getFileInfo public function Returns SplFileInfo instance for the extension's info file.
Extension::getFilename public function Returns the filename of the extension's info file. 1
Extension::getName public function Returns the internal name of the extension. 1
Extension::getPath public function Returns the relative path of the extension. 1
Extension::getPathname public function Returns the relative path and filename of the extension's info file. 1
Extension::getType public function Returns the type of the extension.
Extension::isExperimental public function Checks if an extension is marked as experimental. 1
Extension::isObsolete public function Checks if an extension is marked as obsolete. 1
Extension::load public function Loads the main extension file, if any. 1
Extension::__construct public function Constructs a new Extension object. 1
Extension::__sleep public function Magic method implementation to serialize the extension object.
Extension::__wakeup public function Magic method implementation to unserialize the extension object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.