1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\SimpleS3\Console; |
4
|
|
|
|
5
|
|
|
use Matecat\SimpleS3\Client; |
6
|
|
|
use Matecat\SimpleS3\Helpers\File; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
|
14
|
|
|
class CacheStatsCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $s3Client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* CacheStatsCommand constructor. |
23
|
|
|
* |
24
|
|
|
* @param Client $s3Client |
25
|
|
|
* @param null $name |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct(Client $s3Client, $name = null) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($name); |
30
|
|
|
|
31
|
|
|
$this->s3Client = $s3Client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function configure() |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('ss3:cache:stats') |
38
|
|
|
->setDescription('Get the cache statistics.') |
39
|
|
|
->setHelp('This command displays the cache statistics.') |
40
|
|
|
->addArgument('bucket', InputArgument::REQUIRED, 'The name of the bucket') |
41
|
|
|
->addArgument('prefix', InputArgument::REQUIRED, 'The prefix in the bucket') |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
if (false === $this->s3Client->hasCache()) { |
48
|
|
|
throw new \Exception('Cache in not enabled. You have to enable caching to use this command'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
if (false === is_string($input->getArgument('bucket')) or false === is_string($input->getArgument('prefix'))) { |
53
|
|
|
throw new \InvalidArgumentException('Provided bucket or prefix name were not strings'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$bucket = $input->getArgument('bucket'); |
57
|
|
|
$prefix = $input->getArgument('prefix'); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$items = $this->s3Client->getItemsInABucket([ |
61
|
|
|
'bucket' => $bucket, |
62
|
|
|
'prefix' => $prefix, |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$tableFeed = []; |
66
|
|
|
foreach ($items as $key) { |
67
|
|
|
$inCache = $this->s3Client->getCache()->search($bucket, $key); |
68
|
|
|
if (count($inCache) > 0) { |
69
|
|
|
$index = $this->getDirName($inCache[0]); |
70
|
|
|
|
71
|
|
|
$files = []; |
72
|
|
|
foreach ($inCache as $item) { |
73
|
|
|
$files[$item] = $this->s3Client->getConn()->doesObjectExist($bucket, $item); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$tableFeed[$index] = [ |
77
|
|
|
'count' => count($inCache), |
78
|
|
|
'files' => $files, |
79
|
|
|
'ttl' => $this->s3Client->getCache()->ttl($bucket, $key), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$table = new Table($output); |
85
|
|
|
$table->setHeaders(['prefix', 'count', 'ttl', 'files', 'align']); |
86
|
|
|
|
87
|
|
|
foreach ($tableFeed as $prefix => $data) { |
88
|
|
|
$count = (int)$data['count']; |
89
|
|
|
|
90
|
|
|
$files = implode(PHP_EOL, array_keys($data['files'])); |
91
|
|
|
$enabled = implode(PHP_EOL, $data['files']); |
92
|
|
|
$enabled = str_replace('1', '<fg=green>✓</>', $enabled); |
93
|
|
|
$enabled = str_replace('0', '<fg=red>✗</>', $enabled); |
94
|
|
|
|
95
|
|
|
$table->addRow([ |
96
|
|
|
$prefix, $count, $data['ttl'], $files, $enabled |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
$table->render(); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
$io = new SymfonyStyle($input, $output); |
102
|
|
|
$io->error('No results were found'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $item |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private function getDirName($item) |
112
|
|
|
{ |
113
|
|
|
if (File::endsWith($item, $this->s3Client->getPrefixSeparator())) { |
114
|
|
|
return $item; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$fileInfo = File::getPathInfo($item); |
118
|
|
|
|
119
|
|
|
return $fileInfo['dirname'] . $this->s3Client->getPrefixSeparator(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|