ServiceContainer.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @file
  4. * Contains ServiceContainer
  5. */
  6. use Drupal\service_container\DependencyInjection\CachedContainerBuilder;
  7. use Drupal\service_container\DependencyInjection\ServiceProviderPluginManager;
  8. /**
  9. * Static Service Container wrapper extension - initializes the container.
  10. */
  11. class ServiceContainer extends Drupal {
  12. /**
  13. * Initializes the container.
  14. *
  15. * This can be safely called from hook_boot() because the container will
  16. * only be build if we have reached the DRUPAL_BOOTSTRAP_FULL phase.
  17. *
  18. * @return bool
  19. * TRUE when the container was initialized, FALSE otherwise.
  20. */
  21. public static function init() {
  22. // If this is set already, just return.
  23. if (isset(static::$container)) {
  24. return TRUE;
  25. }
  26. $container_builder = static::getContainerBuilder();
  27. if ($container_builder->isCached()) {
  28. static::$container = $container_builder->compile();
  29. static::dispatchStaticEvent('containerReady', array(static::$container));
  30. return TRUE;
  31. }
  32. // If we have not yet fully bootstrapped, we can't build the container.
  33. if (drupal_bootstrap(NULL, FALSE) != DRUPAL_BOOTSTRAP_FULL) {
  34. return FALSE;
  35. }
  36. // Rebuild the container.
  37. static::$container = $container_builder->compile();
  38. static::dispatchStaticEvent('containerReady', array(static::$container));
  39. return (bool) static::$container;
  40. }
  41. /**
  42. * Dispatches an event to static classes.
  43. *
  44. * This is needed to inform other static classes when the container is ready.
  45. *
  46. * @param string $event
  47. * The member function to call.
  48. * @param array $arguments
  49. * The arguments to pass.
  50. */
  51. protected static function dispatchStaticEvent($event, $arguments) {
  52. $event_listeners = static::$container->getParameter('service_container.static_event_listeners');
  53. foreach ($event_listeners as $class) {
  54. $function = $class . '::' . $event;
  55. if (is_callable($function)) {
  56. call_user_func_array($function, $arguments);
  57. }
  58. }
  59. }
  60. /**
  61. * Reset the internal cache.
  62. *
  63. * Note: This is just thought for tests.
  64. */
  65. public static function reset() {
  66. static::getContainerBuilder()->reset();
  67. static::$container = NULL;
  68. }
  69. /**
  70. * @return \Drupal\service_container\DependencyInjection\CachedContainerBuilder
  71. */
  72. protected static function getContainerBuilder() {
  73. $service_provider_manager = new ServiceProviderPluginManager();
  74. // This is an internal API, but we need the cache object.
  75. $cache = _cache_get_object('cache');
  76. $container_builder = new CachedContainerBuilder($service_provider_manager, $cache);
  77. return $container_builder;
  78. }
  79. }