@@ -21,16 +21,39 @@ class ClassAliasAutoloader
2121 */
2222 protected $ classes = [];
2323
24+ /**
25+ * Path to the vendor directory.
26+ *
27+ * @var string
28+ */
29+ protected $ vendorPath ;
30+
31+ /**
32+ * Explicitly included namespaces/classes.
33+ *
34+ * @var \Illuminate\Support\Collection
35+ */
36+ protected $ includedAliases ;
37+
38+ /**
39+ * Excluded namespaces/classes.
40+ *
41+ * @var \Illuminate\Support\Collection
42+ */
43+ protected $ excludedAliases ;
44+
2445 /**
2546 * Register a new alias loader instance.
2647 *
2748 * @param \Psy\Shell $shell
2849 * @param string $classMapPath
50+ * @param array $includedAliases
51+ * @param array $excludedAliases
2952 * @return static
3053 */
31- public static function register (Shell $ shell , $ classMapPath )
54+ public static function register (Shell $ shell , $ classMapPath, array $ includedAliases = [], array $ excludedAliases = [] )
3255 {
33- return tap (new static ($ shell , $ classMapPath ), function ($ loader ) {
56+ return tap (new static ($ shell , $ classMapPath, $ includedAliases , $ excludedAliases ), function ($ loader ) {
3457 spl_autoload_register ([$ loader , 'aliasClass ' ]);
3558 });
3659 }
@@ -40,26 +63,21 @@ public static function register(Shell $shell, $classMapPath)
4063 *
4164 * @param \Psy\Shell $shell
4265 * @param string $classMapPath
66+ * @param array $includedAliases
67+ * @param array $excludedAliases
4368 * @return void
4469 */
45- public function __construct (Shell $ shell , $ classMapPath )
70+ public function __construct (Shell $ shell , $ classMapPath, array $ includedAliases = [], array $ excludedAliases = [] )
4671 {
4772 $ this ->shell = $ shell ;
48-
49- $ vendorPath = dirname (dirname ($ classMapPath ));
73+ $ this ->vendorPath = dirname (dirname ($ classMapPath ));
74+ $ this ->includedAliases = collect ($ includedAliases );
75+ $ this ->excludedAliases = collect ($ excludedAliases );
5076
5177 $ classes = require $ classMapPath ;
5278
53- $ excludedAliases = collect (config ('tinker.dont_alias ' , []));
54-
5579 foreach ($ classes as $ class => $ path ) {
56- if (! Str::contains ($ class , '\\' ) || Str::startsWith ($ path , $ vendorPath )) {
57- continue ;
58- }
59-
60- if (! $ excludedAliases ->filter (function ($ alias ) use ($ class ) {
61- return Str::startsWith ($ class , $ alias );
62- })->isEmpty ()) {
80+ if (! $ this ->isAliasable ($ class , $ path )) {
6381 continue ;
6482 }
6583
@@ -83,9 +101,7 @@ public function aliasClass($class)
83101 return ;
84102 }
85103
86- $ fullName = isset ($ this ->classes [$ class ])
87- ? $ this ->classes [$ class ]
88- : false ;
104+ $ fullName = $ this ->classes [$ class ] ?? false ;
89105
90106 if ($ fullName ) {
91107 $ this ->shell ->writeStdout ("[!] Aliasing ' {$ class }' to ' {$ fullName }' for this Tinker session. \n" );
@@ -113,4 +129,35 @@ public function __destruct()
113129 {
114130 $ this ->unregister ();
115131 }
132+
133+ /**
134+ * Whether a class may be aliased.
135+ *
136+ * @param string $class
137+ * @param string $path
138+ */
139+ public function isAliasable ($ class , $ path )
140+ {
141+ if (! Str::contains ($ class , '\\' )) {
142+ return false ;
143+ }
144+
145+ if (! $ this ->includedAliases ->filter (function ($ alias ) use ($ class ) {
146+ return Str::startsWith ($ class , $ alias );
147+ })->isEmpty ()) {
148+ return true ;
149+ }
150+
151+ if (Str::startsWith ($ path , $ this ->vendorPath )) {
152+ return false ;
153+ }
154+
155+ if (! $ this ->excludedAliases ->filter (function ($ alias ) use ($ class ) {
156+ return Str::startsWith ($ class , $ alias );
157+ })->isEmpty ()) {
158+ return false ;
159+ }
160+
161+ return true ;
162+ }
116163}
0 commit comments