Skip to content

Instantly share code, notes, and snippets.

@prinsss
Created November 1, 2016 06:40
Show Gist options
  • Select an option

  • Save prinsss/d7eb791b346e0521f838e269d8e87bd3 to your computer and use it in GitHub Desktop.

Select an option

Save prinsss/d7eb791b346e0521f838e269d8e87bd3 to your computer and use it in GitHub Desktop.
<?php
public function boot(PluginManager $plugins)
{
// store paths of class files of plugins
$src_paths = [];
foreach ($plugins->getPlugins() as $plugin) {
$src_paths[$plugin->getNameSpace()] = $plugin->getPath()."/src";
}
$this->registerClassAutoloader($src_paths);
// call bootstrappers here
}
/**
* Register class autoloader for plugins.
*
* @return void
*/
protected function registerClassAutoloader($paths)
{
spl_autoload_register(function ($class) use ($paths) {
// traverse in registered plugin paths
foreach ((array) array_keys($paths) as $namespace) {
if ($namespace != '' && mb_strpos($class, $namespace) === 0) {
// parse real file path
$path = $paths[$namespace].Str::replaceFirst($namespace, '', $class).".php";
$path = str_replace('\\', '/', $path);
if (file_exists($path)) {
// include class file if it exists
include $path;
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment