add namespace hints for plugin views and translation

This commit is contained in:
printempw 2016-10-27 22:52:26 +08:00
parent e6fedfd2f3
commit 2559f7907b
5 changed files with 37 additions and 37 deletions

View File

@ -15,34 +15,28 @@ class PluginServiceProvider extends ServiceProvider
*/
public function boot(PluginManager $plugins)
{
$namespaces = [];
// 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');
foreach ($plugins->getPlugins() as $plugin) {
$namespaces[$plugin->getNameSpace()] = $plugin->getPath()."/src";
$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");
}
// register class autoloader for plugins
spl_autoload_register(function($class) use ($namespaces) {
// traverse in registered plugin namespaces
foreach ((array) array_keys($namespaces) as $namespace) {
if ($namespace != '' && mb_strpos($class, $namespace) === 0) {
// parse real file path
$path = $namespaces[$namespace].Str::replaceFirst($namespace, '', $class).".php";
$path = str_replace('\\', '/', $path);
if (file_exists($path)) {
include $path;
}
}
}
});
$this->registerClassAutoloader($src_paths);
$bootstrappers = $plugins->getEnabledBootstrappers();
foreach ($bootstrappers as $file) {
// bootstraper is a closure
$bootstrapper = require $file;
// call closure using service container
$this->app->call($bootstrapper);
}
}
@ -56,4 +50,28 @@ class PluginServiceProvider extends ServiceProvider
{
$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;
}
}
}
});
}
}

View File

@ -15,7 +15,6 @@ dashboard: Dashboard
my-closet: Closet
player-manage: Player Manage
user-manage: User Manage
generate-config: Generate Config
plugin-manage: Plugin Manage
customize: Customize
options: Options

View File

@ -144,11 +144,3 @@ profile:
wrong-password: Wrong password.
success: Your account is deleted successfully.
config:
mod-requirement: MOD Requirement
mod-intro: |
<p>We support all versions of <a href="http://www.mcbbs.net/forum.php?mod=viewthread&tid=358932" target="_blank">UniSkinMod</a> and <a href="http://www.mcbbs.net/thread-269807-1-1.html" target="_blank">CustomSkinLoader</a>, and any other skin mod using with leagcy links.</p>
<p>Detailed Documentation Here: <a href="https://github.com/printempw/blessing-skin-server/wiki/Mod-Configuration" target="_blank">@GitHub</a></p>
version: Version
config-file: Config File

View File

@ -15,7 +15,6 @@ dashboard: 仪表盘
my-closet: 我的衣柜
player-manage: 角色管理
user-manage: 用户管理
generate-config: 配置生成
plugin-manage: 插件管理
customize: 个性化
options: 站点配置

View File

@ -151,11 +151,3 @@ profile:
wrong-password: 密码错误
success: 账号已被成功删除
config:
mod-requirement: MOD 需求
mod-intro: |
<p>本站支持的皮肤 MOD 有 <a href="http://www.mcbbs.net/forum.php?mod=viewthread&tid=358932" target="_blank">UniSkinMod</a><a href="http://www.mcbbs.net/thread-269807-1-1.html" target="_blank">CustomSkinLoader</a> 各自的新版和旧版,以及任何支持传统皮肤加载链接的皮肤 MOD。</p>
<p>详细教程:<a href="https://github.com/printempw/blessing-skin-server/wiki/Mod-Configuration" target="_blank">@GitHub</a></p>
version: 版本
config-file: 配置文件