forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheRebuildCommand.php
More file actions
109 lines (92 loc) · 3.56 KB
/
Copy pathCacheRebuildCommand.php
File metadata and controls
109 lines (92 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\CacheRebuildCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CacheRebuildCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('cache:rebuild')
->setDescription($this->trans('commands.cache.rebuild.description'))
->addOption('cache', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.cache.rebuild.options.cache'),
'');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
require_once DRUPAL_ROOT . '/core/includes/utility.inc';
$validators = $this->getHelperSet()->get('validators');
// Get the --cache option and make validation
$cache = $input->getOption('cache');
$validated_cache = $validators->validateCache($cache);
if (!$validated_cache) {
throw new \InvalidArgumentException(
sprintf(
$this->trans('commands.cache.rebuild.messages.invalid_cache'),
$cache
)
);
}
// Start rebuilding cache
$output->writeln('');
$output->writeln('[+] <comment>' . $this->trans('commands.cache.rebuild.messages.rebuild') . '</comment>');
// Get data needed to rebuild cache
$kernelHelper = $this->getHelper('kernel');
$classLoader = $kernelHelper->getClassLoader();
$request = $kernelHelper->getRequest();
// Check cache to rebuild
if ($cache === 'all') {
// If cache is all, then clear all caches
\drupal_rebuild($classLoader, $request);
} else {
// Else, clear the selected cache
$caches = $validators->getCaches();
$caches[$cache]->deleteAll();
}
// Finish rebuiilding cache
$output->writeln('[+] <info>' . $this->trans('commands.cache.rebuild.messages.completed') . '</info>');
}
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
// Get the cache option
$cache = $this->getCacheOption($input, $output, $dialog);
$input->setOption('cache', $cache);
}
private function getCacheOption($input, $output, $dialog)
{
$validators = $this->getHelperSet()->get('validators');
// Get the --cache option and make user interaction with validation
$cache = $input->getOption('cache');
if (!$cache) {
$caches = $validators->getCaches();
$cache_keys = array_keys($caches);
$cache_keys[] = 'all';
$cache = $dialog->askAndValidate(
$output,
$dialog->getQuestion($this->trans('commands.cache.rebuild.questions.cache'), 'all'),
function ($cache) use ($validators) {
$validated_cache = $validators->validateCache($cache);
if (!$validated_cache) {
throw new \InvalidArgumentException(
sprintf(
$this->trans('commands.cache.rebuild.messages.invalid_cache'),
$cache
)
);
}
return $validated_cache;
},
false,
'all',
$cache_keys
);
}
return $cache;
}
}