-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathShortClassNames.php
48 lines (37 loc) · 1.13 KB
/
ShortClassNames.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace Spatie\TinkerTools;
class ShortClassNames
{
/** @var \Illuminate\Support\Collection */
public $classes;
public static function register(string $classMapPath = null)
{
$classMapPath = $classMapPath ?? base_path('vendor/composer/autoload_classmap.php');
(new static($classMapPath))->registerAutoloader();
}
public function __construct(string $classMapPath)
{
$classFiles = include $classMapPath;
$this->classes = collect($classFiles)
->map(function (string $path, string $fqcn) {
$name = last(explode('\\', $fqcn));
return compact('fqcn', 'name');
})
->filter()
->values();
}
public function registerAutoloader()
{
spl_autoload_register([$this, 'aliasClass']);
}
public function aliasClass($findClass)
{
$class = $this->classes->first(function ($class) use ($findClass) {
return $class['name'] === $findClass;
});
if (! $class) {
return;
}
class_alias($class['fqcn'], $class['name']);
}
}