You are here

final class YamlDefinitionsLoader in Hook Event Dispatcher 8

Class YamlDefinitionsLoader.

@package Drupal\Tests\hook_event_dispatcher\Preprocess\Helpers

Hierarchy

Expanded class hierarchy of YamlDefinitionsLoader

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

... See full list

File

tests/src/Unit/Preprocess/Helpers/YamlDefinitionsLoader.php, line 13

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers
View source
final class YamlDefinitionsLoader {

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

  /**
   * Factory mapper.
   *
   * @var \Drupal\hook_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() {
    $yaml = new Parser();
    $content = file_get_contents(dirname(dirname(dirname(dirname(dirname(__DIR__))))) . '/hook_event_dispatcher.services.yml');
    $services = $yaml
      ->parse($content)['services'];

    // Remove the Manager.
    unset($services['hook_event_dispatcher.manager']);
    $factories = $this->services = $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() {
    $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\hook_event_dispatcher\Unit\Preprocess\Helpers\YamlDefinitionsLoader
   *   Existing or new YamlDefinitionsLoader instance.
   */
  public static function getInstance() {
    if (static::$instance === NULL) {
      static::$instance = new static();
    }
    return static::$instance;
  }

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

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

  /**
   * Get the Factories.
   *
   * @return array
   *   Factory definitions.
   */
  public function getFactories() {
    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.