annotate core/lib/Drupal/Core/Controller/ControllerBase.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
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@0 96 * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
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@0 132 if (!$this->entityManager) {
Chris@0 133 $this->entityManager = $this->container()->get('entity.manager');
Chris@0 134 }
Chris@0 135 return $this->entityManager;
Chris@0 136 }
Chris@0 137
Chris@0 138 /**
Chris@0 139 * Retrieves the entity type manager.
Chris@0 140 *
Chris@0 141 * @return \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 142 * The entity type manager.
Chris@0 143 */
Chris@0 144 protected function entityTypeManager() {
Chris@0 145 if (!isset($this->entityTypeManager)) {
Chris@0 146 $this->entityTypeManager = $this->container()->get('entity_type.manager');
Chris@0 147 }
Chris@0 148 return $this->entityTypeManager;
Chris@0 149 }
Chris@0 150
Chris@0 151 /**
Chris@0 152 * Retrieves the entity form builder.
Chris@0 153 *
Chris@0 154 * @return \Drupal\Core\Entity\EntityFormBuilderInterface
Chris@0 155 * The entity form builder.
Chris@0 156 */
Chris@0 157 protected function entityFormBuilder() {
Chris@0 158 if (!$this->entityFormBuilder) {
Chris@0 159 $this->entityFormBuilder = $this->container()->get('entity.form_builder');
Chris@0 160 }
Chris@0 161 return $this->entityFormBuilder;
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Returns the requested cache bin.
Chris@0 166 *
Chris@0 167 * @param string $bin
Chris@0 168 * (optional) The cache bin for which the cache object should be returned,
Chris@0 169 * defaults to 'default'.
Chris@0 170 *
Chris@0 171 * @return \Drupal\Core\Cache\CacheBackendInterface
Chris@0 172 * The cache object associated with the specified bin.
Chris@0 173 */
Chris@0 174 protected function cache($bin = 'default') {
Chris@0 175 return $this->container()->get('cache.' . $bin);
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Retrieves a configuration object.
Chris@0 180 *
Chris@0 181 * This is the main entry point to the configuration API. Calling
Chris@0 182 * @code $this->config('book.admin') @endcode will return a configuration
Chris@0 183 * object in which the book module can store its administrative settings.
Chris@0 184 *
Chris@0 185 * @param string $name
Chris@0 186 * The name of the configuration object to retrieve. The name corresponds to
Chris@0 187 * a configuration file. For @code \Drupal::config('book.admin') @endcode,
Chris@0 188 * the config object returned will contain the contents of book.admin
Chris@0 189 * configuration file.
Chris@0 190 *
Chris@0 191 * @return \Drupal\Core\Config\Config
Chris@0 192 * A configuration object.
Chris@0 193 */
Chris@0 194 protected function config($name) {
Chris@0 195 if (!$this->configFactory) {
Chris@0 196 $this->configFactory = $this->container()->get('config.factory');
Chris@0 197 }
Chris@0 198 return $this->configFactory->get($name);
Chris@0 199 }
Chris@0 200
Chris@0 201 /**
Chris@0 202 * Returns a key/value storage collection.
Chris@0 203 *
Chris@0 204 * @param string $collection
Chris@0 205 * Name of the key/value collection to return.
Chris@0 206 *
Chris@0 207 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
Chris@0 208 */
Chris@0 209 protected function keyValue($collection) {
Chris@0 210 if (!$this->keyValue) {
Chris@0 211 $this->keyValue = $this->container()->get('keyvalue')->get($collection);
Chris@0 212 }
Chris@0 213 return $this->keyValue;
Chris@0 214 }
Chris@0 215
Chris@0 216 /**
Chris@0 217 * Returns the state storage service.
Chris@0 218 *
Chris@0 219 * Use this to store machine-generated data, local to a specific environment
Chris@0 220 * that does not need deploying and does not need human editing; for example,
Chris@0 221 * the last time cron was run. Data which needs to be edited by humans and
Chris@0 222 * needs to be the same across development, production, etc. environments
Chris@0 223 * (for example, the system maintenance message) should use config() instead.
Chris@0 224 *
Chris@0 225 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
Chris@0 226 */
Chris@0 227 protected function state() {
Chris@0 228 if (!$this->stateService) {
Chris@0 229 $this->stateService = $this->container()->get('state');
Chris@0 230 }
Chris@0 231 return $this->stateService;
Chris@0 232 }
Chris@0 233
Chris@0 234 /**
Chris@0 235 * Returns the module handler.
Chris@0 236 *
Chris@0 237 * @return \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 238 */
Chris@0 239 protected function moduleHandler() {
Chris@0 240 if (!$this->moduleHandler) {
Chris@0 241 $this->moduleHandler = $this->container()->get('module_handler');
Chris@0 242 }
Chris@0 243 return $this->moduleHandler;
Chris@0 244 }
Chris@0 245
Chris@0 246 /**
Chris@0 247 * Returns the form builder service.
Chris@0 248 *
Chris@0 249 * @return \Drupal\Core\Form\FormBuilderInterface
Chris@0 250 */
Chris@0 251 protected function formBuilder() {
Chris@0 252 if (!$this->formBuilder) {
Chris@0 253 $this->formBuilder = $this->container()->get('form_builder');
Chris@0 254 }
Chris@0 255 return $this->formBuilder;
Chris@0 256 }
Chris@0 257
Chris@0 258 /**
Chris@0 259 * Returns the current user.
Chris@0 260 *
Chris@0 261 * @return \Drupal\Core\Session\AccountInterface
Chris@0 262 * The current user.
Chris@0 263 */
Chris@0 264 protected function currentUser() {
Chris@0 265 if (!$this->currentUser) {
Chris@0 266 $this->currentUser = $this->container()->get('current_user');
Chris@0 267 }
Chris@0 268 return $this->currentUser;
Chris@0 269 }
Chris@0 270
Chris@0 271 /**
Chris@0 272 * Returns the language manager service.
Chris@0 273 *
Chris@0 274 * @return \Drupal\Core\Language\LanguageManagerInterface
Chris@0 275 * The language manager.
Chris@0 276 */
Chris@0 277 protected function languageManager() {
Chris@0 278 if (!$this->languageManager) {
Chris@0 279 $this->languageManager = $this->container()->get('language_manager');
Chris@0 280 }
Chris@0 281 return $this->languageManager;
Chris@0 282 }
Chris@0 283
Chris@0 284 /**
Chris@0 285 * Returns the service container.
Chris@0 286 *
Chris@0 287 * This method is marked private to prevent sub-classes from retrieving
Chris@0 288 * services from the container through it. Instead,
Chris@0 289 * \Drupal\Core\DependencyInjection\ContainerInjectionInterface should be used
Chris@0 290 * for injecting services.
Chris@0 291 *
Chris@0 292 * @return \Symfony\Component\DependencyInjection\ContainerInterface
Chris@0 293 * The service container.
Chris@0 294 */
Chris@0 295 private function container() {
Chris@0 296 return \Drupal::getContainer();
Chris@0 297 }
Chris@0 298
Chris@0 299 }