1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Buttress\Concrete\CommandBus\Handler; |
4
|
|
|
|
5
|
|
|
use Buttress\Concrete\Client\Connection\Connection; |
6
|
|
|
use Buttress\Concrete\Client\Connection\ModernConnection; |
7
|
|
|
use Buttress\Concrete\CommandBus\Command\Cache\Clear; |
8
|
|
|
use Buttress\Concrete\Locator\Site; |
9
|
|
|
use Concrete\Core\Site\Service; |
10
|
|
|
use League\CLImate\CLImate; |
11
|
|
|
|
12
|
|
|
class CacheHandler |
13
|
|
|
{ |
14
|
|
|
/** @var \Buttress\Concrete\Locator\Site */ |
15
|
|
|
private $site; |
16
|
|
|
|
17
|
|
|
/** @var \League\CLImate\CLImate */ |
18
|
|
|
private $cli; |
19
|
|
|
|
20
|
|
|
/** @var \Buttress\Concrete\Client\Connection\Connection */ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
public function __construct(Connection $connection, CLImate $cli, Site $site) |
24
|
|
|
{ |
25
|
|
|
$this->connection = $connection; |
26
|
|
|
$this->cli = $cli; |
27
|
|
|
$this->site = $site; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Handle the Clear command |
32
|
|
|
* @param \Buttress\Concrete\CommandBus\Command\Cache\Clear $clear |
33
|
|
|
*/ |
34
|
|
|
public function handleClear(Clear $clear) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
if ($this->connection instanceof ModernConnection) { |
38
|
|
|
$this->clearModern($this->connection); |
39
|
|
|
} else { |
40
|
|
|
$this->clearLegacy($this->connection); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Clear cache for a ModernConnection |
46
|
|
|
* @param \Buttress\Concrete\Client\Connection\ModernConnection $connection |
47
|
|
|
*/ |
48
|
|
|
private function clearModern(ModernConnection $connection) |
49
|
|
|
{ |
50
|
|
|
$app = $connection->getApplication(); |
51
|
|
|
$site = $app->make(Service::class)->getDefault()->getSiteName(); |
52
|
|
|
|
53
|
|
|
if ($this->confirm($site)) { |
54
|
|
|
$app->clearCaches(); |
55
|
|
|
$this->notify($site); |
56
|
|
|
} else { |
57
|
|
|
$this->cli->dim('okay.'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Clear cache for a LegacyConnection |
63
|
|
|
* @param \Buttress\Concrete\CommandBus\Handler\LegacyConnection $connection |
64
|
|
|
*/ |
65
|
|
|
private function clearLegacy(LegacyConnection $connection) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$site = SITE; |
68
|
|
|
if ($this->confirm($site)) { |
69
|
|
|
Loader::library('cache'); |
70
|
|
|
$cache = new Cache; |
71
|
|
|
$cache->flush(); |
72
|
|
|
$this->notify($site); |
73
|
|
|
} else { |
74
|
|
|
$this->cli->dim('okay.'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function confirm($site) |
79
|
|
|
{ |
80
|
|
|
/** @var \League\CLImate\TerminalObject\Dynamic\Confirm $input */ |
81
|
|
|
$input = $this->cli->confirm( |
82
|
|
|
sprintf('Really clear the cache on <bold>%s</bold>?', $site) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return strtolower($input->accept(['Yes', 'No'], true)->prompt()) === 'yes'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function notify($site) |
89
|
|
|
{ |
90
|
|
|
$this->cli->info( |
91
|
|
|
sprintf('Cleared cache on <bold>%s</bold>', $site) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.