comparison core/lib/Drupal/Core/Controller/ControllerBase.php @ 0:4c8ae668cc8c

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