1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Command; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\App; |
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
|
|
|
private $fs; |
20
|
|
|
|
21
|
5 |
|
protected function configure() { |
22
|
5 |
|
$this->setName('site:clean') |
23
|
5 |
|
->setDescription('Do a cleanup') |
24
|
5 |
|
->setHelp("This command cleans all generated files.") |
25
|
5 |
|
->addOption(self::FORCE, InputArgument::OPTIONAL); |
26
|
|
|
|
27
|
5 |
|
$this->fs = new Filesystem(); |
28
|
5 |
|
} |
29
|
|
|
|
30
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) { |
31
|
1 |
|
$force = $input->getOption(self::FORCE); |
32
|
1 |
|
$publicDir = App::getParameter('directories.public'); |
33
|
1 |
|
$cacheDir = App::getParameter('directories.cache'); |
34
|
|
|
|
35
|
1 |
|
$this->checkForcedClean($force, $publicDir, $cacheDir, $input, $output); |
36
|
|
|
|
37
|
1 |
|
$log = []; |
38
|
1 |
|
$log[] = $this->cleanPublicDir($publicDir); |
39
|
1 |
|
$log[] = $this->cleanCacheDir($cacheDir); |
40
|
|
|
|
41
|
1 |
|
$output->writeln("Successfully cleaned up\n"); |
42
|
|
|
|
43
|
1 |
|
foreach ($log as $line) { |
44
|
1 |
|
$output->writeln("- {$line}"); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$output->writeln("\nRun <fg=green>site:generate</> to generate these files again."); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
1 |
|
private function checkForcedClean($force = false, string $publicDir, string $cacheDir, InputInterface $input, OutputInterface $output) { |
51
|
1 |
|
if ($force) { |
52
|
1 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$questionHelper = $this->getHelper('question'); |
56
|
|
|
$question = new Question("Are you sure you want to clean all generated files ({$publicDir} and {$cacheDir}) [y/N] ", false); |
57
|
|
|
|
58
|
|
|
if (!$questionHelper->ask($input, $output, $question)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
private function cleanPublicDir(string $publicDir) : string { |
64
|
1 |
|
if (!$this->fs->exists($publicDir)) { |
65
|
|
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$publicDirectories = Finder::create()->directories()->in($publicDir); |
69
|
1 |
|
$directoryPaths = []; |
70
|
|
|
|
71
|
|
|
/** @var SplFileInfo $directory */ |
72
|
1 |
|
foreach ($publicDirectories as $directory) { |
73
|
1 |
|
$directoryPaths[] = $directory->getPathname(); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$this->fs->remove($directoryPaths); |
77
|
|
|
|
78
|
1 |
|
$publicFiles = Finder::create()->files()->in($publicDir)->notName('.htaccess')->ignoreDotFiles(true); |
79
|
|
|
|
80
|
|
|
/** @var SplFileInfo $file */ |
81
|
1 |
|
foreach ($publicFiles as $file) { |
82
|
1 |
|
$this->fs->remove($file->getPathname()); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return "Cleaned the public directory: {$publicDir}"; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
private function cleanCacheDir(string $cacheDir) { |
89
|
1 |
|
if (!$this->fs->exists($cacheDir)) { |
90
|
|
|
return ''; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$this->fs->remove($cacheDir); |
94
|
|
|
|
95
|
1 |
|
return "Removed the cache directory: {$cacheDir}"; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|