You are here

public static function BusinessRulesListener::getSubscribedEvents in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/BusinessRulesListener.php \Drupal\business_rules\EventSubscriber\BusinessRulesListener::getSubscribedEvents()

File

src/EventSubscriber/BusinessRulesListener.php, line 117

Class

BusinessRulesListener
Class BusinessRulesListener.

Namespace

Drupal\business_rules\EventSubscriber

Code

public static function getSubscribedEvents() {
  $return['business_rules.item_pos_delete'] = 'itemPosDelete';
  $return[KernelEvents::TERMINATE][] = [
    'onTerminate',
    100,
  ];

  // If there is no state service there is nothing we can do here. This static
  // method could be called early when the container is built, so the state
  // service might not always be available.
  if (!\Drupal::hasService('state')) {
    return self::$staticEvents;
  }

  // If there is no container service there is not possible to load any event.
  // As this method can be called before the container is ready, it might not
  // be available.
  // To avoid the necessity to manually clear all caches via user interface,
  // we are getting the plugin definition using this ugly way.
  if (!\Drupal::hasContainer() || !\Drupal::hasService('plugin.manager.business_rules.reacts_on')) {
    $query = Database::getConnection()
      ->query('SELECT value FROM {key_value} WHERE collection = :collection AND name = :name', [
      ':collection' => 'state',
      ':name' => 'system.module.files',
    ])
      ->fetchCol();
    $modules = [];
    if (isset($query[0])) {
      $modules = unserialize($query[0]);
    }
    foreach ($modules as $name => $module) {
      $arr = explode('/', $module);
      unset($arr[count($arr) - 1]);
      $path = implode('/', $arr);

      // Skip core modules.
      if ($arr[0] != 'core') {
        $root_namespaces["Drupal\\{$name}"] = "{$path}/src";
      }
    }
    $root_namespaces['_serviceId'] = 'container.namespaces';
    $root_namespaces = new \ArrayIterator($root_namespaces);
    $annotation = new AnnotatedClassDiscovery('/Plugin/BusinessRulesReactsOn', $root_namespaces, 'Drupal\\business_rules\\Annotation\\BusinessRulesReactsOn');
    $eventsDefinitions = $annotation
      ->getDefinitions();
  }
  else {

    // If we have the container, we can get the definitions using the correct
    // process.
    $container = \Drupal::getContainer();
    $reactionEvents = $container
      ->get('plugin.manager.business_rules.reacts_on');
    $eventsDefinitions = $reactionEvents
      ->getDefinitions();
  }
  foreach ($eventsDefinitions as $event) {
    $return[$event['eventName']] = [
      'process',
      $event['priority'],
    ];
  }
  return $return;
}