测试entity filter

This commit is contained in:
Edward Yang 2022-07-20 16:06:46 +08:00
parent 350765e160
commit 59854c52b1
6 changed files with 123 additions and 1 deletions

View File

@ -65,6 +65,43 @@ class Manager
return $this->plugins;
}
/**
* 获取已开启的插件
*
* @return Collection
* @throws \Exception
*/
public function getEnabledPlugins(): Collection
{
$allPlugins = $this->getPlugins();
return $allPlugins->filter(function (Plugin $plugin) {
return $plugin->getEnabled();
});
}
/**
* 获取已开启插件对应根目录下的启动文件 bootstrap.php
*
* @return Collection
* @throws \Exception
*/
public function getEnabledBootstraps(): Collection
{
$bootstraps = new Collection;
foreach ($this->getEnabledPlugins() as $plugin) {
if ($this->filesystem->exists($file = $plugin->getPath() . '/bootstrap.php')) {
$bootstraps->push([
'code' => $plugin->getDirName(),
'file' => $file
]);
}
}
return $bootstraps;
}
/**
* 获取单个插件
*

View File

@ -92,6 +92,11 @@ class Plugin implements Arrayable, \ArrayAccess
}
public function getDirname(): string
{
return $this->dirName;
}
public function getPath(): string
{
return $this->path;

View File

@ -0,0 +1,52 @@
<?php
/**
* PluginServiceProvider.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-20 14:42:10
* @modified 2022-07-20 14:42:10
*/
namespace Beike\Shop\Providers;
use Beike\Plugin\Manager;
use Illuminate\Support\ServiceProvider;
class PluginServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('plugin', function () {
return new Manager();
});
}
/**
* Bootstrap Plugin Service Provider
*
* @param Manager $manager
* @throws \Exception
*/
public function boot(Manager $manager)
{
$plugins = $manager->getPlugins();
$bootstraps = $manager->getEnabledBootstraps();
foreach ($bootstraps as $bootstrap) {
$filePath = $bootstrap['file'];
$pluginCode = $bootstrap['code'];
if (file_exists($filePath)) {
require_once $filePath;
$className = "Plugin\\{$pluginCode}\\Bootstrap";
(new $className)->boot();
}
}
}
}

View File

@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use Beike\Shop\View\Components\AccountSidebar;
use Illuminate\View\FileViewFinder;
use TorMorten\Eventy\Facades\Eventy;
class ShopServiceProvider extends ServiceProvider
{
@ -69,7 +70,7 @@ class ShopServiceProvider extends ServiceProvider
View::share('languages', languages());
$menuCategories = CategoryRepo::getTwoLevelCategories();
View::share('categories', $menuCategories);
View::share('categories', Eventy::filter('header.categories', $menuCategories));
View::share('shop_base_url', shop_route('home.index'));
}

View File

@ -177,6 +177,7 @@ return [
Beike\Admin\Providers\AdminServiceProvider::class,
Beike\Shop\Providers\ShopServiceProvider::class,
Beike\Shop\Providers\PluginServiceProvider::class,
],

View File

@ -0,0 +1,26 @@
<?php
/**
* bootstrap.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-20 15:35:59
* @modified 2022-07-20 15:35:59
*/
namespace Plugin\BKStripe;
use TorMorten\Eventy\Facades\Eventy;
class Bootstrap
{
public function boot()
{
dump(__CLASS__, __METHOD__, __FUNCTION__);
Eventy::addFilter('header.categories', function($data) {
dump($data);
return $data;
}, 20, 1);
}
}