Skip to content

Commit 07833ff

Browse files
authored
Classloader improvements (#779)
See wintercms/storm#72
1 parent 189c8f2 commit 07833ff

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ServiceProvider extends ModuleServiceProvider
3535
*/
3636
public function register()
3737
{
38+
parent::register();
39+
3840
$this->registerSingletons();
3941
$this->registerPrivilegedActions();
4042

classes/PluginManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public function loadPlugin(string $namespace, string $path): ?PluginBase
150150
$className = $namespace . '\Plugin';
151151
$classPath = $path . '/Plugin.php';
152152

153+
$this->app->make(ClassLoader::class)->autoloadPackage($namespace, $path);
154+
153155
try {
154156
// Autoloader failed?
155157
if (!class_exists($className)) {

tests/bootstrap/app.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,34 @@
1717
);
1818

1919
$loader->register();
20-
$loader->addDirectories([
21-
'modules',
22-
'plugins'
23-
]);
20+
21+
/*
22+
* Manually register all module classes for autoloading
23+
*/
24+
foreach (glob($baseDir . '/modules/*', GLOB_ONLYDIR) as $modulePath) {
25+
$loader->autoloadPackage(basename($modulePath), $modulePath);
26+
}
27+
28+
/*
29+
* Manually register all plugin classes for autoloading
30+
*/
31+
$dirPath = $baseDir . '/plugins';
32+
if (is_dir($dirPath)) {
33+
$it = new RecursiveIteratorIterator(
34+
new RecursiveDirectoryIterator($dirPath, FilesystemIterator::FOLLOW_SYMLINKS)
35+
);
36+
$it->setMaxDepth(2);
37+
$it->rewind();
38+
39+
while ($it->valid()) {
40+
if (($it->getDepth() > 1) && $it->isFile() && (strtolower($it->getFilename()) === "plugin.php")) {
41+
$filePath = dirname($it->getPathname());
42+
$pluginName = basename($filePath);
43+
$vendorName = basename(dirname($filePath));
44+
45+
$loader->autoloadPackage($vendorName . '\\' . $pluginName, $filePath);
46+
}
47+
48+
$it->next();
49+
}
50+
}

0 commit comments

Comments
 (0)