1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Command; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Stitcher; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\Console\Question\Question; |
12
|
|
|
use Symfony\Component\Finder\Finder; |
13
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
14
|
|
|
|
15
|
|
|
class CleanCommand extends Command |
16
|
|
|
{ |
17
|
|
|
const FORCE = 'force'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Filesystem |
21
|
|
|
*/ |
22
|
|
|
private $fs; |
23
|
|
|
|
24
|
|
|
protected function configure() { |
25
|
|
|
$this->setName('site:clean') |
26
|
|
|
->setDescription('Do a cleanup') |
27
|
|
|
->setHelp("This command cleans all generated files.") |
28
|
|
|
->addOption(self::FORCE, InputArgument::OPTIONAL); |
29
|
|
|
|
30
|
|
|
$this->fs = new Filesystem(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
40
|
|
|
Stitcher::create(); |
41
|
|
|
|
42
|
|
|
$force = $input->getOption(self::FORCE); |
43
|
|
|
$publicDir = Stitcher::getParameter('directories.public'); |
44
|
|
|
$cacheDir = Stitcher::getParameter('directories.cache'); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->checkForcedClean($force, $publicDir, $cacheDir, $input, $output); |
48
|
|
|
|
49
|
|
|
$log = []; |
50
|
|
|
$log[] = $this->cleanPublicDir($publicDir); |
51
|
|
|
$log[] = $this->cleanCacheDir($cacheDir); |
52
|
|
|
|
53
|
|
|
$output->writeln("Successfully cleaned up\n"); |
54
|
|
|
|
55
|
|
|
foreach ($log as $line) { |
56
|
|
|
$output->writeln("- {$line}"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$output->writeln("\nRun <fg=green>site:generate</> to generate these files again."); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param bool $force |
64
|
|
|
* @param string $publicDir |
65
|
|
|
* @param string $cacheDir |
66
|
|
|
* @param InputInterface $input |
67
|
|
|
* @param OutputInterface $output |
68
|
|
|
*/ |
69
|
|
|
private function checkForcedClean($force = false, string $publicDir, string $cacheDir, InputInterface $input, OutputInterface $output) : void { |
70
|
|
|
if ($force) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$questionHelper = $this->getHelper('question'); |
75
|
|
|
$question = new Question("Are you sure you want to clean all generated files ({$publicDir} and {$cacheDir}) [y/N] ", false); |
76
|
|
|
|
77
|
|
|
if (!$questionHelper->ask($input, $output, $question)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $publicDir |
84
|
|
|
* |
85
|
|
|
* @return null|string |
86
|
|
|
*/ |
87
|
|
|
private function cleanPublicDir(string $publicDir) : string { |
88
|
|
|
if (!$this->fs->exists($publicDir)) { |
89
|
|
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$publicDirectories = Finder::create()->directories()->in($publicDir); |
93
|
|
|
$directoryPaths = []; |
94
|
|
|
|
95
|
|
|
/** @var SplFileInfo $directory */ |
96
|
|
|
foreach ($publicDirectories as $directory) { |
97
|
|
|
$directoryPaths[] = $directory->getPathname(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->fs->remove($directoryPaths); |
101
|
|
|
|
102
|
|
|
$publicFiles = Finder::create()->files()->in($publicDir)->notName('.htaccess')->ignoreDotFiles(true); |
103
|
|
|
|
104
|
|
|
/** @var SplFileInfo $file */ |
105
|
|
|
foreach ($publicFiles as $file) { |
106
|
|
|
$this->fs->remove($file->getPathname()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return "Cleaned the public directory: {$publicDir}"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $cacheDir |
114
|
|
|
* |
115
|
|
|
* @return null|string |
116
|
|
|
*/ |
117
|
|
|
private function cleanCacheDir(string $cacheDir) : ?string { |
118
|
|
|
if (!$this->fs->exists($cacheDir)) { |
119
|
|
|
return ''; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->fs->remove($cacheDir); |
123
|
|
|
|
124
|
|
|
return "Removed the cache directory: {$cacheDir}"; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|