Skip to content

Commit

Permalink
feat: publish imports (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan-casey committed Oct 29, 2024
1 parent 9cb48b7 commit 823d03b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/PublishesFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ trait PublishesFiles
{
use HasConsole;

private array $publishedFiles = [];

private array $publishedClasses = [];

/**
* @param Closure(string $source, string $destination): void|null $callback
*/
Expand Down Expand Up @@ -51,13 +55,28 @@ public function publish(

$this->updateClass($destination);

$this->publishedFiles[] = $destination;

if ($callback !== null) {
$callback($source, $destination);
}

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

public function publishImports(): void
{
foreach ($this->publishedFiles as $file) {
$contents = str(file_get_contents($file));

foreach ($this->publishedClasses as $old => $new) {
$contents = $contents->replace("use {$old};", "use {$new};");
}

file_put_contents($file, $contents);
}
}

private function updateClass(string $destination): void
{
try {
Expand All @@ -75,9 +94,13 @@ private function updateClass(string $destination): void
->implode('\\')
->toString();

$oldClassName = $class->getClassName();

$class
->setNamespace($namespace)
->removeClassAttribute(DoNotDiscover::class)
->save($destination);

$this->publishedClasses[$oldClassName] = $class->getClassName();
}
}

0 comments on commit 823d03b

Please sign in to comment.