annotate core/lib/Drupal/Core/Controller/ControllerBase.php @ 19:fa3358dc1485 tip

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