Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Cache / RebuildCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Cache\RebuildCommand.
6  */
7
8 namespace Drupal\Console\Command\Cache;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\HttpFoundation\RequestStack;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Utils\DrupalApi;
17 use Drupal\Console\Utils\Site;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 /**
21  * Class RebuildCommand
22  *
23  * @package Drupal\Console\Command\Cache
24  */
25 class RebuildCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30       * @var DrupalApi
31       */
32     protected $drupalApi;
33
34     /**
35      * @var Site
36      */
37     protected $site;
38
39     protected $classLoader;
40     /**
41      * @var RequestStack
42      */
43     protected $requestStack;
44
45     /**
46      * RebuildCommand constructor.
47      *
48      * @param DrupalApi    $drupalApi
49      * @param Site         $site
50      * @param $classLoader
51      * @param RequestStack $requestStack
52      */
53     public function __construct(
54         DrupalApi $drupalApi,
55         Site $site,
56         $classLoader,
57         RequestStack $requestStack
58     ) {
59         $this->drupalApi = $drupalApi;
60         $this->site = $site;
61         $this->classLoader = $classLoader;
62         $this->requestStack = $requestStack;
63         parent::__construct();
64     }
65
66     /**
67      * {@inheritdoc}
68      */
69     protected function configure()
70     {
71         $this
72             ->setName('cache:rebuild')
73             ->setDescription($this->trans('commands.cache.rebuild.description'))
74             ->addArgument(
75                 'cache',
76                 InputArgument::OPTIONAL,
77                 $this->trans('commands.cache.rebuild.options.cache')
78             );
79     }
80
81     /**
82      * {@inheritdoc}
83      */
84     protected function execute(InputInterface $input, OutputInterface $output)
85     {
86         $io = new DrupalStyle($input, $output);
87         $cache = $input->getArgument('cache')?:'all';
88         $this->site->loadLegacyFile('/core/includes/utility.inc');
89
90         if ($cache && !$this->drupalApi->isValidCache($cache)) {
91             $io->error(
92                 sprintf(
93                     $this->trans('commands.cache.rebuild.messages.invalid_cache'),
94                     $cache
95                 )
96             );
97
98             return 1;
99         }
100
101         $io->newLine();
102         $io->comment($this->trans('commands.cache.rebuild.messages.rebuild'));
103
104         if ($cache === 'all') {
105             $this->drupalApi->drupal_rebuild(
106                 $this->classLoader,
107                 $this->requestStack->getCurrentRequest()
108             );
109         } else {
110             $caches = $this->drupalApi->getCaches();
111             $caches[$cache]->deleteAll();
112         }
113
114         $io->success($this->trans('commands.cache.rebuild.messages.completed'));
115
116         return 0;
117     }
118
119     /**
120      * {@inheritdoc}
121      */
122     protected function interact(InputInterface $input, OutputInterface $output)
123     {
124         $io = new DrupalStyle($input, $output);
125
126         $cache = $input->getArgument('cache');
127         if (!$cache) {
128             $cacheKeys = array_keys($this->drupalApi->getCaches());
129
130             $cache = $io->choiceNoList(
131                 $this->trans('commands.cache.rebuild.questions.cache'),
132                 $cacheKeys,
133                 'all'
134             );
135
136             $input->setArgument('cache', $cache);
137         }
138     }
139 }