Skip to content

Commit c6f5105

Browse files
committed
refactor: extract methods
Extracted two methods: - createPrependAutoloader - createAppendAutoloader Additionally, changed `$loaded` to be an `ArrayObject` instance, which allows removal of the `static` declaration, as well as the need to pass by reference (since objects always are). These changes also allows removing intermediary variable representations within `load()`, making it more clear what is being passed to each autoloader.
1 parent 3e79df9 commit c6f5105

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

src/Autoloader.php

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Laminas\ZendFrameworkBridge;
99

10+
use ArrayObject;
1011
use Composer\Autoload\ClassLoader;
1112
use RuntimeException;
1213

@@ -32,12 +33,47 @@ class Autoloader
3233
*/
3334
public static function load()
3435
{
35-
static $loaded = array();
36+
$loaded = new ArrayObject(array());
37+
38+
spl_autoload_register(self::createPrependAutoloader(
39+
RewriteRules::namespaceReverse(),
40+
self::getClassLoader(),
41+
$loaded
42+
), true, true);
43+
44+
spl_autoload_register(self::createAppendAutoloader(
45+
RewriteRules::namespaceRewrite(),
46+
$loaded
47+
));
48+
}
3649

37-
$classLoader = self::getClassLoader();
50+
/**
51+
* @return ClassLoader
52+
* @throws RuntimeException
53+
*/
54+
private static function getClassLoader()
55+
{
56+
if (file_exists(__DIR__ . '/../../../autoload.php')) {
57+
return include __DIR__ . '/../../../autoload.php';
58+
}
3859

39-
$namespaces = RewriteRules::namespaceReverse();
40-
spl_autoload_register(function ($class) use ($namespaces, $classLoader, &$loaded) {
60+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
61+
return include __DIR__ . '/../vendor/autoload.php';
62+
}
63+
64+
throw new RuntimeException('Cannot detect composer autoload. Please run composer install');
65+
}
66+
67+
/**
68+
* @return callable
69+
*/
70+
private static function createPrependAutoloader(array $namespaces, ClassLoader $classLoader, ArrayObject $loaded)
71+
{
72+
/**
73+
* @param string $class Class name to autoload
74+
* @return void
75+
*/
76+
return function ($class) use ($namespaces, $classLoader, $loaded) {
4177
if (isset($loaded[$class])) {
4278
return;
4379
}
@@ -60,10 +96,19 @@ public static function load()
6096
$legacy = $namespaces[$check] . str_replace('Laminas', 'Zend', substr($class, strlen($check)));
6197
class_alias($class, $legacy);
6298
}
63-
}, true, true);
99+
};
100+
}
64101

65-
$namespaces = RewriteRules::namespaceRewrite();
66-
spl_autoload_register(function ($class) use ($namespaces, &$loaded) {
102+
/**
103+
* @return callable
104+
*/
105+
private static function createAppendAutoloader(array $namespaces, ArrayObject $loaded)
106+
{
107+
/**
108+
* @param string $class Class name to autoload
109+
* @return void
110+
*/
111+
return function ($class) use ($namespaces, $loaded) {
67112
$segments = explode('\\', $class);
68113

69114
$i = 0;
@@ -85,23 +130,6 @@ class_alias($class, $legacy);
85130
if (class_exists($alias) || interface_exists($alias) || trait_exists($alias)) {
86131
class_alias($alias, $class);
87132
}
88-
});
89-
}
90-
91-
/**
92-
* @return ClassLoader
93-
* @throws RuntimeException
94-
*/
95-
private static function getClassLoader()
96-
{
97-
if (file_exists(__DIR__ . '/../../../autoload.php')) {
98-
return include __DIR__ . '/../../../autoload.php';
99-
}
100-
101-
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
102-
return include __DIR__ . '/../vendor/autoload.php';
103-
}
104-
105-
throw new RuntimeException('Cannot detect composer autoload. Please run composer install');
133+
};
106134
}
107135
}

0 commit comments

Comments
 (0)