You are here

class HabitatSubscriber in Habitat 8

Hierarchy

  • class \Drupal\habitat\EventSubscriber\HabitatSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of HabitatSubscriber

1 string reference to 'HabitatSubscriber'
habitat.services.yml in ./habitat.services.yml
habitat.services.yml
1 service uses HabitatSubscriber
habitat_event_subscriber in ./habitat.services.yml
Drupal\habitat\EventSubscriber\HabitatSubscriber

File

src/EventSubscriber/HabitatSubscriber.php, line 16
Contains \Drupal\habitat\EventSubscriber\HabitatSubscriber.

Namespace

Drupal\habitat\EventSubscriber
View source
class HabitatSubscriber implements EventSubscriberInterface {
  public function ensureHabitat(GetResponseEvent $event) {
    $habitat_variable = \Drupal::config('habitat.settings')
      ->get('habitat_variable');
    if ($habitat = Settings::get($habitat_variable)) {
      $this
        ->ensureModulesDisabled($habitat);
      $this
        ->ensureModulesEnabled($habitat);
    }
  }
  public function ensureModulesDisabled($habitat) {
    $uninstalled_modules = \Drupal::config('habitat.settings')
      ->get('habitat_uninstall_' . $habitat);
    if (count($uninstalled_modules)) {
      $module_installer = \Drupal::service('module_installer');
      foreach ($uninstalled_modules as $module) {
        if (\Drupal::moduleHandler()
          ->moduleExists($module)) {
          $module_installer
            ->uninstall(array(
            $module,
          ));
          \Drupal::logger('habitat')
            ->info('%module was uninstalled for the %habitat habitat', array(
            '%module' => $module,
            '%habitat' => $habitat,
          ));
        }
      }
    }
  }
  public function ensureModulesEnabled($habitat) {
    $installed_modules = \Drupal::config('habitat.settings')
      ->get('habitat_install_' . $habitat);
    if (count($installed_modules)) {
      $module_installer = \Drupal::service('module_installer');
      foreach ($installed_modules as $module) {
        if (!\Drupal::moduleHandler()
          ->moduleExists($module)) {
          $module_installer
            ->install(array(
            $module,
          ));
          \Drupal::logger('habitat')
            ->info('%module was installed for the %habitat habitat', array(
            '%module' => $module,
            '%habitat' => $habitat,
          ));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array(
      'ensureHabitat',
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HabitatSubscriber::ensureHabitat public function
HabitatSubscriber::ensureModulesDisabled public function
HabitatSubscriber::ensureModulesEnabled public function
HabitatSubscriber::getSubscribedEvents static function Returns an array of event names this subscriber wants to listen to.