Skip to content

Commit a8f94a8

Browse files
authored
Merge pull request #193 from ByteInternet/cachetool_update
Cachetool: Don't run by default
2 parents 2c3208c + afafd29 commit a8f94a8

2 files changed

Lines changed: 106 additions & 14 deletions

File tree

src/Deployer/Task/After/CachetoolTask.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ class CachetoolTask extends TaskBase
1919
/**
2020
* @var string[]
2121
*
22+
* CacheTool 9.x/10.x works with PHP >=8.1
2223
* CacheTool 8.x works with PHP >=8.0
2324
* CacheTool 7.x works with PHP >=7.3
2425
* CacheTool 6.x works with PHP >=7.3
2526
* CacheTool 5.x works with PHP >=7.2
2627
* CacheTool 4.x works with PHP >=7.1
2728
*/
2829
private $versionBinaryMapping = [
29-
8 => 'https://github.com/gordalina/cachetool/releases/download/8.4.0/cachetool.phar',
30+
10 => 'https://github.com/gordalina/cachetool/releases/download/10.0.0/cachetool.phar',
31+
8 => 'https://github.com/gordalina/cachetool/releases/download/8.6.1/cachetool.phar',
3032
7 => 'https://github.com/gordalina/cachetool/releases/download/7.1.0/cachetool.phar',
3133
6 => 'https://github.com/gordalina/cachetool/releases/download/6.6.0/cachetool.phar',
3234
5 => 'https://github.com/gordalina/cachetool/releases/download/5.1.3/cachetool.phar',
@@ -35,7 +37,6 @@ class CachetoolTask extends TaskBase
3537

3638
public function register(): void
3739
{
38-
after('deploy:symlink', 'cachetool:clear:opcache');
3940
after('cachetool:clear:opcache', 'cachetool:cleanup');
4041
}
4142

@@ -52,10 +53,10 @@ public function configure(Configuration $config): void
5253
$cachetoolBinary = get('cachetool_binary');
5354

5455
within('{{release_path}}', function () {
55-
run('curl -L -o cachetool.phar ' . $this->getCachetoolUrl());
56+
$phpVersion = $this->getPhpVersion();
5657
$cachetoolBinary = '{{release_path}}/cachetool.phar';
57-
58-
writeln(sprintf("Downloaded cachetool %s for PHP %f", $cachetoolBinary, $this->getPhpVersion()));
58+
run('curl -L -o cachetool.phar ' . $this->getCachetoolUrl($phpVersion));
59+
writeln(sprintf("Downloaded cachetool %s for PHP %s", $cachetoolBinary, $phpVersion));
5960
return $cachetoolBinary;
6061
});
6162
return $cachetoolBinary;
@@ -103,30 +104,36 @@ public function configure(Configuration $config): void
103104
});
104105
}
105106

106-
protected function getPhpVersion(): float
107+
protected function getPhpVersion(): string
107108
{
108-
return (float) run('{{bin/php}} -r "echo PHP_VERSION . \" - \" . PHP_VERSION_ID;"');
109+
return run('{{bin/php}} -r "echo PHP_VERSION;"');
109110
}
110111

