This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
console.php
87 lines (78 loc) · 3.03 KB
/
console.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
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
<?php
require_once __DIR__.'/vendor/autoload.php';
$app = require(__DIR__.'/app.php');
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
$console = new Application();
$console
->register('cache:clear-old-file')
->setDefinition(array(
new InputOption('ttl', 't', InputOption::VALUE_OPTIONAL, 'ttl', 300), // 5 minutes
new InputOption('dry-run', null, InputOption::VALUE_NONE, ''),
new InputOption('without-hashed-file', null, InputOption::VALUE_NONE, ''),
new InputOption('hashed-file-ttl', null, InputOption::VALUE_OPTIONAL, '', 60*60*24*30*6) //6 months
))
->setDescription('Clear old cache files')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$ttl = $input->getOption('ttl');
$dryRun = $input->getOption('dry-run');
$dryRunText = '';
if ($dryRun) {
$dryRunText = ' (dry-run)';
}
$finder = new Finder();
$fs = new Filesystem();
$finder->files()
->notName('/\$[0-9a-f]+\.json$/')
->date('until '.$ttl.' sec ago')
->in($app['cache_dir']);
foreach ($finder as $file) {
$output->writeln('<info>Remove '.$file->getRealpath().$dryRunText.'</info>');
if (!$dryRun) {
$fs->remove($file);
}
}
if (!$input->getOption('without-hashed-file')) {
$hashedFileFinder = new Finder();
$hashedFileTtl = $input->getOption('hashed-file-ttl');
$hashedFileFinder->files()
->name('/\$[0-9a-f]+\.json$/')
->date('until '.$hashedFileTtl.' sec ago')
->in($app['cache_dir']);
foreach ($hashedFileFinder as $file) {
$output->writeln('<info>Remove '.$file->getRealpath().$dryRunText.'</info>');
if (!$dryRun) {
$fs->remove($file);
}
}
}
});
$console
->register('cache:clear-all')
->setDefinition(array(
new InputOption('dry-run', null, InputOption::VALUE_NONE, ''),
))
->setDescription('Clear all cache files')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$dryRun = $input->getOption('dry-run');
$dryRunText = '';
if ($dryRun) {
$dryRunText = ' (dry-run)';
}
$finder = new Finder();
$fs = new Filesystem();
$finder->files()->in($app['cache_dir']);
foreach ($finder as $file) {
$output->writeln('<info>Remove '.$file->getRealpath().$dryRunText.'</info>');
if (!$dryRun) {
$fs->remove($file);
}
}
});
$console->run();