annotate core/lib/Drupal/Core/Controller/ControllerBase.php @ 13:5fb285c0d0e3

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