Skip to content

Commit 5f99753

Browse files
committed
feat: add class_alias to add alias to original class name
1 parent 28aae65 commit 5f99753

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/ClassAliasAutoloader.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ class ClassAliasAutoloader
5151
* @param array $excludedAliases
5252
* @return static
5353
*/
54-
public static function register(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [])
54+
public static function register(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [], array $classAliases = [])
5555
{
56-
return tap(new static($shell, $classMapPath, $includedAliases, $excludedAliases), function ($loader) {
56+
return tap(new static($shell, $classMapPath, $includedAliases, $excludedAliases, $classAliases), function ($loader) {
5757
spl_autoload_register([$loader, 'aliasClass']);
5858
});
5959
}
@@ -67,7 +67,7 @@ public static function register(Shell $shell, $classMapPath, array $includedAlia
6767
* @param array $excludedAliases
6868
* @return void
6969
*/
70-
public function __construct(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [])
70+
public function __construct(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [], array $classAliases = [])
7171
{
7272
$this->shell = $shell;
7373
$this->vendorPath = dirname(dirname($classMapPath));
@@ -87,6 +87,13 @@ public function __construct(Shell $shell, $classMapPath, array $includedAliases
8787
$this->classes[$name] = $class;
8888
}
8989
}
90+
91+
foreach ($classAliases as $alias => $class) {
92+
if (!isset($classes[$class])) {
93+
continue;
94+
}
95+
$this->classes[$alias] = $class;
96+
}
9097
}
9198

9299
/**

src/Console/TinkerCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public function handle()
6868
$config = $this->getLaravel()->make('config');
6969

7070
$loader = ClassAliasAutoloader::register(
71-
$shell, $path, $config->get('tinker.alias', []), $config->get('tinker.dont_alias', [])
71+
$shell,
72+
$path,
73+
$config->get('tinker.alias', []),
74+
$config->get('tinker.dont_alias', []),
75+
classAliases: $config->get('tinker.class_alias', [])
7276
);
7377

7478
if ($code = $this->option('execute')) {

tests/ClassAliasAutoloaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,20 @@ public function testVendorClassesCanBeWhitelisted()
7878
$this->assertTrue(class_exists('Three'));
7979
$this->assertInstanceOf(\One\Two\Three::class, new \Three);
8080
}
81+
82+
public function testCanAliasClassesToAnotherName()
83+
{
84+
$this->loader = ClassAliasAutoloader::register(
85+
$shell = Mockery::mock(Shell::class),
86+
$this->classmapPath,
87+
classAliases: ['Four' => 'One\Two\Three']
88+
);
89+
90+
$shell->shouldReceive('writeStdout')
91+
->with("[!] Aliasing 'Four' to 'One\Two\Three' for this Tinker session.\n")
92+
->once();
93+
94+
$this->assertTrue(class_exists('Four'));
95+
$this->assertInstanceOf(\One\Two\Three::class, new \Four);
96+
}
8197
}

0 commit comments

Comments
 (0)