Refactor booting plugin

This commit is contained in:
Pig Fang 2019-08-16 15:51:20 +08:00
parent 72aa2c39ac
commit eedcfdf957

View File

@ -132,106 +132,91 @@ class PluginManager
return; return;
} }
$enabled = $this->getEnabledPlugins(); $this->getEnabledPlugins()->each(function ($plugin) {
$this->bootPlugin($plugin);
$this->registerAutoload( });
$enabled->mapWithKeys(function ($plugin) {
return [$plugin->namespace => $plugin->getPath().'/src'];
})
);
$this->loadVendor($enabled);
$this->loadViewsAndTranslations($enabled);
$this->registerServiceProviders($enabled);
$this->loadBootstrapper($enabled);
$this->registerLifecycleHooks(); $this->registerLifecycleHooks();
$this->booted = true; $this->booted = true;
} }
/** /**
* Register classes autoloading. * Boot one plugin.
*
* @param Collection $paths
*/ */
protected function registerAutoload($paths) public function bootPlugin(Plugin $plugin)
{ {
spl_autoload_register(function ($class) use ($paths) { $this->registerAutoload($plugin);
$paths->each(function ($path, $namespace) use ($class) { $this->loadVendor($plugin);
if ($namespace != '' && mb_strpos($class, $namespace) === 0) { $this->loadViewsAndTranslations($plugin);
// Parse real file path $this->registerServiceProviders($plugin);
$path = $path.Str::replaceFirst($namespace, '', $class).'.php'; $this->loadBootstrapper($plugin);
$path = str_replace('\\', '/', $path); }
if ($this->filesystem->exists($path)) { /**
$this->filesystem->getRequire($path); * Register classes autoloading.
} */
protected function registerAutoload(Plugin $plugin)
{
spl_autoload_register(function ($class) use ($plugin) {
$namespace = $plugin->namespace;
$path = $plugin->getPath().'/src';
if ($namespace != '' && mb_strpos($class, $namespace) === 0) {
// Parse real file path
$path = $path.Str::replaceFirst($namespace, '', $class).'.php';
$path = str_replace('\\', '/', $path);
if ($this->filesystem->exists($path)) {
$this->filesystem->getRequire($path);
} }
}); }
}); });
} }
/** /**
* Load Composer dumped autoload file. * Load Composer dumped autoload file.
*
* @param Collection $enabled
*/ */
protected function loadVendor($enabled) protected function loadVendor(Plugin $plugin)
{ {
$enabled->each(function ($plugin) { $path = $plugin->getPath().'/vendor/autoload.php';
$path = $plugin->getPath().'/vendor/autoload.php'; if ($this->filesystem->exists($path)) {
if ($this->filesystem->exists($path)) { $this->filesystem->getRequire($path);
$this->filesystem->getRequire($path); }
}
});
} }
/** /**
* Load views and translations. * Load views and translations.
*
* @param Collection $enabled
*/ */
protected function loadViewsAndTranslations($enabled) protected function loadViewsAndTranslations(Plugin $plugin)
{ {
$translations = $this->app->make('translation.loader'); $translations = $this->app->make('translation.loader');
$view = $this->app->make('view'); $view = $this->app->make('view');
$enabled->each(function ($plugin) use (&$translations, &$view) { $namespace = $plugin->namespace;
$namespace = $plugin->namespace; $path = $plugin->getPath();
$path = $plugin->getPath();
$translations->addNamespace($namespace, $path.'/lang'); $translations->addNamespace($namespace, $path.'/lang');
$view->addNamespace($namespace, $path.'/views'); $view->addNamespace($namespace, $path.'/views');
});
} }
/** protected function registerServiceProviders(Plugin $plugin)
* @param Collection $enabled
*/
protected function registerServiceProviders($enabled)
{ {
$enabled->each(function ($plugin) { $providers = Arr::get($plugin->getManifest(), 'enchants.providers', []);
$providers = Arr::get($plugin->getManifest(), 'enchants.providers', []); array_walk($providers, function ($provider) use ($plugin) {
array_walk($providers, function ($provider) use ($plugin) { $class = Str::start(Str::finish($provider, 'ServiceProvider'), $plugin->namespace.'\\');
$class = Str::start(Str::finish($provider, 'ServiceProvider'), $plugin->namespace.'\\'); if (class_exists($class)) {
if (class_exists($class)) { $this->app->register($class);
$this->app->register($class); }
}
});
}); });
} }
/** /**
* Load plugin's bootstrapper. * Load plugin's bootstrapper.
*
* @param Collection $enabled
*/ */
protected function loadBootstrapper($enabled) protected function loadBootstrapper(Plugin $plugin)
{ {
$enabled->each(function ($plugin) { $path = $plugin->getPath().'/bootstrap.php';
$path = $plugin->getPath().'/bootstrap.php'; if ($this->filesystem->exists($path)) {
if ($this->filesystem->exists($path)) { $this->app->call($this->filesystem->getRequire($path));
$this->app->call($this->filesystem->getRequire($path)); }
}
});
} }
protected function registerLifecycleHooks() protected function registerLifecycleHooks()