class MissingClassDetectionClassLoader

Defines a classloader that detects missing classes.

This does not load classes. It allows calling code to explicitly check whether a class that was requested failed to be discovered by other class loaders.

It also works around a PHP limitation when it attempts to load a class that relies on a trait that does not exist. This is a common situation with Drupal plugins, which may be intended to be dormant unless certain other modules are installed.

@internal

Hierarchy

Expanded class hierarchy of MissingClassDetectionClassLoader

See also

https://github.com/php/php-src/issues/17959

1 file declares its use of MissingClassDetectionClassLoader
AttributeClassDiscovery.php in core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php

File

core/lib/Drupal/Component/Discovery/MissingClassDetectionClassLoader.php, line 22

Namespace

Drupal\Component\Discovery
View source
final class MissingClassDetectionClassLoader {
    
    /**
     * An array of detected missing traits.
     */
    protected array $missingTraits = [];
    
    /**
     * Flag indicating whether there was an attempt to load a missing class.
     */
    protected bool $missingClass = FALSE;
    
    /**
     * Records missing classes and aliases missing traits.
     *
     * This method is registered as a class loader during attribute discovery and
     * runs last. Any call to this method means that the requested class is
     * missing. If that class is a trait, it is aliased to a stub trait to avoid
     * an uncaught PHP fatal error.
     *
     * @param string $class
     *   The class name to load.
     */
    public function loadClass(string $class) : void {
        $this->missingClass = TRUE;
        if (str_ends_with($class, 'Trait')) {
            $this->missingTraits[] = $class;
            class_alias(StubTrait::class, $class);
        }
    }
    
    /**
     * Returns whether there was an attempt to load a missing class.
     *
     * @return bool
     *   TRUE if there was an attempt to load a missing class, otherwise FALSE.
     */
    public function hasMissingClass() : bool {
        return $this->missingClass;
    }
    
    /**
     * Returns all recorded missing traits since the last reset.
     *
     * @return string[]
     *   An array of traits recorded as missing.
     */
    public function getMissingTraits() : array {
        return $this->missingTraits;
    }
    
    /**
     * Resets class variables.
     */
    public function reset() : void {
        $this->missingClass = FALSE;
        $this->missingTraits = [];
    }

}

Members

Title Sort descending Modifiers Object type Summary
MissingClassDetectionClassLoader::$missingClass protected property Flag indicating whether there was an attempt to load a missing class.
MissingClassDetectionClassLoader::$missingTraits protected property An array of detected missing traits.
MissingClassDetectionClassLoader::getMissingTraits public function Returns all recorded missing traits since the last reset.
MissingClassDetectionClassLoader::hasMissingClass public function Returns whether there was an attempt to load a missing class.
MissingClassDetectionClassLoader::loadClass public function Records missing classes and aliases missing traits.
MissingClassDetectionClassLoader::reset public function Resets class variables.

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