Skip to content

Commit 52ca58d

Browse files
authored
feat: publish imports (#643)
1 parent 2051dd4 commit 52ca58d

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

src/Tempest/Auth/src/AuthInstaller.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Tempest\Core\PublishesFiles;
99
use function Tempest\src_path;
1010

11-
final readonly class AuthInstaller implements Installer
11+
final class AuthInstaller implements Installer
1212
{
1313
use PublishesFiles;
1414

@@ -34,5 +34,7 @@ public function install(): void
3434
destination: $destination,
3535
);
3636
}
37+
38+
$this->publishImports();
3739
}
3840
}

src/Tempest/Core/src/PublishesFiles.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ trait PublishesFiles
1616
{
1717
use HasConsole;
1818

19+
private array $publishedFiles = [];
20+
21+
private array $publishedClasses = [];
22+
1923
/**
2024
* @param Closure(string $source, string $destination): void|null $callback
2125
*/
@@ -51,13 +55,28 @@ public function publish(
5155

5256
$this->updateClass($destination);
5357

58+
$this->publishedFiles[] = $destination;
59+
5460
if ($callback !== null) {
5561
$callback($source, $destination);
5662
}
5763

5864
$this->success("{$destination} created");
5965
}
6066

67+
public function publishImports(): void
68+
{
69+
foreach ($this->publishedFiles as $file) {
70+
$contents = str(file_get_contents($file));
71+
72+
foreach ($this->publishedClasses as $old => $new) {
73+
$contents = $contents->replace("use {$old};", "use {$new};");
74+
}
75+
76+
file_put_contents($file, $contents);
77+
}
78+
}
79+
6180
private function updateClass(string $destination): void
6281
{
6382
try {
@@ -75,9 +94,13 @@ private function updateClass(string $destination): void
7594
->implode('\\')
7695
->toString();
7796

97+
$oldClassName = $class->getClassName();
98+
7899
$class
79100
->setNamespace($namespace)
80101
->removeClassAttribute(DoNotDiscover::class)
81102
->save($destination);
103+
104+
$this->publishedClasses[$oldClassName] = $class->getClassName();
82105
}
83106
}

src/Tempest/Framework/Installers/FrameworkInstaller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Tempest\Core\PublishesFiles;
99
use function Tempest\root_path;
1010

11-
final readonly class FrameworkInstaller implements Installer
11+
final class FrameworkInstaller implements Installer
1212
{
1313
use PublishesFiles;
1414

src/Tempest/Generation/src/ManipulatesPhpClasses.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public function setNamespace(string $namespace): self
167167
return $this;
168168
}
169169

170+
public function getClassName(): string
171+
{
172+
return $this->namespace . '\\' . $this->classType->getName();
173+
}
174+
170175
public function print(): string
171176
{
172177
if (! $this->namespace) {

tests/Fixtures/TestInstaller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Tempest\Core\PublishesFiles;
99
use function Tempest\src_path;
1010

11-
final readonly class TestInstaller implements Installer
11+
final class TestInstaller implements Installer
1212
{
1313
use PublishesFiles;
1414

tests/Integration/Auth/AuthInstallerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,10 @@ public function test_install_auth(): void
5050
->assertFileContains($path, 'namespace App;')
5151
->assertFileNotContains($path, 'DoNotDiscover');
5252
}
53+
54+
$this->installer->assertFileContains(
55+
'App/User.php',
56+
'use App\UserPermission',
57+
);
5358
}
5459
}

0 commit comments

Comments
 (0)