utility.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // Invalidate the container.
  34. $kernel->invalidateContainer();
  35. // Prepare a NULL request.
  36. $kernel->prepareLegacyRequest($request);
  37. foreach (Cache::getBins() as $bin) {
  38. $bin->deleteAll();
  39. }
  40. // Disable recording of cached pages.
  41. \Drupal::service('page_cache_kill_switch')->trigger();
  42. drupal_flush_all_caches();
  43. // Restore Drupal's error and exception handlers.
  44. // @see \Drupal\Core\DrupalKernel::boot()
  45. set_error_handler('_drupal_error_handler');
  46. set_exception_handler('_drupal_exception_handler');
  47. }