Skip to content

Commit

Permalink
feat: subscribe for import.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsehszroc committed Feb 17, 2025
1 parent a3b0eb2 commit c8a9864
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
51 changes: 45 additions & 6 deletions Src/ArrayPhpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,43 @@

namespace TheWebSolver\Codegarage\Generator;

use Closure;
use Nette\Utils\Strings;
use Nette\PhpGenerator\Dumper;
use Nette\PhpGenerator\Helpers;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\Printer;
use TheWebSolver\Codegarage\Generator\Traits\ArrayExport;
use TheWebSolver\Codegarage\Generator\Traits\ImportResolver;

class ArrayPhpFile {
use ArrayExport;
use ArrayExport, ImportResolver;


/** @var mixed[] */
private array $content;
private string|int $parentKey;

private static bool $subscribeImports = true;

public function __construct(
public readonly PhpFile $phpFile = new PhpFile(),
private Printer $printer = new Printer(),
private Dumper $dumper = new Dumper()
) {
$phpFile->setStrictTypes();
$phpFile->setStrictTypes()->addNamespace( $this->setNamespace()->getNamespace() );
}

public static function subscribeForImport( bool $addUseStatement = true ): Closure {
$previousSubscription = self::$subscribeImports;
self::$subscribeImports = $addUseStatement;

return static fn() => self::$subscribeImports = $previousSubscription;
}

public static function isSubscribedForImport(): bool {
return self::$subscribeImports;
}

/**
Expand All @@ -34,6 +51,8 @@ public function set( array &$array, string $key, mixed $value ): void {
$index = $key;

foreach ( $keys as $i => $key ) {
$this->maybeAddUseOf( $key );

if ( count( $keys ) === 1 ) {
$index = $key;

Expand Down Expand Up @@ -69,8 +88,8 @@ protected function export( mixed &$content, int $level = 0, int $column = 0 ): s
}

public function exportString( string $content ): string {
return str_ends_with( $content, needle: '::class' )
? (string) ( new Literal( $content ) )
return ( ( $alias = $this->resolveImports( $content ) ) !== $content )
? (string) new Literal( $alias )
: $this->dumper->dump( $content );
}

Expand All @@ -81,14 +100,14 @@ public function childOf( string|int $parentKey ): static {
return $this;
}

public function addContent( string|int $key, mixed $value, string|int $index = null ): static {
public function addContent( string|int $key, mixed $value ): static {
$parentKey = ( $this->parentKey ?? null );
$array = $this->content ?? array();

unset( $this->parentKey );

if ( ! $parentKey ) {
$this->content[ $index ?? $key ] = $value;
$this->content[ $key ] = $value;

return $this;
}
Expand All @@ -107,11 +126,31 @@ public function addCallable( string|int $key, string|array $value ): static {
default => $value,
};

$this->maybeAddUseOf( $fqcn );

return $this->addContent( $key, array( $fqcn, $methodName ) );
}

/** @return mixed[] */
public function getContent(): array {
return $this->content;
}

protected function getAliasOf( string $import ): string {
if ( ! in_array( $import, $uses = $this->getNamespace()->getUses(), strict: true ) ) {
return $import;
}

return ( $alias = array_search( $import, $uses, strict: true ) ) ? "{$alias}::class" : $import;
}

protected function resolveImports( string $content ): string {
return self::isSubscribedForImport() ? $this->getAliasOf( $content ) : $content;
}

private function maybeAddUseOf( string $key ): void {
self::isSubscribedForImport()
&& Helpers::isNamespaceIdentifier( $key )
&& $this->addUseStatementOf( $key );
}
}
23 changes: 23 additions & 0 deletions Tests/ArrayPhpFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
use TheWebSolver\Codegarage\Generator\Enum\Argument;

class ArrayPhpFileTest extends TestCase {
#[Test]
public function itEnsuresImportSubscriptionWorks(): void {
$this->assertTrue( ArrayPhpFile::isSubscribedForImport() );

$unsubscribe = ArrayPhpFile::subscribeForImport();

$this->assertTrue( ArrayPhpFile::isSubscribedForImport() );
$unsubscribe();
$this->assertTrue(
ArrayPhpFile::isSubscribedForImport(),
'Default is set to true, so reset (unsubscribe) has no effect.'
);

$unsubscribe = ArrayPhpFile::subscribeForImport( false );

$this->assertFalse( ArrayPhpFile::isSubscribedForImport() );
$unsubscribe();
$this->assertTrue(
ArrayPhpFile::isSubscribedForImport(),
'Resets to previous state before subscription.'
);
}

#[Test]
public function classNameKeyAndCallableMethodAreImportedProperly(): void {
$this->assertTrue( true );
Expand Down

0 comments on commit c8a9864

Please sign in to comment.