You are here

class SophronEventSubscriber in Sophron 8

Sophron's module Event Subscriber.

Hierarchy

  • class \Drupal\sophron\EventSubscriber\SophronEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of SophronEventSubscriber

1 string reference to 'SophronEventSubscriber'
sophron.services.yml in ./sophron.services.yml
sophron.services.yml
1 service uses SophronEventSubscriber
sophron.event_subscriber in ./sophron.services.yml
Drupal\sophron\EventSubscriber\SophronEventSubscriber

File

src/EventSubscriber/SophronEventSubscriber.php, line 15

Namespace

Drupal\sophron\EventSubscriber
View source
class SophronEventSubscriber implements EventSubscriberInterface {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The module configuration settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $sophronSettings;

  /**
   * Constructs a SophronEventSubscriber object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
    $this->sophronSettings = $this->configFactory
      ->get('sophron.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      MapEvent::INIT => 'initializeMap',
    ];
  }

  /**
   * Reacts to a 'sophron.map.initialize' event.
   *
   * Alters the map's mappings at run-time with additional commands stored in
   * the module settings.
   *
   * @param \Drupal\sophron\Event\MapEvent $event
   *   Sophron's map event.
   */
  public function initializeMap(MapEvent $event) {
    $map_commands = $this->sophronSettings
      ->get('map_commands');
    $map = MapHandler::map($event
      ->getMapClass());
    foreach ($map_commands as $command) {
      $method = isset($command[0]) ? $command[0] : '';
      $args = isset($command[1]) ? $command[1] : [];
      try {
        if (!is_callable([
          $map,
          $method,
        ])) {
          throw new \InvalidArgumentException("Non-existing mapping method '{$method}'");
        }
        call_user_func_array([
          $map,
          $method,
        ], $args);
      } catch (MappingException $e) {
        $event
          ->addError((string) $method, (array) $args, 'Mapping', $e
          ->getMessage());
      } catch (MalformedTypeException $e) {
        $event
          ->addError((string) $method, (array) $args, 'Invalid MIME type syntax', $e
          ->getMessage());
      } catch (\Exception $e) {
        $event
          ->addError((string) $method, (array) $args, 'Other', $e
          ->getMessage());
      } catch (\Error $e) {
        $event
          ->addError((string) $method, (array) $args, 'Error', $e
          ->getMessage());
      }
    }
    $map
      ->sort();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SophronEventSubscriber::$configFactory protected property The configuration factory.
SophronEventSubscriber::$sophronSettings protected property The module configuration settings.
SophronEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SophronEventSubscriber::initializeMap public function Reacts to a 'sophron.map.initialize' event.
SophronEventSubscriber::__construct public function Constructs a SophronEventSubscriber object.