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