Version 1
[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\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\CronInterface;
8 use Drupal\Core\Routing\TrustedRedirectResponse;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\RedirectResponse;
11 use Drupal\Core\Menu\ContextualLinkManager;
12 use Drupal\Core\Menu\LocalActionManager;
13 use Drupal\Core\Menu\LocalTaskManager;
14 use Drupal\Core\Menu\MenuLinkManager;
15
16 /**
17  * Class ToolbarController
18  *
19  * @package Drupal\admin_toolbar_tools\Controller
20  */
21 class ToolbarController extends ControllerBase {
22
23   /**
24    * The cron service.
25    *
26    * @var $cron \Drupal\Core\CronInterface
27    */
28   protected $cron;
29   protected $menuLinkManager;
30   protected $contextualLinkManager;
31   protected $localTaskLinkManager;
32   protected $localActionLinkManager;
33   protected $cacheRender;
34
35   /**
36    * Constructs a CronController object.
37    *
38    * @param \Drupal\Core\CronInterface $cron
39    *   The cron service.
40    */
41   public function __construct(CronInterface $cron,
42                               MenuLinkManager $menuLinkManager,
43                               ContextualLinkManager $contextualLinkManager,
44                               LocalTaskManager $localTaskLinkManager,
45                               LocalActionManager $localActionLinkManager,
46                               CacheBackendInterface $cacheRender) {
47     $this->cron = $cron;
48     $this->menuLinkManager = $menuLinkManager;
49     $this->contextualLinkManager = $contextualLinkManager;
50     $this->localTaskLinkManager = $localTaskLinkManager;
51     $this->localActionLinkManager = $localActionLinkManager;
52     $this->cacheRender = $cacheRender;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container) {
59     return new static(
60       $container->get('cron'),
61       $container->get('plugin.manager.menu.link'),
62       $container->get('plugin.manager.menu.contextual_link'),
63       $container->get('plugin.manager.menu.local_task'),
64       $container->get('plugin.manager.menu.local_action'),
65       $container->get('cache.render')
66     );
67   }
68
69   /**
70    * Reload the previous page.
71    */
72   public function reload_page() {
73     $request = \Drupal::request();
74     if ($request->server->get('HTTP_REFERER')) {
75       return $request->server->get('HTTP_REFERER');
76     }
77     else {
78       return '/';
79     }
80   }
81
82   /**
83    * Flushes all caches.
84    */
85   public function flushAll() {
86     drupal_flush_all_caches();
87     drupal_set_message($this->t('All caches cleared.'));
88     return new RedirectResponse($this->reload_page());
89   }
90
91   /**
92    * Flushes css and javascript caches.
93    */
94   public function flush_js_css() {
95     \Drupal::state()
96       ->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));
97     drupal_set_message($this->t('CSS and JavaScript cache cleared.'));
98     return new RedirectResponse($this->reload_page());
99   }
100
101   /**
102    * Flushes plugins caches.
103    */
104   public function flush_plugins() {
105     \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
106     drupal_set_message($this->t('Plugins cache cleared.'));
107     return new RedirectResponse($this->reload_page());
108   }
109
110   /**
111    * Resets all static caches.
112    */
113   public function flush_static() {
114     drupal_static_reset();
115     drupal_set_message($this->t('Static cache cleared.'));
116     return new RedirectResponse($this->reload_page());
117   }
118
119   /**
120    * Clears all cached menu data.
121    */
122   public function flush_menu() {
123     menu_cache_clear_all();
124     $this->menuLinkManager->rebuild();
125     $this->contextualLinkManager->clearCachedDefinitions();
126     $this->localTaskLinkManager->clearCachedDefinitions();
127     $this->localActionLinkManager->clearCachedDefinitions();
128     drupal_set_message($this->t('Routing and links cache cleared.'));
129     return new RedirectResponse($this->reload_page());
130   }
131
132   /**
133    * Links to drupal.org home page.
134    */
135   public function drupal_org() {
136     $response = new TrustedRedirectResponse("https://www.drupal.org");
137     $response->send();
138     return $response;
139   }
140
141   /**
142    * Displays the administration link Development.
143    */
144   public function development() {
145     return new RedirectResponse('/admin/structure/menu/');
146   }
147
148   /**
149    * Access to Drupal 8 changes.
150    * (list changes of the different versions of drupal core).
151    */
152   public function list_changes() {
153     $response = new TrustedRedirectResponse("https://www.drupal.org/list-changes");
154     $response->send();
155     return $response;
156   }
157
158   /**
159    * Adds a link to the Drupal 8 documentation.
160    */
161   public function documentation() {
162     $response = new TrustedRedirectResponse("https://api.drupal.org/api/drupal/8");
163     $response->send();
164     return $response;
165   }
166
167   /**
168    * Run the cron.
169    */
170   public function runCron() {
171     $this->cron->run();
172     drupal_set_message($this->t('Cron ran successfully.'));
173     return new RedirectResponse($this->reload_page());
174   }
175
176   /**
177    * Clear the rendered cache.
178    */
179   public function cacheRender() {
180     $this->cacheRender->invalidateAll();
181     drupal_set_message($this->t('Render cache cleared.'));
182     return new RedirectResponse($this->reload_page());
183   }
184
185 }