diff --git a/core/lib/Drupal/Core/Controller/ControllerBase.php b/core/lib/Drupal/Core/Controller/ControllerBase.php index d0718ff..73c70fe 100644 --- a/core/lib/Drupal/Core/Controller/ControllerBase.php +++ b/core/lib/Drupal/Core/Controller/ControllerBase.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Controller; -use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\HttpFoundation\RedirectResponse; /** @@ -29,7 +28,52 @@ * * @see \Drupal\Core\Controller\ControllerInterface */ -abstract class ControllerBase extends ContainerAware { +abstract class ControllerBase { + + /** + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** + * @var \Drupal\Core\Language\LanguageManager + */ + protected $languageManager; + + /** + * @var \Drupal\Core\StringTranslation\TranslationInterface + */ + protected $translationManager; + + /** + * @var \Drupal\Core\Config\Config + */ + protected $configFactory; + + /** + * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface + */ + protected $kv; + + /** + * @var \Drupal\Core\Routing\UrlGeneratorInterface + */ + protected $urlGenerator; + + /** + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** + * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface + */ + protected $stateService; + + /** + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; /** * Retrieves the entity manager service. @@ -38,7 +82,10 @@ * The entity manager service. */ protected function entityManager() { - return $this->container->get('plugin.manager.entity'); + if (!$this->entityManager) { + $this->entityManager = $this->container->get('plugin.manager.entity'); + } + return $this->entityManager; } /** @@ -52,7 +99,7 @@ protected function entityManager() { * The cache object associated with the specified bin. */ protected function cache($bin = 'cache') { - return $this->container->get('cache.' . $bin); + return $this->container()->get('cache.' . $bin); } /** @@ -72,7 +119,10 @@ protected function cache($bin = 'cache') { * A configuration object. */ protected function config($name) { - return $this->container->get('config.factory')->get($name); + if (!$this->configFactory) { + $this->configFactory = $this->container()->get('config.factory')->get($name); + } + return $this->configFactory; } /** @@ -84,7 +134,10 @@ protected function config($name) { * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ protected function keyValue($collection) { - return $this->container->get('keyvalue')->get($collection); + if (!$this->kv) { + $this->kv = $this->container()->get('keyvalue')->get($collection); + } + return $this->kv; } /** @@ -99,7 +152,10 @@ protected function keyValue($collection) { * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ protected function state() { - return $this->container->get('state'); + if (!$this->stateService) { + $this->stateService = $this->container()->get('state'); + } + return $this->stateService; } /** @@ -108,7 +164,10 @@ protected function state() { * @return \Drupal\Core\Extension\ModuleHandlerInterface */ protected function moduleHandler() { - return $this->container->get('module_handler'); + if (!$this->moduleHandler) { + $this->moduleHandler = $this->container()->get('module_handler'); + } + return $this->moduleHandler; } /** @@ -118,7 +177,10 @@ protected function moduleHandler() { * The url generator service. */ protected function urlGenerator() { - return $this->container->get('url_generator'); + if (!$this->urlGenerator) { + $this->urlGenerator = $this->container()->get('url_generator'); + } + return $this->urlGenerator; } /** @@ -128,7 +190,10 @@ protected function urlGenerator() { * The current user. */ protected function currentUser() { - return $this->container->get('current_user'); + if (!$this->currentUser) { + $this->currentUser = $this->container()->get('current_user'); + } + return $this->currentUser; } /** @@ -151,7 +216,20 @@ protected function currentUser() { * The translated string. */ protected function t($string, array $args = array(), array $options = array()) { - return $this->container->get('string_translation')->translate($string, $args, $options); + return $this->getTranslationManager()->translate($string, $args, $options); + } + + /** + * Returns the translation manager. + * + * @return \Drupal\Core\StringTranslation\TranslationInterface + * The translation manager. + */ + protected function translationManager() { + if (!$this->translationManager) { + $this->translationManager = $this->container()->get('string_translation'); + } + return $this->translationManager; } /** @@ -161,7 +239,20 @@ protected function t($string, array $args = array(), array $options = array()) { * The language manager. */ protected function languageManager() { - return $this->container->get('language_manager'); + if (!$this->languageManager) { + $this->languageManager = $this->container()->get('language_manager'); + } + return $this->languageManager; + } + + /** + * Returns the service container. + * + * @return \Symfony\Component\DependencyInjection\ContainerInterface $container + * The service container. + */ + protected function container() { + return \Drupal::getContainer(); } /** @@ -177,7 +268,7 @@ protected function languageManager() { * A redirect response object that may be returned by the controller. */ public function redirect($route_name, array $parameters = array(), $status = 302) { - $url = $this->container->get('url_generator')->generate($route_name, $parameters, TRUE); + $url = $this->urlGenerator()->generate($route_name, $parameters, TRUE); return new RedirectResponse($url, $status); } } diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php index bdb0004..879afde 100644 --- a/core/lib/Drupal/Core/Form/FormBase.php +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -75,7 +75,7 @@ protected function t($string, array $args = array(), array $options = array()) { */ protected function getTranslationManager() { if (!$this->translationManager) { - $this->translationManager = \Drupal::translation(); + $this->translationManager = $this->container()->get('string_translation'); } return $this->translationManager; } @@ -102,7 +102,7 @@ public function setTranslationManager(TranslationInterface $translation_manager) */ protected function getRequest() { if (!$this->request) { - $this->request = \Drupal::request(); + $this->request = $this->container()->get('request'); } return $this->request; } @@ -127,4 +127,14 @@ protected function getCurrentUser() { return $this->getRequest()->attributes->get('_account'); } + /** + * Returns the service container. + * + * @return \Symfony\Component\DependencyInjection\ContainerInterface $container + * The service container. + */ + protected function container() { + return \Drupal::getContainer(); + } + }