You are here

final class YamlDefinitionsLoader in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/preprocess_event_dispatcher/tests/src/Unit/Helpers/YamlDefinitionsLoader.php \Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader

Class YamlDefinitionsLoader.

Hierarchy

Expanded class hierarchy of YamlDefinitionsLoader

6 files declare their use of YamlDefinitionsLoader
EntityEventTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventTest.php
EntityEventVariablesTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/EntityEventVariablesTest.php
FactoryMapperTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/FactoryMapperTest.php
OtherEventTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventTest.php
OtherEventVariablesTest.php in modules/preprocess_event_dispatcher/tests/src/Unit/OtherEventVariablesTest.php

... See full list

File

modules/preprocess_event_dispatcher/tests/src/Unit/Helpers/YamlDefinitionsLoader.php, line 13

Namespace

Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers
View source
final class YamlDefinitionsLoader {

  /**
   * Singleton instance of this class.
   *
   * @var YamlDefinitionsLoader
   */
  private static $instance;

  /**
   * Factory mapper.
   *
   * @var \Drupal\preprocess_event_dispatcher\Service\PreprocessEventFactoryMapper
   */
  private $mapper;

  /**
   * Service definitions loaded from the YAML services file.
   *
   * @var array
   */
  private $services;

  /**
   * Factory definitions loaded from the YAML services file.
   *
   * @var array
   */
  private $factories;

  /**
   * YamlDefinitionsLoader constructor.
   */
  private function __construct() {
    $this
      ->loadDefinitionsFromServicesYaml();
    $this
      ->setUpFactoriesMapper();
  }

  /**
   * Load the definitions from the services YAML.
   */
  private function loadDefinitionsFromServicesYaml() : void {
    $yaml = new Parser();
    $content = file_get_contents(dirname(__DIR__, 4) . '/preprocess_event_dispatcher.services.yml');
    $factories = $this->services = $yaml
      ->parse($content)['services'];

    // Remove the Service and Factory Mapper.
    unset($factories['preprocess_event.service'], $factories['preprocess_event.factory_mapper']);
    $this->factories = $factories;
  }

  /**
   * Set up Factories Mapper.
   */
  private function setUpFactoriesMapper() : void {
    $this->mapper = new PreprocessEventFactoryMapper();
    foreach ($this->factories as $entry) {
      $factory = new $entry['class']();
      $this->mapper
        ->addFactory($factory);
    }
  }

  /**
   * Singleton get instance function.
   *
   * Only need to load the files once from the filesystem.
   *
   * @return \Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers\YamlDefinitionsLoader
   *   Existing or new YamlDefinitionsLoader instance.
   */
  public static function getInstance() : YamlDefinitionsLoader {
    if (static::$instance === NULL) {
      static::$instance = new static();
    }
    return static::$instance;
  }

  /**
   * Get the FactoryMapper.
   *
   * @return \Drupal\preprocess_event_dispatcher\Service\PreprocessEventFactoryMapper
   *   Factory mapper.
   */
  public function getMapper() : PreprocessEventFactoryMapper {
    return $this->mapper;
  }

  /**
   * Get the Services.
   *
   * @return array
   *   Service definitions.
   */
  public function getServices() : array {
    return $this->services;
  }

  /**
   * Get the Factories.
   *
   * @return array
   *   Factory definitions.
   */
  public function getFactories() : array {
    return $this->factories;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlDefinitionsLoader::$factories private property Factory definitions loaded from the YAML services file.
YamlDefinitionsLoader::$instance private static property Singleton instance of this class.
YamlDefinitionsLoader::$mapper private property Factory mapper.
YamlDefinitionsLoader::$services private property Service definitions loaded from the YAML services file.
YamlDefinitionsLoader::getFactories public function Get the Factories.
YamlDefinitionsLoader::getInstance public static function Singleton get instance function.
YamlDefinitionsLoader::getMapper public function Get the FactoryMapper.
YamlDefinitionsLoader::getServices public function Get the Services.
YamlDefinitionsLoader::loadDefinitionsFromServicesYaml private function Load the definitions from the services YAML.
YamlDefinitionsLoader::setUpFactoriesMapper private function Set up Factories Mapper.
YamlDefinitionsLoader::__construct private function YamlDefinitionsLoader constructor.