ControllerBase.php 8.1 KB

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