You are here

class ComponentDiscovery in Lightning Core 8.3

Same name and namespace in other branches
  1. 8.5 src/ComponentDiscovery.php \Drupal\lightning_core\ComponentDiscovery
  2. 8.4 src/ComponentDiscovery.php \Drupal\lightning_core\ComponentDiscovery

Helper object to locate Lightning components and sub-components.

Hierarchy

Expanded class hierarchy of ComponentDiscovery

2 files declare their use of ComponentDiscovery
ComponentDiscoveryTest.php in tests/src/Kernel/ComponentDiscoveryTest.php
Telemetry.php in modules/acquia_telemetry/src/Telemetry.php

File

src/ComponentDiscovery.php, line 11

Namespace

Drupal\lightning_core
View source
class ComponentDiscovery {

  /**
   * Prefix that Lightning components are expected to start with.
   */
  const COMPONENT_PREFIX = 'lightning_';

  /**
   * The extension discovery iterator.
   *
   * @var \Drupal\Core\Extension\ExtensionDiscovery
   */
  protected $discovery;

  /**
   * The Lightning profile extension object.
   *
   * @var Extension
   */
  protected $profile;

  /**
   * Cache of all discovered components.
   *
   * @var Extension[]
   */
  protected $components;

  /**
   * ComponentDiscovery constructor.
   *
   * @param string $app_root
   *   The application root directory.
   */
  public function __construct($app_root) {
    $this->discovery = new ExtensionDiscovery($app_root);
  }

  /**
   * Returns an extension object for the Lightning profile.
   *
   * @return \Drupal\Core\Extension\Extension
   *   The Lightning profile extension object.
   *
   * @throws \RuntimeException
   *   If the Lightning profile is not found in the system.
   */
  protected function getProfile() {
    if (empty($this->profile)) {
      $profiles = $this->discovery
        ->scan('profile');
      if (empty($profiles['lightning'])) {
        throw new \RuntimeException('Lightning profile not found.');
      }
      $this->profile = $profiles['lightning'];
    }
    return $this->profile;
  }

  /**
   * Returns extension objects for all Lightning components.
   *
   * @return Extension[]
   *   Array of extension objects for all Lightning components.
   */
  public function getAll() {
    if (is_null($this->components)) {
      $identifier = self::COMPONENT_PREFIX;
      $filter = function (Extension $module) use ($identifier) {
        return strpos($module
          ->getName(), $identifier) === 0;
      };
      $this->components = array_filter($this->discovery
        ->scan('module'), $filter);
    }
    return $this->components;
  }

  /**
   * Returns extension objects for all main Lightning components.
   *
   * @return Extension[]
   *   Array of extension objects for top-level Lightning components.
   */
  public function getMainComponents() {
    $identifier = self::COMPONENT_PREFIX;
    $filter = function (Extension $module) use ($identifier) {

      // Assumes that:
      // 1. Lightning sub-components are always in a sub-directory within the
      //    main component.
      // 2. The main component's directory starts with "lightning_".
      // E.g.: "/lightning_core/modules/lightning_search".
      $path = explode(DIRECTORY_SEPARATOR, $module
        ->getPath());
      $parent = $path[count($path) - 3];
      return strpos($parent, $identifier) !== 0;
    };
    return array_filter($this
      ->getAll(), $filter);
  }

  /**
   * Returns extension object for all Lightning sub-components.
   *
   * @return Extension[]
   *   Array of extension objects for Lightning sub-components.
   */
  public function getSubComponents() {
    return array_diff_key($this
      ->getAll(), $this
      ->getMainComponents());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComponentDiscovery::$components protected property Cache of all discovered components.
ComponentDiscovery::$discovery protected property The extension discovery iterator.
ComponentDiscovery::$profile protected property The Lightning profile extension object.
ComponentDiscovery::COMPONENT_PREFIX constant Prefix that Lightning components are expected to start with.
ComponentDiscovery::getAll public function Returns extension objects for all Lightning components.
ComponentDiscovery::getMainComponents public function Returns extension objects for all main Lightning components.
ComponentDiscovery::getProfile protected function Returns an extension object for the Lightning profile.
ComponentDiscovery::getSubComponents public function Returns extension object for all Lightning sub-components.
ComponentDiscovery::__construct public function ComponentDiscovery constructor.