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

Classes

Namesort descending Description
ControllerBase Utility base class for thin controllers.