Skip to content

Commit 335018a

Browse files
authored
chore: installer improvements (#633)
1 parent 6516924 commit 335018a

File tree

7 files changed

+138
-9
lines changed

7 files changed

+138
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App;
4+
5+
final readonly class TestInstallerClass
6+
{
7+
}

src/Tempest/Auth/src/AuthInstaller.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
namespace Tempest\Auth;
66

7-
use Tempest\Core\DoNotDiscover;
87
use Tempest\Core\Installer;
98
use Tempest\Core\PublishesFiles;
10-
use Tempest\Generation\ClassManipulator;
11-
use function Tempest\src_namespace;
129
use function Tempest\src_path;
1310

1411
final readonly class AuthInstaller implements Installer
@@ -35,12 +32,6 @@ public function install(): void
3532
$this->publish(
3633
source: $source,
3734
destination: $destination,
38-
callback: function (string $source, string $destination): void {
39-
(new ClassManipulator($source))
40-
->setNamespace(src_namespace())
41-
->removeClassAttribute(DoNotDiscover::class)
42-
->save($destination);
43-
},
4435
);
4536
}
4637
}

src/Tempest/Core/src/PublishesFiles.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
namespace Tempest\Core;
66

77
use Closure;
8+
use Nette\InvalidStateException;
89
use Tempest\Console\HasConsole;
10+
use Tempest\Generation\ClassManipulator;
11+
use function Tempest\src_namespace;
12+
use function Tempest\src_path;
13+
use function Tempest\Support\str;
914

1015
trait PublishesFiles
1116
{
@@ -44,10 +49,35 @@ public function publish(
4449

4550
copy($source, $destination);
4651

52+
$this->updateClass($destination);
53+
4754
if ($callback !== null) {
4855
$callback($source, $destination);
4956
}
5057

5158
$this->success("{$destination} created");
5259
}
60+
61+
private function updateClass(string $destination): void
62+
{
63+
try {
64+
$class = new ClassManipulator($destination);
65+
} catch (InvalidStateException) {
66+
return;
67+
}
68+
69+
$namespace = str($destination)
70+
->replaceStart(src_path(), src_namespace())
71+
->replaceEnd('.php', '')
72+
->replace('/', '\\')
73+
->explode('\\')
74+
->pop($value)
75+
->implode('\\')
76+
->toString();
77+
78+
$class
79+
->setNamespace($namespace)
80+
->removeClassAttribute(DoNotDiscover::class)
81+
->save($destination);
82+
}
5383
}

tests/Fixtures/TestInstaller.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Fixtures;
6+
7+
use Tempest\Core\Installer;
8+
use Tempest\Core\PublishesFiles;
9+
use function Tempest\src_path;
10+
11+
final readonly class TestInstaller implements Installer
12+
{
13+
use PublishesFiles;
14+
15+
public function getName(): string
16+
{
17+
return 'test';
18+
}
19+
20+
public function install(): void
21+
{
22+
$this->publish(
23+
__DIR__ . '/TestInstallerClass.php',
24+
src_path('Foo/Bar/TestInstallerClass.php')
25+
);
26+
27+
$this->publish(
28+
__DIR__ . '/TestInstallerFile.html',
29+
src_path('View/TestInstallerFile.html')
30+
);
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Fixtures;
6+
7+
use Tempest\Core\DoNotDiscover;
8+
9+
#[DoNotDiscover]
10+
final readonly class TestInstallerClass
11+
{
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html></html>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Core;
6+
7+
use Tempest\Core\ComposerNamespace;
8+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class InstallCommandTest extends FrameworkIntegrationTestCase
14+
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->installer
20+
->configure(
21+
__DIR__ . '/install',
22+
new ComposerNamespace('App\\', __DIR__ . '/install/App'),
23+
)
24+
->setRoot(__DIR__ . '/install');
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
$this->installer->clean();
30+
31+
parent::tearDown();
32+
}
33+
34+
public function test_class_is_adjusted(): void
35+
{
36+
$this->console
37+
->call('install test --force');
38+
39+
$this->installer
40+
->assertFileExists(
41+
path: 'App/Foo/Bar/TestInstallerClass.php',
42+
)
43+
->assertFileNotContains(
44+
path: 'App/Foo/Bar/TestInstallerClass.php',
45+
search: 'DoNotDiscover',
46+
)
47+
->assertFileContains(
48+
path: 'App/Foo/Bar/TestInstallerClass.php',
49+
search: 'namespace App\Foo\Bar;',
50+
)
51+
->assertFileExists(
52+
path: 'App/View/TestInstallerFile.html',
53+
content: '<html></html>',
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)