Skip to content

Commit c8a9864

Browse files
committed
feat: subscribe for import.
1 parent a3b0eb2 commit c8a9864

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

Src/ArrayPhpFile.php

+45-6
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,43 @@
33

44
namespace TheWebSolver\Codegarage\Generator;
55

6+
use Closure;
67
use Nette\Utils\Strings;
78
use Nette\PhpGenerator\Dumper;
9+
use Nette\PhpGenerator\Helpers;
810
use Nette\PhpGenerator\Literal;
911
use Nette\PhpGenerator\PhpFile;
1012
use Nette\PhpGenerator\Printer;
1113
use TheWebSolver\Codegarage\Generator\Traits\ArrayExport;
14+
use TheWebSolver\Codegarage\Generator\Traits\ImportResolver;
1215

1316
class ArrayPhpFile {
14-
use ArrayExport;
17+
use ArrayExport, ImportResolver;
18+
1519

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

24+
private static bool $subscribeImports = true;
25+
2026
public function __construct(
2127
public readonly PhpFile $phpFile = new PhpFile(),
2228
private Printer $printer = new Printer(),
2329
private Dumper $dumper = new Dumper()
2430
) {
25-
$phpFile->setStrictTypes();
31+
$phpFile->setStrictTypes()->addNamespace( $this->setNamespace()->getNamespace() );
32+
}
33+
34+
public static function subscribeForImport( bool $addUseStatement = true ): Closure {
35+
$previousSubscription = self::$subscribeImports;
36+
self::$subscribeImports = $addUseStatement;
37+
38+
return static fn() => self::$subscribeImports = $previousSubscription;
39+
}
40+
41+
public static function isSubscribedForImport(): bool {
42+
return self::$subscribeImports;
2643
}
2744

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

3653
foreach ( $keys as $i => $key ) {
54+
$this->maybeAddUseOf( $key );
55+
3756
if ( count( $keys ) === 1 ) {
3857
$index = $key;
3958

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

7190
public function exportString( string $content ): string {
72-
return str_ends_with( $content, needle: '::class' )
73-
? (string) ( new Literal( $content ) )
91+
return ( ( $alias = $this->resolveImports( $content ) ) !== $content )
92+
? (string) new Literal( $alias )
7493
: $this->dumper->dump( $content );
7594
}
7695

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

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

88107
unset( $this->parentKey );
89108

90109
if ( ! $parentKey ) {
91-
$this->content[ $index ?? $key ] = $value;
110+
$this->content[ $key ] = $value;
92111

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

129+
$this->maybeAddUseOf( $fqcn );
130+
110131
return $this->addContent( $key, array( $fqcn, $methodName ) );
111132
}
112133

113134
/** @return mixed[] */
114135
public function getContent(): array {
115136
return $this->content;
116137
}
138+
139+
protected function getAliasOf( string $import ): string {
140+
if ( ! in_array( $import, $uses = $this->getNamespace()->getUses(), strict: true ) ) {
141+
return $import;
142+
}
143+
144+
return ( $alias = array_search( $import, $uses, strict: true ) ) ? "{$alias}::class" : $import;
145+
}
146+
147+
protected function resolveImports( string $content ): string {
148+
return self::isSubscribedForImport() ? $this->getAliasOf( $content ) : $content;
149+
}
150+
151+
private function maybeAddUseOf( string $key ): void {
152+
self::isSubscribedForImport()
153+
&& Helpers::isNamespaceIdentifier( $key )
154+
&& $this->addUseStatementOf( $key );
155+
}
117156
}

Tests/ArrayPhpFileTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
use TheWebSolver\Codegarage\Generator\Enum\Argument;
1010

1111
class ArrayPhpFileTest extends TestCase {
12+
#[Test]
13+
public function itEnsuresImportSubscriptionWorks(): void {
14+
$this->assertTrue( ArrayPhpFile::isSubscribedForImport() );
15+
16+
$unsubscribe = ArrayPhpFile::subscribeForImport();
17+
18+
$this->assertTrue( ArrayPhpFile::isSubscribedForImport() );
19+
$unsubscribe();
20+
$this->assertTrue(
21+
ArrayPhpFile::isSubscribedForImport(),
22+
'Default is set to true, so reset (unsubscribe) has no effect.'
23+
);
24+
25+
$unsubscribe = ArrayPhpFile::subscribeForImport( false );
26+
27+
$this->assertFalse( ArrayPhpFile::isSubscribedForImport() );
28+
$unsubscribe();
29+
$this->assertTrue(
30+
ArrayPhpFile::isSubscribedForImport(),
31+
'Resets to previous state before subscription.'
32+
);
33+
}
34+
1235
#[Test]
1336
public function classNameKeyAndCallableMethodAreImportedProperly(): void {
1437
$this->assertTrue( true );

0 commit comments

Comments
 (0)