ControllerBase.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 Drupal\Core\Url;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Drupal\Core\Messenger\MessengerTrait;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  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
  23. * better be 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 routing
  33. */
  34. abstract class ControllerBase implements ContainerInjectionInterface {
  35. use LinkGeneratorTrait;
  36. use LoggerChannelTrait;
  37. use MessengerTrait;
  38. use RedirectDestinationTrait;
  39. use StringTranslationTrait;
  40. use UrlGeneratorTrait;
  41. /**
  42. * The entity manager.
  43. *
  44. * @var \Drupal\Core\Entity\EntityManagerInterface
  45. */
  46. protected $entityManager;
  47. /**
  48. * The entity type manager.
  49. *
  50. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  51. */
  52. protected $entityTypeManager;
  53. /**
  54. * The entity form builder.
  55. *
  56. * @var \Drupal\Core\Entity\EntityFormBuilderInterface
  57. */
  58. protected $entityFormBuilder;
  59. /**
  60. * The language manager.
  61. *
  62. * @var \Drupal\Core\Language\LanguageManagerInterface
  63. */
  64. protected $languageManager;
  65. /**
  66. * The configuration factory.
  67. *
  68. * @var \Drupal\Core\Config\ConfigFactoryInterface
  69. */
  70. protected $configFactory;
  71. /**
  72. * The key-value storage.
  73. *
  74. * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  75. */
  76. protected $keyValue;
  77. /**
  78. * The current user service.
  79. *
  80. * @var \Drupal\Core\Session\AccountInterface
  81. */
  82. protected $currentUser;
  83. /**
  84. * The state service.
  85. *
  86. * @var \Drupal\Core\State\StateInterface
  87. */
  88. protected $stateService;
  89. /**
  90. * The module handler.
  91. *
  92. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  93. */
  94. protected $moduleHandler;
  95. /**
  96. * The form builder.
  97. *
  98. * @var \Drupal\Core\Form\FormBuilderInterface
  99. */
  100. protected $formBuilder;
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public static function create(ContainerInterface $container) {
  105. return new static();
  106. }
  107. /**
  108. * Retrieves the entity manager service.
  109. *
  110. * @return \Drupal\Core\Entity\EntityManagerInterface
  111. * The entity manager service.
  112. *
  113. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  114. * Most of the time static::entityTypeManager() is supposed to be used
  115. * instead.
  116. */
  117. protected function entityManager() {
  118. @trigger_error('ControllerBase::getEntityManager() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use ::getEntityTypeManager() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  119. if (!$this->entityManager) {
  120. $this->entityManager = $this->container()->get('entity.manager');
  121. }
  122. return $this->entityManager;
  123. }
  124. /**
  125. * Retrieves the entity type manager.
  126. *
  127. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  128. * The entity type manager.
  129. */
  130. protected function entityTypeManager() {
  131. if (!isset($this->entityTypeManager)) {
  132. $this->entityTypeManager = $this->container()->get('entity_type.manager');
  133. }
  134. return $this->entityTypeManager;
  135. }
  136. /**
  137. * Retrieves the entity form builder.
  138. *
  139. * @return \Drupal\Core\Entity\EntityFormBuilderInterface
  140. * The entity form builder.
  141. */
  142. protected function entityFormBuilder() {
  143. if (!$this->entityFormBuilder) {
  144. $this->entityFormBuilder = $this->container()->get('entity.form_builder');
  145. }
  146. return $this->entityFormBuilder;
  147. }
  148. /**
  149. * Returns the requested cache bin.
  150. *
  151. * @param string $bin
  152. * (optional) The cache bin for which the cache object should be returned,
  153. * defaults to 'default'.
  154. *
  155. * @return \Drupal\Core\Cache\CacheBackendInterface
  156. * The cache object associated with the specified bin.
  157. */
  158. protected function cache($bin = 'default') {
  159. return $this->container()->get('cache.' . $bin);
  160. }
  161. /**
  162. * Retrieves a configuration object.
  163. *
  164. * This is the main entry point to the configuration API. Calling
  165. * @code $this->config('book.admin') @endcode will return a configuration
  166. * object in which the book module can store its administrative settings.
  167. *
  168. * @param string $name
  169. * The name of the configuration object to retrieve. The name corresponds to
  170. * a configuration file. For @code \Drupal::config('book.admin') @endcode,
  171. * the config object returned will contain the contents of book.admin
  172. * configuration file.
  173. *
  174. * @return \Drupal\Core\Config\Config
  175. * A configuration object.
  176. */
  177. protected function config($name) {
  178. if (!$this->configFactory) {
  179. $this->configFactory = $this->container()->get('config.factory');
  180. }
  181. return $this->configFactory->get($name);
  182. }
  183. /**
  184. * Returns a key/value storage collection.
  185. *
  186. * @param string $collection
  187. * Name of the key/value collection to return.
  188. *
  189. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  190. */
  191. protected function keyValue($collection) {
  192. if (!$this->keyValue) {
  193. $this->keyValue = $this->container()->get('keyvalue')->get($collection);
  194. }
  195. return $this->keyValue;
  196. }
  197. /**
  198. * Returns the state storage service.
  199. *
  200. * Use this to store machine-generated data, local to a specific environment
  201. * that does not need deploying and does not need human editing; for example,
  202. * the last time cron was run. Data which needs to be edited by humans and
  203. * needs to be the same across development, production, etc. environments
  204. * (for example, the system maintenance message) should use config() instead.
  205. *
  206. * @return \Drupal\Core\State\StateInterface
  207. */
  208. protected function state() {
  209. if (!$this->stateService) {
  210. $this->stateService = $this->container()->get('state');
  211. }
  212. return $this->stateService;
  213. }
  214. /**
  215. * Returns the module handler.
  216. *
  217. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  218. */
  219. protected function moduleHandler() {
  220. if (!$this->moduleHandler) {
  221. $this->moduleHandler = $this->container()->get('module_handler');
  222. }
  223. return $this->moduleHandler;
  224. }
  225. /**
  226. * Returns the form builder service.
  227. *
  228. * @return \Drupal\Core\Form\FormBuilderInterface
  229. */
  230. protected function formBuilder() {
  231. if (!$this->formBuilder) {
  232. $this->formBuilder = $this->container()->get('form_builder');
  233. }
  234. return $this->formBuilder;
  235. }
  236. /**
  237. * Returns the current user.
  238. *
  239. * @return \Drupal\Core\Session\AccountInterface
  240. * The current user.
  241. */
  242. protected function currentUser() {
  243. if (!$this->currentUser) {
  244. $this->currentUser = $this->container()->get('current_user');
  245. }
  246. return $this->currentUser;
  247. }
  248. /**
  249. * Returns the language manager service.
  250. *
  251. * @return \Drupal\Core\Language\LanguageManagerInterface
  252. * The language manager.
  253. */
  254. protected function languageManager() {
  255. if (!$this->languageManager) {
  256. $this->languageManager = $this->container()->get('language_manager');
  257. }
  258. return $this->languageManager;
  259. }
  260. /**
  261. * Returns a redirect response object for the specified route.
  262. *
  263. * @param string $route_name
  264. * The name of the route to which to redirect.
  265. * @param array $route_parameters
  266. * (optional) Parameters for the route.
  267. * @param array $options
  268. * (optional) An associative array of additional options.
  269. * @param int $status
  270. * (optional) The HTTP redirect status code for the redirect. The default is
  271. * 302 Found.
  272. *
  273. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  274. * A redirect response object that may be returned by the controller.
  275. */
  276. protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
  277. $options['absolute'] = TRUE;
  278. return new RedirectResponse(Url::fromRoute($route_name, $route_parameters, $options)->toString(), $status);
  279. }
  280. /**
  281. * Returns the service container.
  282. *
  283. * This method is marked private to prevent sub-classes from retrieving
  284. * services from the container through it. Instead,
  285. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
  286. * for injecting services.
  287. *
  288. * @return \Symfony\Component\DependencyInjection\ContainerInterface
  289. * The service container.
  290. */
  291. private function container() {
  292. return \Drupal::getContainer();
  293. }
  294. }