1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Incapsula\Command; |
6
|
|
|
|
7
|
|
|
use function json_encode; |
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class ListAllCacheRulesCommand extends SitesListCommand |
14
|
|
|
{ |
15
|
|
|
protected function configure(): void |
16
|
|
|
{ |
17
|
|
|
parent::configure(); |
18
|
|
|
|
19
|
|
|
$this |
20
|
|
|
->setName('sites:listcacherules') |
21
|
|
|
->addOption('json', null, InputOption::VALUE_NONE, 'Output as JSON') |
22
|
|
|
->setDescription('List all cache rules for all sites') |
23
|
|
|
; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
27
|
|
|
{ |
28
|
|
|
$rules = $this->getRules(); |
29
|
|
|
|
30
|
|
|
if (true === $input->getOption('json')) { |
31
|
|
|
$output->write(json_encode($rules)); |
32
|
|
|
|
33
|
|
|
return 0; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$table = new Table($output); |
37
|
|
|
$table->setHeaders(['Site ID', 'Site Name', 'Rule ID', 'Action', 'Disabled?', 'Rule Name', 'Rule Filter', 'TTL']); |
38
|
|
|
|
39
|
|
|
foreach ($rules as $rule) { |
40
|
|
|
$table->addRow([ |
41
|
|
|
$rule['site_id'], |
42
|
|
|
$rule['site_name'], |
43
|
|
|
$rule['rule_id'], |
44
|
|
|
$rule['action'], |
45
|
|
|
$rule['disabled'], |
46
|
|
|
$rule['name'], |
47
|
|
|
$rule['filter'], |
48
|
|
|
$rule['ttl'], |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$table->render(); |
53
|
|
|
|
54
|
|
|
return 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getRules(): array |
58
|
|
|
{ |
59
|
|
|
$api = $this->client->sites(); |
60
|
|
|
$sites = $this->getSites(); |
61
|
|
|
$rules = []; |
62
|
|
|
|
63
|
|
|
foreach ($sites as $site) { |
64
|
|
|
$page = 0; |
65
|
|
|
while (true) { |
66
|
|
|
$response = $api->listCacheRules($site['site_id'], 50, $page); |
67
|
|
|
|
68
|
|
|
if (empty($response)) { |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($response as $ruleType => $rulesOfType) { |
73
|
|
|
foreach ($rulesOfType as $ruleData) { |
74
|
|
|
$ttl = ''; |
75
|
|
|
|
76
|
|
|
if (isset($ruleData['ttl'], $ruleData['ttlUnit'])) { |
77
|
|
|
$ttl = sprintf('%s %s', $ruleData['ttl'], $ruleData['ttlUnit']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$rules[] = [ |
81
|
|
|
'site_id' => $site['site_id'], |
82
|
|
|
'site_name' => $site['display_name'], |
83
|
|
|
'rule_id' => $ruleData['id'], |
84
|
|
|
'action' => $ruleType, |
85
|
|
|
'disabled' => $ruleData['disabled'], |
86
|
|
|
'name' => $ruleData['name'], |
87
|
|
|
'filter' => $ruleData['filter'], |
88
|
|
|
'ttl' => $ttl, |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
++$page; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $rules; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|