utility.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. use Composer\Autoload\ClassLoader;
  11. /**
  12. * Rebuilds all caches even when Drupal itself does not work.
  13. *
  14. * @param \Composer\Autoload\ClassLoader $class_loader
  15. * The class loader.
  16. * @param \Symfony\Component\HttpFoundation\Request $request
  17. * The current request.
  18. *
  19. * @see rebuild.php
  20. */
  21. function drupal_rebuild(ClassLoader $class_loader, Request $request) {
  22. // Remove Drupal's error and exception handlers; they rely on a working
  23. // service container and other subsystems and will only cause a fatal error
  24. // that hides the actual error.
  25. restore_error_handler();
  26. restore_exception_handler();
  27. // Force kernel to rebuild php cache.
  28. PhpStorageFactory::get('twig')->deleteAll();
  29. // Bootstrap up to where caches exist and clear them.
  30. $kernel = new DrupalKernel('prod', $class_loader);
  31. $kernel->setSitePath(DrupalKernel::findSitePath($request));
  32. // Invalidate the container.
  33. $kernel->invalidateContainer();
  34. // Prepare a NULL request.
  35. $kernel->prepareLegacyRequest($request);
  36. foreach (Cache::getBins() as $bin) {
  37. $bin->deleteAll();
  38. }
  39. // Disable recording of cached pages.
  40. \Drupal::service('page_cache_kill_switch')->trigger();
  41. drupal_flush_all_caches();
  42. // Restore Drupal's error and exception handlers.
  43. // @see \Drupal\Core\DrupalKernel::boot()
  44. set_error_handler('_drupal_error_handler');
  45. set_exception_handler('_drupal_exception_handler');
  46. }