Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / admin_toolbar / admin_toolbar_tools / src / Controller / ToolbarController.php
1 <?php
2
3 namespace Drupal\admin_toolbar_tools\Controller;
4
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\CronInterface;
9 use Drupal\Core\Menu\ContextualLinkManagerInterface;
10 use Drupal\Core\Menu\LocalActionManagerInterface;
11 use Drupal\Core\Menu\LocalTaskManagerInterface;
12 use Drupal\Core\Menu\MenuLinkManagerInterface;
13 use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\HttpFoundation\RedirectResponse;
16 use Symfony\Component\HttpFoundation\RequestStack;
17 use Drupal\Core\PhpStorage\PhpStorageFactory;
18
19 /**
20  * Class ToolbarController.
21  *
22  * @package Drupal\admin_toolbar_tools\Controller
23  */
24 class ToolbarController extends ControllerBase {
25
26   /**
27    * A cron instance.
28    *
29    * @var \Drupal\Core\CronInterface
30    */
31   protected $cron;
32
33   /**
34    * A menu link manager instance.
35    *
36    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
37    */
38   protected $menuLinkManager;
39
40   /**
41    * A context link manager instance.
42    *
43    * @var \Drupal\Core\Menu\ContextualLinkManagerInterface
44    */
45   protected $contextualLinkManager;
46
47   /**
48    * A local task manager instance.
49    *
50    * @var \Drupal\Core\Menu\LocalTaskManagerInterface
51    */
52   protected $localTaskLinkManager;
53
54   /**
55    * A local action manager instance.
56    *
57    * @var \Drupal\Core\Menu\LocalActionManagerInterface
58    */
59   protected $localActionLinkManager;
60
61   /**
62    * A cache backend interface instance.
63    *
64    * @var \Drupal\Core\Cache\CacheBackendInterface
65    */
66   protected $cacheRender;
67
68   /**
69    * A date time instance.
70    *
71    * @var \Drupal\Component\Datetime\TimeInterface
72    */
73   protected $time;
74
75   /**
76    * A request stack symfony instance.
77    *
78    * @var \Symfony\Component\HttpFoundation\RequestStack
79    */
80   protected $requestStack;
81
82   /**
83    * A plugin cache clear instance.
84    *
85    * @var \Drupal\Core\Plugin\CachedDiscoveryClearerInterface
86    */
87   protected $pluginCacheClearer;
88
89   /**
90    * Constructs a ToolbarController object.
91    *
92    * @param \Drupal\Core\CronInterface $cron
93    *   A cron instance.
94    * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
95    *   A menu link manager instance.
96    * @param \Drupal\Core\Menu\ContextualLinkManagerInterface $contextualLinkManager
97    *   A context link manager instance.
98    * @param \Drupal\Core\Menu\LocalTaskManagerInterface $localTaskLinkManager
99    *   A local task manager instance.
100    * @param \Drupal\Core\Menu\LocalActionManagerInterface $localActionLinkManager
101    *   A local action manager instance.
102    * @param \Drupal\Core\Cache\CacheBackendInterface $cacheRender
103    *   A cache backend interface instance.
104    * @param \Drupal\Component\Datetime\TimeInterface $time
105    *   A date time instance.
106    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
107    *   A request stack symfony instance.
108    * @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer
109    *   A plugin cache clear instance.
110    */
111   public function __construct(CronInterface $cron,
112                               MenuLinkManagerInterface $menuLinkManager,
113                               ContextualLinkManagerInterface $contextualLinkManager,
114                               LocalTaskManagerInterface $localTaskLinkManager,
115                               LocalActionManagerInterface $localActionLinkManager,
116                               CacheBackendInterface $cacheRender,
117                               TimeInterface $time,
118                               RequestStack $request_stack,
119                               CachedDiscoveryClearerInterface $plugin_cache_clearer) {
120     $this->cron = $cron;
121     $this->menuLinkManager = $menuLinkManager;
122     $this->contextualLinkManager = $contextualLinkManager;
123     $this->localTaskLinkManager = $localTaskLinkManager;
124     $this->localActionLinkManager = $localActionLinkManager;
125     $this->cacheRender = $cacheRender;
126     $this->time = $time;
127     $this->requestStack = $request_stack;
128     $this->pluginCacheClearer = $plugin_cache_clearer;
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public static function create(ContainerInterface $container) {
135     return new static(
136       $container->get('cron'),
137       $container->get('plugin.manager.menu.link'),
138       $container->get('plugin.manager.menu.contextual_link'),
139       $container->get('plugin.manager.menu.local_task'),
140       $container->get('plugin.manager.menu.local_action'),
141       $container->get('cache.render'),
142       $container->get('datetime.time'),
143       $container->get('request_stack'),
144       $container->get('plugin.cache_clearer')
145     );
146   }
147
148   /**
149    * Reload the previous page.
150    */
151   public function reloadPage() {
152     $request = $this->requestStack->getCurrentRequest();
153     if ($request->server->get('HTTP_REFERER')) {
154       return $request->server->get('HTTP_REFERER');
155     }
156     else {
157       return '/';
158     }
159   }
160
161   /**
162    * Flushes all caches.
163    */
164   public function flushAll() {
165     drupal_flush_all_caches();
166     drupal_set_message($this->t('All caches cleared.'));
167     return new RedirectResponse($this->reloadPage());
168   }
169
170   /**
171    * Flushes css and javascript caches.
172    */
173   public function flushJsCss() {
174     $this->state()
175       ->set('system.css_js_query_string', base_convert($this->time->getCurrentTime(), 10, 36));
176     drupal_set_message($this->t('CSS and JavaScript cache cleared.'));
177     return new RedirectResponse($this->reloadPage());
178   }
179
180   /**
181    * Flushes plugins caches.
182    */
183   public function flushPlugins() {
184     $this->pluginCacheClearer->clearCachedDefinitions();
185     drupal_set_message($this->t('Plugins cache cleared.'));
186     return new RedirectResponse($this->reloadPage());
187   }
188
189   /**
190    * Resets all static caches.
191    */
192   public function flushStatic() {
193     drupal_static_reset();
194     drupal_set_message($this->t('Static cache cleared.'));
195     return new RedirectResponse($this->reloadPage());
196   }
197
198   /**
199    * Clears all cached menu data.
200    */
201   public function flushMenu() {
202     menu_cache_clear_all();
203     $this->menuLinkManager->rebuild();
204     $this->contextualLinkManager->clearCachedDefinitions();
205     $this->localTaskLinkManager->clearCachedDefinitions();
206     $this->localActionLinkManager->clearCachedDefinitions();
207     drupal_set_message($this->t('Routing and links cache cleared.'));
208     return new RedirectResponse($this->reloadPage());
209   }
210
211   /**
212    * Clears all cached views data.
213    */
214   public function flushViews() {
215     views_invalidate_cache();
216     drupal_set_message($this->t('Views cache cleared.'));
217     return new RedirectResponse($this->reloadPage());
218   }
219
220   /**
221    * Clears the twig cache.
222    */
223   public function flushTwig() {
224     // @todo Update once Drupal 8.6 will be released.
225     // @see https://www.drupal.org/node/2908461
226     PhpStorageFactory::get('twig')->deleteAll();
227     drupal_set_message($this->t('Twig cache cleared.'));
228     return new RedirectResponse($this->reloadPage());
229   }
230
231   /**
232    * Run the cron.
233    */
234   public function runCron() {
235     $this->cron->run();
236     drupal_set_message($this->t('Cron ran successfully.'));
237     return new RedirectResponse($this->reloadPage());
238   }
239
240   /**
241    * Clear the rendered cache.
242    */
243   public function cacheRender() {
244     $this->cacheRender->invalidateAll();
245     drupal_set_message($this->t('Render cache cleared.'));
246     return new RedirectResponse($this->reloadPage());
247   }
248
249 }