df3416af70c76218216dc7eb1480f3417c42b387
[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 Drupal\Console\Core\Command\Command;
11 use Drupal\Console\Utils\DrupalApi;
12 use Drupal\Console\Utils\Site;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\HttpFoundation\RequestStack;
17
18 /**
19  * Class RebuildCommand
20  *
21  * @package Drupal\Console\Command\Cache
22  */
23 class RebuildCommand extends Command
24 {
25     /**
26       * @var DrupalApi
27       */
28     protected $drupalApi;
29
30     /**
31      * @var Site
32      */
33     protected $site;
34
35     protected $classLoader;
36
37     /**
38      * @var RequestStack
39      */
40     protected $requestStack;
41
42     /**
43      * RebuildCommand constructor.
44      *
45      * @param DrupalApi    $drupalApi
46      * @param Site         $site
47      * @param $classLoader
48      * @param RequestStack $requestStack
49      */
50     public function __construct(
51         DrupalApi $drupalApi,
52         Site $site,
53         $classLoader,
54         RequestStack $requestStack
55     ) {
56         $this->drupalApi = $drupalApi;
57         $this->site = $site;
58         $this->classLoader = $classLoader;
59         $this->requestStack = $requestStack;
60         parent::__construct();
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     protected function configure()
67     {
68         $this
69             ->setName('cache:rebuild')
70             ->setDescription($this->trans('commands.cache.rebuild.description'))
71             ->addArgument(
72                 'cache',
73                 InputArgument::OPTIONAL,
74                 $this->trans('commands.cache.rebuild.options.cache'),
75                 'all'
76             )->setAliases(['cr']);
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     protected function execute(InputInterface $input, OutputInterface $output)
83     {
84         $cache = $input->getArgument('cache')?:'all';
85         $this->site->loadLegacyFile('/core/includes/utility.inc');
86
87         if ($cache && !$this->drupalApi->isValidCache($cache)) {
88             $this->getIo()->error(
89                 sprintf(
90                     $this->trans('commands.cache.rebuild.messages.invalid-cache'),
91                     $cache
92                 )
93             );
94
95             return 1;
96         }
97
98         $this->getIo()->newLine();
99         $this->getIo()->comment($this->trans('commands.cache.rebuild.messages.rebuild'));
100
101         if ($cache === 'all') {
102             $this->drupalApi->drupal_rebuild(
103                 $this->classLoader,
104                 $this->requestStack->getCurrentRequest()
105             );
106         } else {
107             $caches = $this->drupalApi->getCaches();
108             $caches[$cache]->deleteAll();
109         }
110
111         $this->getIo()->success($this->trans('commands.cache.rebuild.messages.completed'));
112
113         return 0;
114     }
115
116 }