Contains \Drupal\Core\Controller\ControllerBase.

Namespace

Drupal\Core\Controller

File

core/lib/Drupal/Core/Controller/ControllerBase.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\Controller\ControllerBase.
  5. */
  6. namespace Drupal\Core\Controller;
  7. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  8. use Drupal\Core\Routing\LinkGeneratorTrait;
  9. use Drupal\Core\Routing\RedirectDestinationTrait;
  10. use Drupal\Core\Routing\UrlGeneratorTrait;
  11. use Drupal\Core\StringTranslation\StringTranslationTrait;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. /**
  14. * Utility base class for thin controllers.
  15. *
  16. * Controllers that use this base class have access to a number of utility
  17. * methods and to the Container, which can greatly reduce boilerplate dependency
  18. * handling code. However, it also makes the class considerably more
  19. * difficult to unit test. Therefore this base class should only be used by
  20. * controller classes that contain only trivial glue code. Controllers that
  21. * contain sufficiently complex logic that it's worth testing should not use
  22. * this base class but use ContainerInjectionInterface instead, or even better be
  23. * refactored to be trivial glue code.
  24. *
  25. * The services exposed here are those that it is reasonable for a well-behaved
  26. * controller to leverage. A controller that needs other services may
  27. * need to be refactored into a thin controller and a dependent unit-testable
  28. * service.
  29. *
  30. * @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
  31. *
  32. * @ingroup menu
  33. */
  34. abstract class ControllerBase implements ContainerInjectionInterface {
  35. use LinkGeneratorTrait;
  36. use RedirectDestinationTrait;
  37. use StringTranslationTrait;
  38. use UrlGeneratorTrait;
  39. /**
  40. * The entity manager.
  41. *
  42. * @var \Drupal\Core\Entity\EntityManagerInterface
  43. */
  44. protected $entityManager;
  45. /**
  46. * The entity form builder.
  47. *
  48. * @var \Drupal\Core\Entity\EntityFormBuilderInterface
  49. */
  50. protected $entityFormBuilder;
  51. /**
  52. * The language manager.
  53. *
  54. * @var \Drupal\Core\Language\LanguageManagerInterface
  55. */
  56. protected $languageManager;
  57. /**
  58. * The configuration factory.
  59. *
  60. * @var \Drupal\Core\Config\Config
  61. */
  62. protected $configFactory;
  63. /**
  64. * The key-value storage.
  65. *
  66. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  67. */
  68. protected $keyValue;
  69. /**
  70. * The current user service.
  71. *
  72. * @var \Drupal\Core\Session\AccountInterface
  73. */
  74. protected $currentUser;
  75. /**
  76. * The state service.
  77. *
  78. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  79. */
  80. protected $stateService;
  81. /**
  82. * The module handler.
  83. *
  84. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  85. */
  86. protected $moduleHandler;
  87. /**
  88. * The form builder.
  89. *
  90. * @var \Drupal\Core\Form\FormBuilderInterface
  91. */
  92. protected $formBuilder;
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public static function create(ContainerInterface $container) {
  97. return new static();
  98. }
  99. /**
  100. * Retrieves the entity manager service.
  101. *
  102. * @return \Drupal\Core\Entity\EntityManagerInterface
  103. * The entity manager service.
  104. */
  105. protected function entityManager() {
  106. if (!$this->entityManager) {
  107. $this->entityManager = $this->container()->get('entity.manager');
  108. }
  109. return $this->entityManager;
  110. }
  111. /**
  112. * Retrieves the entity form builder.
  113. *
  114. * @return \Drupal\Core\Entity\EntityFormBuilderInterface
  115. * The entity form builder.
  116. */
  117. protected function entityFormBuilder() {
  118. if (!$this->entityFormBuilder) {
  119. $this->entityFormBuilder = $this->container()->get('entity.form_builder');
  120. }
  121. return $this->entityFormBuilder;
  122. }
  123. /**
  124. * Returns the requested cache bin.
  125. *
  126. * @param string $bin
  127. * (optional) The cache bin for which the cache object should be returned,
  128. * defaults to 'default'.
  129. *
  130. * @return \Drupal\Core\Cache\CacheBackendInterface
  131. * The cache object associated with the specified bin.
  132. */
  133. protected function cache($bin = 'default') {
  134. return $this->container()->get('cache.' . $bin);
  135. }
  136. /**
  137. * Retrieves a configuration object.
  138. *
  139. * This is the main entry point to the configuration API. Calling
  140. * @code $this->config('book.admin') @endcode will return a configuration
  141. * object in which the book module can store its administrative settings.
  142. *
  143. * @param string $name
  144. * The name of the configuration object to retrieve. The name corresponds to
  145. * a configuration file. For @code \Drupal::config('book.admin') @endcode,
  146. * the config object returned will contain the contents of book.admin
  147. * configuration file.
  148. *
  149. * @return \Drupal\Core\Config\Config
  150. * A configuration object.
  151. */
  152. protected function config($name) {
  153. if (!$this->configFactory) {
  154. $this->configFactory = $this->container()->get('config.factory');
  155. }
  156. return $this->configFactory->get($name);
  157. }
  158. /**
  159. * Returns a key/value storage collection.
  160. *
  161. * @param string $collection
  162. * Name of the key/value collection to return.
  163. *
  164. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  165. */
  166. protected function keyValue($collection) {
  167. if (!$this->keyValue) {
  168. $this->keyValue = $this->container()->get('keyvalue')->get($collection);
  169. }
  170. return $this->keyValue;
  171. }
  172. /**
  173. * Returns the state storage service.
  174. *
  175. * Use this to store machine-generated data, local to a specific environment
  176. * that does not need deploying and does not need human editing; for example,
  177. * the last time cron was run. Data which needs to be edited by humans and
  178. * needs to be the same across development, production, etc. environments
  179. * (for example, the system maintenance message) should use config() instead.
  180. *
  181. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  182. */
  183. protected function state() {
  184. if (!$this->stateService) {
  185. $this->stateService = $this->container()->get('state');
  186. }
  187. return $this->stateService;
  188. }
  189. /**
  190. * Returns the module handler.
  191. *
  192. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  193. */
  194. protected function moduleHandler() {
  195. if (!$this->moduleHandler) {
  196. $this->moduleHandler = $this->container()->get('module_handler');
  197. }
  198. return $this->moduleHandler;
  199. }
  200. /**
  201. * Returns the form builder service.
  202. *
  203. * @return \Drupal\Core\Form\FormBuilderInterface
  204. */
  205. protected function formBuilder() {
  206. if (!$this->formBuilder) {
  207. $this->formBuilder = $this->container()->get('form_builder');
  208. }
  209. return $this->formBuilder;
  210. }
  211. /**
  212. * Returns the current user.
  213. *
  214. * @return \Drupal\Core\Session\AccountInterface
  215. * The current user.
  216. */
  217. protected function currentUser() {
  218. if (!$this->currentUser) {
  219. $this->currentUser = $this->container()->get('current_user');
  220. }
  221. return $this->currentUser;
  222. }
  223. /**
  224. * Returns the language manager service.
  225. *
  226. * @return \Drupal\Core\Language\LanguageManagerInterface
  227. * The language manager.
  228. */
  229. protected function languageManager() {
  230. if (!$this->languageManager) {
  231. $this->languageManager = $this->container()->get('language_manager');
  232. }
  233. return $this->languageManager;
  234. }
  235. /**
  236. * Returns the service container.
  237. *
  238. * This method is marked private to prevent sub-classes from retrieving
  239. * services from the container through it. Instead,
  240. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
  241. * for injecting services.
  242. *
  243. * @return \Symfony\Component\DependencyInjection\ContainerInterface $container
  244. * The service container.
  245. */
  246. private function container() {
  247. return \Drupal::getContainer();
  248. }
  249. }

Classes

Namesort descending Description
ControllerBase Utility base class for thin controllers.