YamlDefinitionsLoader.php in Hook Event Dispatcher 3.x
File
modules/preprocess_event_dispatcher/tests/src/Unit/Helpers/YamlDefinitionsLoader.php
View source
<?php
namespace Drupal\Tests\preprocess_event_dispatcher\Unit\Helpers;
use Drupal\preprocess_event_dispatcher\Service\PreprocessEventFactoryMapper;
use Symfony\Component\Yaml\Parser;
use function dirname;
use function file_get_contents;
final class YamlDefinitionsLoader {
private static $instance;
private $mapper;
private $services;
private $factories;
private function __construct() {
$this
->loadDefinitionsFromServicesYaml();
$this
->setUpFactoriesMapper();
}
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'];
unset($factories['preprocess_event.service'], $factories['preprocess_event.factory_mapper']);
$this->factories = $factories;
}
private function setUpFactoriesMapper() : void {
$this->mapper = new PreprocessEventFactoryMapper();
foreach ($this->factories as $entry) {
$factory = new $entry['class']();
$this->mapper
->addFactory($factory);
}
}
public static function getInstance() : YamlDefinitionsLoader {
if (static::$instance === NULL) {
static::$instance = new static();
}
return static::$instance;
}
public function getMapper() : PreprocessEventFactoryMapper {
return $this->mapper;
}
public function getServices() : array {
return $this->services;
}
public function getFactories() : array {
return $this->factories;
}
}