Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Extension.php

class Extension

Same name in this branch
  1. 11.1.x vendor/phar-io/manifest/src/values/Extension.php \PharIo\Manifest\Extension
  2. 11.1.x vendor/symfony/http-kernel/DependencyInjection/Extension.php \Symfony\Component\HttpKernel\DependencyInjection\Extension
  3. 11.1.x vendor/symfony/dependency-injection/Extension/Extension.php \Symfony\Component\DependencyInjection\Extension\Extension
  4. 11.1.x core/lib/Drupal/Core/Extension/Extension.php \Drupal\Core\Extension\Extension
  5. 11.1.x core/modules/system/src/Plugin/migrate/source/Extension.php \Drupal\system\Plugin\migrate\source\Extension

Defines an extension (file) object.

Bundled version of \Drupal\Core\Extension\Extension.

Hierarchy

  • class \mglaman\PHPStanDrupal\Drupal\Extension

Expanded class hierarchy of Extension

1 file declares its use of Extension
ExtensionDiscovery.php in vendor/mglaman/phpstan-drupal/src/Drupal/ExtensionDiscovery.php
14 string references to 'Extension'
Application::writePharExtensionInformation in vendor/phpunit/phpunit/src/TextUI/Application.php
@psalm-param ?list<string> $pharExtensions
Bundle::getContainerExtensionClass in vendor/symfony/http-kernel/Bundle/Bundle.php
Returns the bundle's container extension class.
ContainsElement::getExtensionElement in vendor/phar-io/manifest/src/xml/ContainsElement.php
ConvertImageEffect::submitConfigurationForm in core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php
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

... See full list

File

vendor/mglaman/phpstan-drupal/src/Drupal/Extension.php, line 19

Namespace

mglaman\PHPStanDrupal\Drupal
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;
    
    /**
     * @var string
     */
    public $subpath = '';
    
    /**
     * @var string
     */
    public $origin = '';
    
    /**
     * @var array|null
     */
    private $info;
    
    /**
     * @var string[]|null
     */
    private $dependencies;
    
    /**
     * 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) {
        $this->root = $root;
        $this->type = $type;
        $this->pathname = $pathname;
        $this->filename = $filename;
    }
    
    /**
     * Returns the type of the extension.
     *
     * @return string
     */
    public function getType() : string {
        return $this->type;
    }
    
    /**
     * Returns the internal name of the extension.
     *
     * @return string
     */
    public function getName() : string {
        return basename($this->pathname, '.info.yml');
    }
    
    /**
     * Returns the relative path of the extension.
     *
     * @return string
     */
    public function getPath() : string {
        return dirname($this->pathname);
    }
    public function getAbsolutePath() : string {
        return $this->root . DIRECTORY_SEPARATOR . $this->getPath();
    }
    
    /**
     * Returns the relative path and filename of the extension's info file.
     *
     * @return string
     */
    public function getPathname() : string {
        return $this->pathname;
    }
    
    /**
     * Returns the filename of the extension's info file.
     *
     * @return string
     */
    public function getFilename() : string {
        return basename($this->pathname);
    }
    
    /**
     * Returns the relative path of the main extension file, if any.
     *
     * @return string|null
     */
    public function getExtensionPathname() : ?string {
        if ($this->filename !== null) {
            return $this->getPath() . '/' . $this->filename;
        }
        return null;
    }
    
    /**
     * Returns the name of the main extension file, if any.
     *
     * @return string|null
     */
    public function getExtensionFilename() : ?string {
        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() : bool {
        if ($this->filename !== null) {
            include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
            return true;
        }
        return false;
    }
    
    /**
     * @return string[]
     */
    public function getDependencies() : array {
        if (is_array($this->dependencies)) {
            return $this->dependencies;
        }
        $info = $this->parseInfo();
        $dependencies = $info['dependencies'] ?? [];
        if ($dependencies === []) {
            return $this->dependencies = $dependencies;
        }
        $this->dependencies = [];
        // @see \Drupal\Core\Extension\Dependency::createFromString().
        foreach ($dependencies as $dependency) {
            if (strpos($dependency, ':') !== false) {
                [
                    ,
                    $dependency,
                ] = explode(':', $dependency);
            }
            $parts = explode('(', $dependency, 2);
            $this->dependencies[] = trim($parts[0]);
        }
        return $this->dependencies;
    }
    private function parseInfo() : array {
        if (is_array($this->info)) {
            return $this->info;
        }
        $infoContent = file_get_contents(sprintf('%s/%s', $this->root, $this->getPathname()));
        if (false === $infoContent) {
            throw new RuntimeException(sprintf('Cannot read "%s', $this->getPathname()));
        }
        return $this->info = Yaml::parse($infoContent);
    }

}

Members

Title Sort descending Modifiers Object type Summary
Extension::$dependencies private property
Extension::$filename protected property The filename of the main extension file (e.g., &#039;node.module&#039;).
Extension::$info private property
Extension::$origin public property
Extension::$pathname protected property The relative pathname of the extension (e.g.,
&#039;core/modules/node/node.info.yml&#039;).
Extension::$root protected property The app root.
Extension::$splFileInfo protected property An SplFileInfo instance for the extension&#039;s info file.
Extension::$subpath public property
Extension::$type protected property The type of the extension (e.g., &#039;module&#039;).
Extension::getAbsolutePath public function
Extension::getDependencies public function
Extension::getExtensionFilename public function Returns the name of the main extension file, if any.
Extension::getExtensionPathname public function Returns the relative path of the main extension file, if any.
Extension::getFilename public function Returns the filename of the extension&#039;s info file.
Extension::getName public function Returns the internal name of the extension.
Extension::getPath public function Returns the relative path of the extension.
Extension::getPathname public function Returns the relative path and filename of the extension&#039;s info file.
Extension::getType public function Returns the type of the extension.
Extension::load public function Loads the main extension file, if any.
Extension::parseInfo private function
Extension::__construct public function Constructs a new Extension object.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal