2016-10-17 12:20:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2017-01-14 21:30:05 +08:00
|
|
|
use Event;
|
|
|
|
use App\Events;
|
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
|
|
|
{
|
2018-02-16 17:31:04 +08:00
|
|
|
// Store paths of class files of plugins
|
2016-10-27 22:52:26 +08:00
|
|
|
$src_paths = [];
|
|
|
|
|
|
|
|
$loader = $this->app->make('translation.loader');
|
2018-02-16 17:31:04 +08:00
|
|
|
// Make view instead of view.finder since the finder is defined as not a singleton
|
2016-10-27 22:52:26 +08:00
|
|
|
$finder = $this->app->make('view');
|
2016-10-17 12:20:55 +08:00
|
|
|
|
2017-01-02 10:39:50 +08:00
|
|
|
foreach ($plugins->getPlugins() as $plugin) {
|
|
|
|
if ($plugin->isEnabled()) {
|
|
|
|
$src_paths[$plugin->getNameSpace()] = $plugin->getPath()."/src";
|
2018-02-16 17:31:04 +08:00
|
|
|
// Add paths of views
|
2017-01-02 10:39:50 +08:00
|
|
|
$finder->addNamespace($plugin->getNameSpace(), $plugin->getPath()."/views");
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// Always add paths of translation files for namespace hints
|
2016-10-27 22:52:26 +08:00
|
|
|
$loader->addNamespace($plugin->getNameSpace(), $plugin->getPath()."/lang");
|
2016-10-24 22:32:07 +08:00
|
|
|
}
|
|
|
|
|
2017-01-14 21:30:05 +08:00
|
|
|
$this->registerPluginCallbackListener();
|
2016-10-27 22:52:26 +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;
|
2018-02-16 17:31:04 +08:00
|
|
|
// Call closure using service container
|
2016-10-17 12:20:55 +08:00
|
|
|
$this->app->call($bootstrapper);
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 21:30:05 +08:00
|
|
|
|
|
|
|
protected function registerPluginCallbackListener()
|
|
|
|
{
|
2017-01-20 21:36:29 +08:00
|
|
|
Event::listen([
|
|
|
|
Events\PluginWasEnabled::class,
|
|
|
|
Events\PluginWasDeleted::class,
|
|
|
|
Events\PluginWasDisabled::class,
|
|
|
|
], function ($event) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// Call callback functions of plugin
|
2017-01-14 21:30:05 +08:00
|
|
|
if (file_exists($filename = $event->plugin->getPath()."/callbacks.php")) {
|
|
|
|
$callbacks = require $filename;
|
|
|
|
|
2017-01-20 21:36:29 +08:00
|
|
|
$callback = array_get($callbacks, get_class($event));
|
2017-01-14 21:30:05 +08:00
|
|
|
|
2017-01-20 21:36:29 +08:00
|
|
|
return $callback ? app()->call($callback, [$event->plugin]) : null;
|
2017-01-14 21:30:05 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-10-24 22:32:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
$this->app->singleton('plugins', PluginManager::class);
|
|
|
|
}
|
2016-10-27 22:52:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register class autoloader for plugins.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function registerClassAutoloader($paths)
|
|
|
|
{
|
|
|
|
spl_autoload_register(function ($class) use ($paths) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// Traverse in registered plugin paths
|
2016-10-27 22:52:26 +08:00
|
|
|
foreach ((array) array_keys($paths) as $namespace) {
|
|
|
|
if ($namespace != '' && mb_strpos($class, $namespace) === 0) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// Parse real file path
|
2016-10-27 22:52:26 +08:00
|
|
|
$path = $paths[$namespace].Str::replaceFirst($namespace, '', $class).".php";
|
|
|
|
$path = str_replace('\\', '/', $path);
|
|
|
|
|
|
|
|
if (file_exists($path)) {
|
2018-02-16 17:31:04 +08:00
|
|
|
// Include class file if it exists
|
2016-10-27 22:52:26 +08:00
|
|
|
include $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-10-17 12:20:55 +08:00
|
|
|
}
|