-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCachetoolTask.php
More file actions
139 lines (113 loc) · 4.46 KB
/
CachetoolTask.php
File metadata and controls
139 lines (113 loc) · 4.46 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Hypernode\Deploy\Deployer\Task\After;
use Hypernode\Deploy\Deployer\Task\TaskBase;
use Hypernode\DeployConfiguration\Configuration;
use function Deployer\after;
use function Deployer\desc;
use function Deployer\get;
use function Deployer\run;
use function Deployer\set;
use function Deployer\task;
use function Deployer\within;
use function Deployer\writeln;
class CachetoolTask extends TaskBase
{
/**
* @var string[]
*
* CacheTool 9.x/10.x works with PHP >=8.1
* CacheTool 8.x works with PHP >=8.0
* CacheTool 7.x works with PHP >=7.3
* CacheTool 6.x works with PHP >=7.3
* CacheTool 5.x works with PHP >=7.2
* CacheTool 4.x works with PHP >=7.1
*/
private $versionBinaryMapping = [
10 => 'https://github.com/gordalina/cachetool/releases/download/10.0.0/cachetool.phar',
8 => 'https://github.com/gordalina/cachetool/releases/download/8.6.1/cachetool.phar',
7 => 'https://github.com/gordalina/cachetool/releases/download/7.1.0/cachetool.phar',
6 => 'https://github.com/gordalina/cachetool/releases/download/6.6.0/cachetool.phar',
5 => 'https://github.com/gordalina/cachetool/releases/download/5.1.3/cachetool.phar',
4 => 'https://gordalina.github.io/cachetool/downloads/cachetool-4.1.1.phar',
];
public function register(): void
{
after('cachetool:clear:opcache', 'cachetool:cleanup');
}
public function configure(Configuration $config): void
{
set('cachetool_binary', function () {
set('cachetool_args', '');
return "cachetool.phar";
});
set('cachetool', '127.0.0.1:9000');
set('bin/cachetool', function () {
$cachetoolBinary = get('cachetool_binary');
within('{{release_path}}', function () {
$phpVersion = $this->getPhpVersion();
$cachetoolBinary = '{{release_path}}/cachetool.phar';
run('curl -L -o cachetool.phar ' . $this->getCachetoolUrl($phpVersion));
writeln(sprintf("Downloaded cachetool %s for PHP %s", $cachetoolBinary, $phpVersion));
return $cachetoolBinary;
});
return $cachetoolBinary;
});
set('cachetool_options', function () {
$options = get('cachetool');
$fullOptions = get('cachetool_args');
if (strlen($fullOptions) > 0) {
$options = "{$fullOptions}";
} elseif (strlen($options) > 0) {
$options = "--fcgi={$options}";
}
return $options;
});
desc('Clearing APC system cache');
task('cachetool:clear:apc', function () {
run("cd {{release_path}} && {{bin/php}} {{bin/cachetool}} apc:cache:clear system {{cachetool_options}}");
});
/**
* Clear opcache cache
*/
desc('Clearing OPcode cache');
task('cachetool:clear:opcache', function () {
run("cd {{release_path}} && {{bin/php}} {{bin/cachetool}} opcache:reset {{cachetool_options}}");
});
/**
* Clear APCU cache
*/
desc('Clearing APCu system cache');
task('cachetool:clear:apcu', function () {
run("cd {{release_path}} && {{bin/php}} {{bin/cachetool}} apcu:cache:clear {{cachetool_options}}");
});
task('cachetool:clear:opcache');
task('cachetool:cleanup', function () {
run('cd {{deploy_path}} && rm -f current/{{bin/cachetool}}');
});
}
protected function getPhpVersion(): string
{
return run('{{bin/php}} -r "echo PHP_VERSION;"');
}
public function getCachetoolUrl(?string $phpVersion = null): string
{
$phpVersion = $phpVersion ?? $this->getPhpVersion();
if (version_compare($phpVersion, '8.1.0', '>=')) {
return $this->versionBinaryMapping[10];
}
if (version_compare($phpVersion, '8.0.0', '>=')) {
return $this->versionBinaryMapping[8];
}
if (version_compare($phpVersion, '7.3.0', '>=')) {
return $this->versionBinaryMapping[7];
}
if (version_compare($phpVersion, '7.2.0', '>=')) {
return $this->versionBinaryMapping[5];
}
if (version_compare($phpVersion, '7.1.0', '>=')) {
return $this->versionBinaryMapping[4];
}
// Default to latest for unknown/newer PHP versions
return $this->versionBinaryMapping[10];
}
}