You are here

public function CacheflushApi::clearModuleCache in CacheFlush 8

Clear modules cache.

File

src/Controller/CacheflushApi.php, line 180

Class

CacheflushApi
Returns responses for Cacheflush routes.

Namespace

Drupal\cacheflush\Controller

Code

public function clearModuleCache() {
  $module_handler = $this
    ->moduleHandler();

  // Invalidate the container.
  $this->container
    ->get('kernel')
    ->invalidateContainer();

  // Rebuild module and theme data.
  $module_data = system_rebuild_module_data();

  /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  $theme_handler = $this->container
    ->get('theme_handler');
  $theme_handler
    ->refreshInfo();

  // In case the active theme gets requested later in the same request we need
  // to reset the theme manager.
  $this->container
    ->get('theme.manager')
    ->resetActiveTheme();

  // Rebuild and reboot a new kernel. A simple DrupalKernel reboot is not
  // sufficient, since the list of enabled modules might have been adjusted
  // above due to changed code.
  $files = [];
  foreach ($module_data as $name => $extension) {
    if ($extension->status) {
      $files[$name] = $extension;
    }
  }
  $this->container
    ->get('kernel')
    ->updateModules($this
    ->moduleHandler()
    ->getModuleList(), $files);

  // New container, new module handler.
  $module_handler = $this
    ->moduleHandler();

  // Ensure that all modules that are currently supposed to be enabled are
  // actually loaded.
  $module_handler
    ->loadAll();

  // Rebuild all information based on new module data.
  $module_handler
    ->invokeAll('rebuild');

  // Re-initialize the maintenance theme, if the current request attempted to
  // use it. Unlike regular usages of this function, the installer and update
  // scripts need to flush all caches during GET requests/page building.
  if (function_exists('_drupal_maintenance_theme')) {
    $this->container
      ->get('theme.manager')
      ->resetActiveTheme();
    drupal_maintenance_theme();
  }
}