| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace app\common\services;
- use app\common\events;
- use app\common\helpers\Cache;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- use Illuminate\Filesystem\Filesystem;
- use Illuminate\Contracts\Events\Dispatcher;
- use app\common\repositories\OptionRepository;
- use Illuminate\Contracts\Foundation\Application;
- use Illuminate\Support\Facades\DB;
- class PluginManager
- {
- /**
- * @var Application
- */
- protected $app;
- /**
- * @var OptionRepository
- */
- protected $option;
- /**
- * @var Dispatcher
- */
- protected $dispatcher;
- /**
- * @var Filesystem
- */
- protected $filesystem;
- /**
- * @var Collection|null
- */
- protected $plugins;
- public function __construct(
- Application $app,
- OptionRepository $option,
- Dispatcher $dispatcher,
- Filesystem $filesystem
- )
- {
- $this->app = $app;
- $this->option = $option;
- $this->dispatcher = $dispatcher;
- $this->filesystem = $filesystem;
- }
- /**
- * @return Collection
- */
- public function getPlugins()
- {
- if (is_null($this->plugins)) {
- $this->plugins = Cache::get('plugins_list');
- if (!is_null($this->plugins)) {
- return $this->plugins;
- }
- $plugins = new Collection();
- $pluginDirs = $this->filesystem->directories(base_path('plugins'));
- if (\YunShop::app()->uniacid || \Setting::$uniqueAccountId) {
- foreach ($pluginDirs as $pluginDir) {
- if (file_exists($pluginDir . "/package.json")) {
- // Instantiates an Plugin object using the package path and package.json file.
- $plugin = new Plugin($pluginDir);
- // Per default all plugins are installed if they are registered in composer.
- $plugin->setInstalled(true);
- $plugin->setEnabled($this->isOptionEnable($plugin->name));
- $plugins->put($plugin->name, $plugin);
- }
- }
- }else {
- foreach ($pluginDirs as $pluginDir) {
- if (file_exists($pluginDir . "/package.json")) {
- $plugin = new Plugin($pluginDir);
- $plugins->put($plugin->name, $plugin);
- }
- }
- }
- $this->plugins = $plugins->sortBy(function ($plugin, $name) {
- return $plugin->name;
- });
- Cache::put('plugins_list',$this->plugins,5);
- }
- return $this->plugins;
- }
- /**
- * Loads an Plugin with all information.
- *
- * @param string $name
- * @return Plugin|null
- */
- public function getPlugin($name)
- {
- return $this->getPlugins()->get($name);
- }
- public function getEnablePlugin($name)
- {
- return $this->getEnabledPlugins()->get($name);
- }
- public function getPluginId($name)
- {
- $pluginIdConfig = array_first(\app\common\modules\shop\ShopConfig::current()->get('plugin'), function ($item) use ($name) {
- return $item['name'] == $name;
- }, []);
- return $pluginIdConfig['id'];
- }
- public function findPlugin($id)
- {
- return $this->getPlugins()->first(function (Plugin $plugin) use ($id) {
- if ('' === $plugin->getId()) {
- return false;
- }
- return $plugin->getId() == $id;
- });
- }
- /**
- * Enables the plugin.
- *
- * @param string $name
- */
- public function enable($name)
- {
- if (!$this->isOptionEnable($name)) {
- DB::transaction(function () use ($name) {
- $plugin = $this->getPlugin($name);
- $enabled = $this->getEnabled();
- // $enabled[] = $name;
- $this->setEnabled($enabled[$name]['id'], 1, $name);
- $plugin->app()->init();
- $this->dispatcher->dispatch(new events\PluginWasEnabled($plugin));
- });
- }
- }
- /**
- * Disables an plugin.
- *
- * @param string $name
- */
- public function disable($name)
- {
- $enabled = $this->getEnabled();
- $plugin = $this->getPlugin($name);
- $this->option->editDisable($enabled[$name]['id']);
- $this->dispatcher->dispatch(new events\PluginWasEnabled($plugin));
- }
- /**
- * 启动插件套餐.
- *
- * @param string $name
- */
- public function enableMeal($name)
- {
- \Log::error("sss5".$name);
- DB::transaction(function () use ($name) {
- $plugin = $this->getPlugin($name);
- $enabled = $this->getEnabled();
- $this->setEnabled($enabled[$name]['id'], 1, $name);
- $plugin->app()->init();
- $this->dispatcher->dispatch(new events\PluginWasEnabled($plugin));
- });
- }
- public function disableMeal($uniacid)
- {
- DB::table('yz_options')->where('uniacid', $uniacid)->delete();
- \app\common\modules\option\OptionRepository::flush();
- app('supervisor')->restart();
- }
- //发放关闭所有套餐后的事件
- public function dispatchEvent($name)
- {
- $plugin = $this->getPlugin($name);
- $this->dispatcher->dispatch(new events\PluginWasEnabled($plugin));
- }
- /**
- * Uninstalls an plugin.
- *
- * @param string $name
- */
- public function uninstall($name)
- {
- $plugin = $this->getPlugin($name);
- $this->disable($name);
- // fire event before deleeting plugin files
- // $this->dispatcher->fire(new events\PluginWasDeleted($plugin));
- $find = storage_path('/logs/plugin_uninstall.log');
- if(!file_exists($find)){
- fopen($find,'a');
- }
- file_put_contents($find, date('Y-m-d H:i:s').'操作员:'. \Auth::guard('admin')->user()->uid.'卸载了插件'.$name.PHP_EOL, FILE_APPEND);
- $this->filesystem->deleteDirectory($plugin->getPath());
- // refresh plugin list
- $this->plugins = null;
- }
- /**
- * Get only enabled plugins.
- *
- * @return Collection
- */
- public function getEnabledPlugins($uniacid = null)
- {
- $only = [];
- foreach ($this->getEnabled($uniacid) as $key => $plugin) {
- if ($plugin['enabled']) {
- $only[] = $key;
- }
- }
- return $this->getPlugins()->only($only);
- }
- public function getEnableApp($name)
- {
- return $this->getEnablePlugin($name)->app();
- }
- /**
- * The id's of the enabled plugins.
- *
- * @return array
- */
- public function getEnabled($uniacid = null)
- {
- if ($uniacid == '*') {
- return (array)$this->option->all();
- }
- \YunShop::app()->uniacid = \YunShop::app()->uniacid ?:\Setting::$uniqueAccountId;
- $uniacid = $uniacid ?: \YunShop::app()->uniacid;
- return (array)$this->option->uniacid($uniacid);
- }
- /**
- * Persist the currently enabled plugins.
- *
- * @param array $enabled
- */
- protected function setEnabled($id, $enabled, $name = null)
- {
- $pluginData = [
- 'uniacid' => \YunShop::app()->uniacid,
- 'option_name' => $name,
- 'option_value' => 'true',
- 'enabled' => $enabled,
- ];
- return $this->option->insertPlugin($pluginData);
- }
- /**
- * Whether the plugin is enabled.
- *
- * @param $plugin
- * @return bool
- */
- public function isEnabled($pluginName)
- {
- $plugin = $this->getPlugin($pluginName);
- if (!$plugin) {
- return false;
- }
- return $this->isOptionEnable($pluginName);
- }
- private function isOptionEnable($plugin)
- {
- $plugins = $this->getEnabled();
- return $plugins[$plugin]['enabled'] ?: false;
- // return in_array($plugin, $this->getEnabled());
- }
- /**
- * The plugins path.
- *
- * @return string
- */
- protected function getPluginsDir()
- {
- return $this->app->basePath() . '/plugins';
- }
- public function enTopShow($name, $enable)
- {
- if (!$this->getPlugin($name)) {
- $name = str_replace("_", "-", $name);
- }
- $enabled = $this->getEnabled();
- $this->setTopShow($enabled[$name]['id'], $enable);
- }
- public function setTopShow($id, $enabled, $name = null)
- {
- if ($id) {
- return $this->option->editTopShowById($id, $enabled);
- } else {
- $pluginData = [
- 'uniacid' => \YunShop::app()->uniacid,
- 'option_name' => $name,
- 'option_value' => 'true',
- 'top_show' => $enabled
- ];
- return $this->option->insertPlugin($pluginData);
- }
- }
- public function isTopShow($name)
- {
- $plugins = (array)$this->option->uniacid(\YunShop::app()->uniacid);
- if (!$this->getPlugin($name)) {
- $name = str_replace("_", "-", $name);
- }
- return $plugins = $plugins[$name]['top_show'];
- }
- }
|