Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #176

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/ClassAliasAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class ClassAliasAutoloader
* @param array $excludedAliases
* @return static
*/
public static function register(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [])
public static function register(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [], array $classAliases = [])
{
return tap(new static($shell, $classMapPath, $includedAliases, $excludedAliases), function ($loader) {
return tap(new static($shell, $classMapPath, $includedAliases, $excludedAliases, $classAliases), function ($loader) {
spl_autoload_register([$loader, 'aliasClass']);
});
}
Expand All @@ -67,7 +67,7 @@ public static function register(Shell $shell, $classMapPath, array $includedAlia
* @param array $excludedAliases
* @return void
*/
public function __construct(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [])
public function __construct(Shell $shell, $classMapPath, array $includedAliases = [], array $excludedAliases = [], array $classAliases = [])
{
$this->shell = $shell;
$this->vendorPath = dirname(dirname($classMapPath));
Expand All @@ -87,6 +87,13 @@ public function __construct(Shell $shell, $classMapPath, array $includedAliases
$this->classes[$name] = $class;
}
}

foreach ($classAliases as $alias => $class) {
if (! isset($classes[$class])) {
continue;
}
$this->classes[$alias] = $class;
}
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Console/TinkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public function handle()
$config = $this->getLaravel()->make('config');

$loader = ClassAliasAutoloader::register(
$shell, $path, $config->get('tinker.alias', []), $config->get('tinker.dont_alias', [])
$shell,
$path,
$config->get('tinker.alias', []),
$config->get('tinker.dont_alias', []),
$config->get('tinker.class_alias', [])
);

if ($code = $this->option('execute')) {
Expand Down
18 changes: 18 additions & 0 deletions tests/ClassAliasAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,22 @@ public function testVendorClassesCanBeWhitelisted()
$this->assertTrue(class_exists('Three'));
$this->assertInstanceOf(\One\Two\Three::class, new \Three);
}

public function testCanAliasClassesToAnotherName()
{
$this->loader = ClassAliasAutoloader::register(
$shell = Mockery::mock(Shell::class),
$this->classmapPath,
[],
[],
['Four' => 'One\Two\Three']
);

$shell->shouldReceive('writeStdout')
->with("[!] Aliasing 'Four' to 'One\Two\Three' for this Tinker session.\n")
->once();

$this->assertTrue(class_exists('Four'));
$this->assertInstanceOf(\One\Two\Three::class, new \Four);
}
}