This commit is contained in:
Edward Yang 2022-06-29 20:44:06 +08:00
parent 3aeacf0bf6
commit 09bded4a32
7 changed files with 220 additions and 35 deletions

View File

@ -11,10 +11,17 @@
namespace Beike\Admin\Http\Controllers;
use Beike\Plugin\Manager;
class PluginController extends Controller
{
/**
* @throws \Exception
*/
public function index()
{
$data = (new Manager)->getPlugins();
dd($data);
return view('admin::pages.plugins.index', []);
}
}

92
beike/Plugin/Manager.php Normal file
View File

@ -0,0 +1,92 @@
<?php
/**
* Manager.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-06-29 19:38:30
* @modified 2022-06-29 19:38:30
*/
namespace Beike\Plugin;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
class Manager
{
protected $plugins;
protected Filesystem $filesystem;
public function __construct()
{
$this->filesystem = new Filesystem();
}
/**
* 获取所有插件
*
* @return Collection
* @throws \Exception
*/
public function getPlugins(): Collection
{
if ($this->plugins) {
return $this->plugins;
}
$existed = $this->getPluginsConfig();
$plugins = new Collection();
foreach ($existed as $dirname => $package) {
$pluginPath = $this->getPluginsDir() . DIRECTORY_SEPARATOR . $dirname;
$plugin = new Plugin($pluginPath, $package);
$plugin->setDirname($dirname);
$plugin->setInstalled(true);
$plugin->setVersion(Arr::get($package, 'version'));
if ($plugins->has($plugin->code)) {
throw new \Exception("有重名插件:" . $plugin->code);
}
$plugins->put($plugin->code, $plugin);
}
$this->plugins = $plugins->sortBy(function ($plugin) {
return $plugin->name;
});
return $this->plugins;
}
protected function getPluginsConfig(): array
{
$installed = [];
$resource = opendir($this->getPluginsDir());
while ($filename = @readdir($resource)) {
if ($filename == '.' || $filename == '..') {
continue;
}
$path = $this->getPluginsDir() . DIRECTORY_SEPARATOR . $filename;
if (is_dir($path)) {
$packageJsonPath = $path . DIRECTORY_SEPARATOR . 'config.json';
if (file_exists($packageJsonPath)) {
$installed[$filename] = json_decode($this->filesystem->get($packageJsonPath), true);
}
}
}
closedir($resource);
return $installed;
}
/**
* 插件根目录
*
* @return string
*/
protected function getPluginsDir(): string
{
return config('plugins.directory') ?: base_path('plugins');
}
}

109
beike/Plugin/Plugin.php Normal file
View File

@ -0,0 +1,109 @@
<?php
/**
* Plugin.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-06-29 20:27:21
* @modified 2022-06-29 20:27:21
*/
namespace Beike\Plugin;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class Plugin implements Arrayable, \ArrayAccess
{
protected $path;
protected $name;
protected $packageInfo;
protected $dirName;
protected $installed;
protected $enabled;
protected $version;
public function __construct(string $path, array $packageInfo)
{
$this->path = $path;
$this->packageInfo = $packageInfo;
}
public function __get($name)
{
return $this->packageInfoAttribute(Str::snake($name, '-'));
}
public function __isset($name)
{
return isset($this->{$name}) || $this->packageInfoAttribute(Str::snake($name, '-'));
}
public function packageInfoAttribute($name)
{
return Arr::get($this->packageInfo, $name);
}
public function setDirname(string $dirName): Plugin
{
$this->dirName = $dirName;
return $this;
}
public function setInstalled(bool $installed): Plugin
{
$this->installed = $installed;
return $this;
}
public function setEnabled(bool $enabled): Plugin
{
$this->enabled = $enabled;
return $this;
}
public function setVersion(string $version): Plugin
{
$this->version = $version;
return $this;
}
public function getVersion(): string
{
return $this->version;
}
public function toArray(): array
{
return (array)array_merge([
'name' => $this->name,
'version' => $this->getVersion(),
'path' => $this->path
], $this->packageInfo);
}
public function offsetExists($key): bool
{
return Arr::has($this->packageInfo, $key);
}
public function offsetGet($key)
{
return $this->packageInfoAttribute($key);
}
public function offsetSet($key, $value)
{
return Arr::set($this->packageInfo, $key, $value);
}
public function offsetUnset($key)
{
unset($this->packageInfo[$key]);
}
}

View File

@ -0,0 +1,12 @@
{
"code": "bk_stripe",
"name": "Stripe 支付",
"description": "Stripe 支付 <a href='https://stripe.com/'>Stripe</a>",
"type": "payment",
"version": "v1.0.0",
"icon": "https://via.placeholder.com/30x30.png/00ee99?text=a",
"author": {
"name": "成都光大网络科技有限公司",
"email": "yangjin@opencart.cn"
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Plugin\Guangda\Alipay;
class Alipay
{
public function handle(): string
{
return 'alipay';
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace Plugin\Guangda\Seller\Models;
class Product extends \App\Models\Product
{
public function getFillable(): array
{
$fillable = $this->fillable;
$fillable[] = 'seller_id';
return $fillable;
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Plugin\Guangda\WeChat;
class WeChat
{
public function handle(): string
{
return 'wechat';
}
}