diff --git a/beike/Admin/Http/Controllers/PluginController.php b/beike/Admin/Http/Controllers/PluginController.php index bab1432c..0eb0fdaa 100644 --- a/beike/Admin/Http/Controllers/PluginController.php +++ b/beike/Admin/Http/Controllers/PluginController.php @@ -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', []); } } diff --git a/beike/Plugin/Manager.php b/beike/Plugin/Manager.php new file mode 100644 index 00000000..57eb272d --- /dev/null +++ b/beike/Plugin/Manager.php @@ -0,0 +1,92 @@ + + * @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'); + } +} diff --git a/beike/Plugin/Plugin.php b/beike/Plugin/Plugin.php new file mode 100644 index 00000000..436598e5 --- /dev/null +++ b/beike/Plugin/Plugin.php @@ -0,0 +1,109 @@ + + * @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]); + } +} diff --git a/plugins/BKStripe/config.json b/plugins/BKStripe/config.json new file mode 100644 index 00000000..5629f123 --- /dev/null +++ b/plugins/BKStripe/config.json @@ -0,0 +1,12 @@ +{ + "code": "bk_stripe", + "name": "Stripe 支付", + "description": "Stripe 支付 Stripe", + "type": "payment", + "version": "v1.0.0", + "icon": "https://via.placeholder.com/30x30.png/00ee99?text=a", + "author": { + "name": "成都光大网络科技有限公司", + "email": "yangjin@opencart.cn" + } +} diff --git a/plugins/Guangda/Alipay/Alipay.php b/plugins/Guangda/Alipay/Alipay.php deleted file mode 100644 index 7e96a6c5..00000000 --- a/plugins/Guangda/Alipay/Alipay.php +++ /dev/null @@ -1,11 +0,0 @@ -fillable; - $fillable[] = 'seller_id'; - return $fillable; - } -} diff --git a/plugins/Guangda/WeChat/WeChat.php b/plugins/Guangda/WeChat/WeChat.php deleted file mode 100644 index 12e24849..00000000 --- a/plugins/Guangda/WeChat/WeChat.php +++ /dev/null @@ -1,11 +0,0 @@ -