Skip to content

Commit 9ab4cf3

Browse files
committed
feat: Add Cms\License::delete() & ::root()
1 parent 242555b commit 9ab4cf3

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/Cms/License.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class License
3030

3131
protected const SALT = 'kwAHMLyLPBnHEskzH9pPbJsBxQhKXZnX';
3232

33+
protected App $kirby;
34+
3335
// cache
3436
protected LicenseStatus $status;
3537
protected LicenseType $type;
@@ -50,6 +52,8 @@ public function __construct(
5052
if ($email !== null) {
5153
$this->email = $this->normalizeEmail($email);
5254
}
55+
56+
$this->kirby = App::instance();
5357
}
5458

5559
/**
@@ -100,6 +104,15 @@ public function date(
100104
return $this->date !== null ? Str::date(strtotime($this->date), $format, $handler) : null;
101105
}
102106

107+
/**
108+
* Deletes the license file if it exists
109+
* @since 5.0.0
110+
*/
111+
public function delete(): bool
112+
{
113+
return F::remove($this->root());
114+
}
115+
103116
/**
104117
* Returns the activation domain if available
105118
*/
@@ -179,7 +192,7 @@ public function isLegacy(): bool
179192
}
180193

181194
// get release date of current major version
182-
$major = Str::before(App::instance()->version(), '.');
195+
$major = Str::before($this->kirby->version(), '.');
183196
$release = strtotime(static::HISTORY[$major] ?? '');
184197

185198
// if there's no matching version in the history
@@ -219,7 +232,7 @@ public function isOnCorrectDomain(): bool
219232
}
220233

221234
// compare domains
222-
if ($this->normalizeDomain(App::instance()->system()->indexUrl()) !== $this->normalizeDomain($this->domain)) {
235+
if ($this->normalizeDomain($this->kirby->system()->indexUrl()) !== $this->normalizeDomain($this->domain)) {
223236
return false;
224237
}
225238

@@ -236,7 +249,7 @@ public function isSigned(): bool
236249
}
237250

238251
// get the public key
239-
$pubKey = F::read(App::instance()->root('kirby') . '/kirby.pub');
252+
$pubKey = F::read($this->kirby->root('kirby') . '/kirby.pub');
240253

241254
// verify the license signature
242255
$data = json_encode($this->signatureData());
@@ -328,7 +341,7 @@ public static function polyfill(array $license): array
328341
public static function read(): static
329342
{
330343
try {
331-
$license = Json::read(App::instance()->root('license'));
344+
$license = Json::read(static::root());
332345
} catch (Throwable) {
333346
return new static();
334347
}
@@ -409,6 +422,15 @@ public function request(string $path, array $data): array
409422
// @codeCoverageIgnoreEnd
410423
}
411424

425+
/**
426+
* Returns the root path to the license file
427+
* @since 5.0.0
428+
*/
429+
public static function root(): string
430+
{
431+
return App::instance()->root('license');
432+
}
433+
412434
/**
413435
* Saves the license in the config folder
414436
*/
@@ -420,11 +442,11 @@ public function save(): bool
420442
);
421443
}
422444

423-
// where to store the license file
424-
$file = App::instance()->root('license');
425-
426445
// save the license information
427-
return Json::write($file, $this->content());
446+
return Json::write(
447+
file: $this->root(),
448+
data: $this->content()
449+
);
428450
}
429451

430452
/**

tests/Cms/System/LicenseTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kirby\Cms;
44

55
use Kirby\Exception\InvalidArgumentException;
6+
use Kirby\Filesystem\F;
67
use Kirby\Toolkit\Str;
78
use PHPUnit\Framework\Attributes\CoversClass;
89
use PHPUnit\Framework\Attributes\DataProvider;
@@ -85,6 +86,16 @@ public function testDate(): void
8586
$this->assertSame('1/12/2023 00:00', $license->date('d/M/yyyy HH:mm', 'intl'));
8687
}
8788

89+
public function testDelete(): void
90+
{
91+
$license = new License();
92+
F::write($license->root(), 'test');
93+
94+
$this->assertFileExists($license->root());
95+
$this->assertTrue($license->delete());
96+
$this->assertFileDoesNotExist($license->root());
97+
}
98+
8899
public function testDomain(): void
89100
{
90101
$license = new License(
@@ -357,6 +368,14 @@ public function testRenewal(): void
357368
$this->assertNull($license->renewal('Y-m-d'));
358369
}
359370

371+
public function testRoot(): void
372+
{
373+
$this->assertSame(
374+
App::instance()->root('license'),
375+
License::root()
376+
);
377+
}
378+
360379
public function testSaveWhenNotActivatable(): void
361380
{
362381
$license = new License();

0 commit comments

Comments
 (0)