/
CacheClearCommand.php
62 lines (51 loc) · 2.43 KB
/
CacheClearCommand.php
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
<?php
namespace Console\Core;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* This command will clear all the cache
*/
class CacheClearCommand extends Command
{
protected function configure(): void
{
$this->setName('forkcms:cache:clear')
->setDescription('Clear the cache');
}
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$this->removeFilesInFolder('/src/Frontend/Cache/CompiledTemplates', $io, 'frontend compiled templates');
$this->removeFilesInFolder('/src/Frontend/Cache/Locale', $io, 'frontend cached locale');
$this->removeFilesInFolder('/src/Frontend/Cache/MinifiedCss', $io, 'frontend minified css');
$this->removeFilesInFolder('/src/Frontend/Cache/MinifiedJs', $io, 'frontend minified js');
$this->removeFilesInFolder('/src/Frontend/Cache/Navigation', $io, 'frontend cached navigation');
$this->removeFilesInFolder('/src/Backend/Cache/CompiledTemplates', $io, 'backend compiled templates');
$this->removeFilesInFolder('/src/Backend/Cache/Locale', $io, 'backend cached locale');
$this->removeFilesInFolder('/src/Backend/Cache/MinifiedCss', $io, 'backend minified css');
$this->removeFilesInFolder('/src/Backend/Cache/MinifiedJs', $io, 'backend minified js');
$symfonyCacheClearCommand = $this->getApplication()->find('cache:clear');
$symfonyCacheClearCommand->run(new ArrayInput(['--no-warmup' => true]), $output);
// clear file info cache
clearstatcache();
$io->success('Cache is cleared');
}
/**
* Remove the files in a given folder
*
* @param string $path
* @param SymfonyStyle $io
* @param string $name
*/
private function removeFilesInFolder(string $path, SymfonyStyle $io, string $name): void
{
$fullPath = realpath(__DIR__ . '/../../..' . $path);
// I use a rm-command because this is much faster then using the finder/filesystem-component
$command = 'find %1$s ! -name ".gitignore" -type f ! -path *.svn/* -type f | xargs rm -f';
shell_exec(sprintf($command, $fullPath));
$io->comment(sprintf('Removed %1$s', $name));
}
}