utility.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * Miscellaneous functions.
  5. */
  6. use Drupal\Core\PhpStorage\PhpStorageFactory;
  7. use Drupal\Core\Cache\Cache;
  8. use Drupal\Core\DrupalKernel;
  9. use Symfony\Component\HttpFoundation\Request;
  10. /**
  11. * Rebuilds all caches even when Drupal itself does not work.
  12. *
  13. * @param $class_loader
  14. * The class loader. Normally Composer's ClassLoader, as included by the
  15. * front controller, but may also be decorated; e.g.,
  16. * \Symfony\Component\ClassLoader\ApcClassLoader, \Symfony\Component\ClassLoader\WinCacheClassLoader, or \Symfony\Component\ClassLoader\XcacheClassLoader
  17. * @param \Symfony\Component\HttpFoundation\Request $request
  18. * The current request.
  19. *
  20. * @see rebuild.php
  21. */
  22. function drupal_rebuild($class_loader, Request $request) {
  23. // Remove Drupal's error and exception handlers; they rely on a working
  24. // service container and other subsystems and will only cause a fatal error
  25. // that hides the actual error.
  26. restore_error_handler();
  27. restore_exception_handler();
  28. // Force kernel to rebuild php cache.
  29. PhpStorageFactory::get('twig')->deleteAll();
  30. // Bootstrap up to where caches exist and clear them.
  31. $kernel = new DrupalKernel('prod', $class_loader);
  32. $kernel->setSitePath(DrupalKernel::findSitePath($request));
  33. $kernel->boot();
  34. $kernel->preHandle($request);
  35. // Ensure our request includes the session if appropriate.
  36. if (PHP_SAPI !== 'cli') {
  37. $request->setSession($kernel->getContainer()->get('session'));
  38. }
  39. // Invalidate the container.
  40. $kernel->invalidateContainer();
  41. foreach (Cache::getBins() as $bin) {
  42. $bin->deleteAll();
  43. }
  44. // Disable recording of cached pages.
  45. \Drupal::service('page_cache_kill_switch')->trigger();
  46. drupal_flush_all_caches();
  47. // Restore Drupal's error and exception handlers.
  48. // @see \Drupal\Core\DrupalKernel::boot()
  49. set_error_handler('_drupal_error_handler');
  50. set_exception_handler('_drupal_exception_handler');
  51. }