111-
public function getCachetoolUrl(): string
112+
public function getCachetoolUrl(?string $phpVersion = null): string
112113
{
113-
$phpVersion = $this->getPhpVersion();
114-
if ($phpVersion >= 8.0) {
114+
$phpVersion = $phpVersion ?? $this->getPhpVersion();
115+
116+
if (version_compare($phpVersion, '8.1.0', '>=')) {
117+
return $this->versionBinaryMapping[10];
118+
}
119+
120+
if (version_compare($phpVersion, '8.0.0', '>=')) {
115121
return $this->versionBinaryMapping[8];
116122
}
117123

118-
if ($phpVersion >= 7.3) {
124+
if (version_compare($phpVersion, '7.3.0', '>=')) {
119125
return $this->versionBinaryMapping[7];
120126
}
121127

122-
if ($phpVersion >= 7.2) {
128+
if (version_compare($phpVersion, '7.2.0', '>=')) {
123129
return $this->versionBinaryMapping[5];
124130
}
125131

126-
if ($phpVersion >= 7.1) {
132+
if (version_compare($phpVersion, '7.1.0', '>=')) {
127133
return $this->versionBinaryMapping[4];
128134
}
129135

130-
return $this->versionBinaryMapping[8];
136+
// Default to latest for unknown/newer PHP versions
137+
return $this->versionBinaryMapping[10];
131138
}
132139
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Deploy\Tests\Unit\Deployer\Task\After;
6+
7+
use Hypernode\Deploy\Deployer\Task\After\CachetoolTask;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class CachetoolTaskTest extends TestCase
11+
{
12+
private CachetoolTask $task;
13+
14+
protected function setUp(): void
15+
{
16+
$this->task = new CachetoolTask();
17+
}
18+
19+
/**
20+
* @dataProvider phpVersionToCachetoolUrlProvider
21+
*/
22+
public function testGetCachetoolUrlReturnsCorrectVersionForPhpVersion(
23+
string $phpVersion,
24+
string $expectedUrlPart
25+
): void {
26+
$result = $this->task->getCachetoolUrl($phpVersion);
27+
28+
$this->assertStringContainsString($expectedUrlPart, $result);
29+
}
30+
31+
/**
32+
* @return array<string, array{string, string}>
33+
*/
34+
public static function phpVersionToCachetoolUrlProvider(): array
35+
{
36+
return [
37+
// PHP 8.1+ should get cachetool 10.x
38+
'PHP 8.3.15 gets cachetool 10' => ['8.3.15', '10.0.0'],
39+
'PHP 8.2.0 gets cachetool 10' => ['8.2.0', '10.0.0'],
40+
'PHP 8.1.0 gets cachetool 10' => ['8.1.0', '10.0.0'],
41+
'PHP 8.1.27 gets cachetool 10' => ['8.1.27', '10.0.0'],
42+
43+
// PHP 8.0.x should get cachetool 8.x
44+
'PHP 8.0.0 gets cachetool 8' => ['8.0.0', '8.6.1'],
45+
'PHP 8.0.30 gets cachetool 8' => ['8.0.30', '8.6.1'],
46+
47+
// PHP 7.3+ should get cachetool 7.x
48+
'PHP 7.4.33 gets cachetool 7' => ['7.4.33', '7.1.0'],
49+
'PHP 7.3.0 gets cachetool 7' => ['7.3.0', '7.1.0'],
50+
'PHP 7.3.33 gets cachetool 7' => ['7.3.33', '7.1.0'],
51+
52+
// PHP 7.2.x should get cachetool 5.x
53+
'PHP 7.2.0 gets cachetool 5' => ['7.2.0', '5.1.3'],
54+
'PHP 7.2.34 gets cachetool 5' => ['7.2.34', '5.1.3'],
55+
56+
// PHP 7.1.x should get cachetool 4.x
57+
'PHP 7.1.0 gets cachetool 4' => ['7.1.0', '4.1.1'],
58+
'PHP 7.1.33 gets cachetool 4' => ['7.1.33', '4.1.1'],
59+
60+
// Unsupported/old PHP versions fall back to latest (10.x)
61+
'PHP 7.0.33 falls back to cachetool 10' => ['7.0.33', '10.0.0'],
62+
'PHP 5.6.40 falls back to cachetool 10' => ['5.6.40', '10.0.0'],
63+
64+
// Future PHP versions should get latest (10.x)
65+
'PHP 9.0.0 gets cachetool 10' => ['9.0.0', '10.0.0'],
66+
'PHP 8.4.0 gets cachetool 10' => ['8.4.0', '10.0.0'],
67+
];
68+
}
69+
70+
public function testGetCachetoolUrlReturnsFullGithubUrl(): void
71+
{
72+
$result = $this->task->getCachetoolUrl('8.2.0');
73+
74+
$this->assertStringStartsWith('https://github.com/gordalina/cachetool/releases/download/', $result);
75+
$this->assertStringEndsWith('/cachetool.phar', $result);
76+
}
77+
78+
public function testGetCachetoolUrlForPhp71ReturnsLegacyUrl(): void
79+
{
80+
$result = $this->task->getCachetoolUrl('7.1.0');
81+
82+
// Cachetool 4.x uses a different URL format (gordalina.github.io)
83+
$this->assertStringStartsWith('https://gordalina.github.io/cachetool/downloads/', $result);
84+
}
85+
}

0 commit comments

Comments
 (0)