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