blessing-skin-server/app/Providers/PluginServiceProvider.php

78 lines
2.3 KiB
PHP
Raw Normal View History

2016-10-17 12:20:55 +08:00
<?php
namespace App\Providers;
2016-10-24 22:32:07 +08:00
use Illuminate\Support\Str;
use App\Services\PluginManager;
2016-10-17 12:20:55 +08:00
use Illuminate\Support\ServiceProvider;
class PluginServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
2016-10-24 22:32:07 +08:00
public function boot(PluginManager $plugins)
2016-10-17 12:20:55 +08:00
{
// store paths of class files of plugins
$src_paths = [];
$loader = $this->app->make('translation.loader');
// make view instead of view.finder since the finder is defined as not a singleton
$finder = $this->app->make('view');
2016-10-17 12:20:55 +08:00
foreach ($plugins->getEnabledPlugins() as $plugin) {
$src_paths[$plugin->getNameSpace()] = $plugin->getPath()."/src";
// add paths of translation files for namespace hints
$loader->addNamespace($plugin->getNameSpace(), $plugin->getPath()."/lang");
// add paths of views
$finder->addNamespace($plugin->getNameSpace(), $plugin->getPath()."/views");
2016-10-24 22:32:07 +08:00
}
$this->registerClassAutoloader($src_paths);
2016-10-24 22:32:07 +08:00
$bootstrappers = $plugins->getEnabledBootstrappers();
2016-10-17 12:20:55 +08:00
foreach ($bootstrappers as $file) {
$bootstrapper = require $file;
// call closure using service container
2016-10-17 12:20:55 +08:00
$this->app->call($bootstrapper);
}
}
2016-10-24 22:32:07 +08:00
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('plugins', PluginManager::class);
}
/**
* 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;
}
}
}
});
}
2016-10-17 12:20:55 +08:00
}