utility.inc 1.57 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Miscellaneous functions.
 */

8
use Drupal\Core\PhpStorage\PhpStorageFactory;
9
use Drupal\Core\Cache\Cache;
10 11 12
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Composer\Autoload\ClassLoader;
13

14 15 16
/**
 * Rebuilds all caches even when Drupal itself does not work.
 *
17 18
 * @param \Composer\Autoload\ClassLoader $class_loader
 *   The class loader.
19 20 21
 * @param \Symfony\Component\HttpFoundation\Request $request
 *   The current request.
 *
22 23
 * @see rebuild.php
 */
24
function drupal_rebuild(ClassLoader $class_loader, Request $request) {
25 26 27 28 29 30
  // Remove Drupal's error and exception handlers; they rely on a working
  // service container and other subsystems and will only cause a fatal error
  // that hides the actual error.
  restore_error_handler();
  restore_exception_handler();

31
  // Force kernel to rebuild php cache.
32 33
  PhpStorageFactory::get('twig')->deleteAll();

34
  // Bootstrap up to where caches exist and clear them.
35
  $kernel = new DrupalKernel('prod', $class_loader);
36
  $kernel->setSitePath(DrupalKernel::findSitePath($request));
37 38 39 40 41

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

  // Prepare a NULL request.
42 43
  $kernel->prepareLegacyRequest($request);

44 45 46 47
  foreach (Cache::getBins() as $bin) {
    $bin->deleteAll();
  }

48 49
  // Disable recording of cached pages.
  \Drupal::service('page_cache_kill_switch')->trigger();
50

51
  drupal_flush_all_caches();
52 53

  // Restore Drupal's error and exception handlers.
54
  // @see \Drupal\Core\DrupalKernel::boot()
55 56
  set_error_handler('_drupal_error_handler');
  set_exception_handler('_drupal_exception_handler');
57
}