| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace app\common\services;
- use ArrayAccess;
- use Illuminate\Support\Arr;
- use Illuminate\Contracts\Support\Arrayable;
- use app\common\helpers\Url;
- use Illuminate\Support\Facades\Redis;
- /**
- * @property string $name
- * @property string $description
- * @property string $title
- * @property array $author
- */
- class Plugin implements Arrayable, ArrayAccess
- {
- /**
- * @var PluginApplication
- */
- protected $pluginApp;
- /**
- * The full directory of this plugin.
- *
- * @var string
- */
- protected $path;
- /**
- * The directory name where the plugin installed.
- *
- * @var string
- */
- protected $dirname;
- /**
- * package.json of the package.
- *
- * @var array
- */
- protected $packageInfo;
- /**
- * Whether the plugin is installed.
- *
- * @var bool
- */
- protected $installed = true;
- /**
- * Whether the plugin is enabled.
- *
- * @var bool
- */
- protected $enabled = false;
- /**
- * @param $path
- * @param array $packageInfo
- */
- public function __construct($path)
- {
- $this->path = $path;
- $this->packageInfo = $this->getPackageInfo();
- }
- public function getPackageInfo()
- {
- $package = Redis::get('plugin-package' . array_last(explode('/', $this->path)));
- if (!$package) {
- $package = app('files')->get($this->path . "/package.json");
- Redis::setex('plugin-package' . array_last(explode('/', $this->path)), 120, $package);
- }
- return json_decode($package, true);
- }
- /**
- * {@inheritdoc}
- */
- public function __get($name)
- {
- return $this->packageInfoAttribute($name);
- }
- /**
- * {@inheritdoc}
- */
- public function __isset($name)
- {
- return isset($this->{$name}) || $this->packageInfoAttribute(snake_case($name, '-'));
- }
- /**
- * Dot notation getter for composer.json attributes.
- *
- * @see https://laravel.com/docs/5.1/helpers#arrays
- *
- * @param $name
- * @return mixed
- */
- public function packageInfoAttribute($name)
- {
- if (!array_key_exists($name, $this->packageInfo)) {
- return null;
- }
- return $this->packageInfo[$name];
- }
- public function assets($relativeUri)
- {
- return Url::shopUrl("plugins/{$this->getDirname()}/$relativeUri");
- }
- /**
- * @param bool $installed
- * @return Plugin
- */
- public function setInstalled($installed)
- {
- $this->installed = $installed;
- return $this;
- }
- public function getId()
- {
- if (!isset($this->id)) {
- $idConfig = array_first(\app\common\modules\shop\ShopConfig::current()->get('plugin'), function ($v) {
- return $v['name'] == $this->name;
- });
- if ($idConfig) {
- return $idConfig['id'];
- } else {
- return '';
- }
- }
- return $this->id;
- }
- /**
- * @return bool
- */
- public function isInstalled()
- {
- return $this->installed;
- }
- public function getDirname()
- {
- return $this->name;
- }
- public function setDirname($dirname)
- {
- $this->dirname = $dirname;
- return $this;
- }
- public function getNameSpace()
- {
- return $this->namespace;
- }
- public function setNameSpace($namespace)
- {
- $this->namespace = $namespace;
- return $this;
- }
- public function getViewPath($name)
- {
- return $this->getViewPathByFileName("$name.tpl");
- }
- public function getViewPathByFileName($filename)
- {
- return $this->path . "/views/$filename";
- }
- public function getConfigView()
- {
- return $this->hasConfigView() ? view()->file($this->getViewPathByFileName(Arr::get($this->packageInfo, 'config'))) : null;
- }
- public function hasConfigView()
- {
- $filename = Arr::get($this->packageInfo, 'config');
- return $filename && file_exists($this->getViewPathByFileName($filename));
- }
- /**
- * @param string $version
- * @return Plugin
- */
- public function setVersion($version)
- {
- $this->version = $version;
- return $this;
- }
- /**
- * @return string
- */
- public function getVersion()
- {
- return $this->version;
- }
- /**
- * @param bool $enabled
- * @return Plugin
- */
- public function setEnabled($enabled)
- {
- $this->enabled = $enabled;
- return $this;
- }
- /**
- * @return bool
- */
- public function isEnabled()
- {
- return app('plugins')->isEnabled($this->name);
- }
- /**
- * @return string
- */
- public function getPath()
- {
- return $this->path;
- }
- /**
- * Determine if the given option option exists.
- *
- * @param string $key
- * @return bool
- */
- public function offsetExists($key)
- {
- return Arr::has($this->packageInfo, $key);
- }
- /**
- * Get a option option.
- *
- * @param string $key
- * @return mixed
- */
- public function offsetGet($key)
- {
- return $this->packageInfoAttribute($key);
- }
- /**
- * Set a option option.
- * @param mixed $key
- * @param mixed $value
- * @return array
- */
- public function offsetSet($key, $value)
- {
- return Arr::set($this->packageInfo, $key, $value);
- }
- /**
- * Unset a option option.
- *
- * @param string $key
- * @return void
- */
- public function offsetUnset($key)
- {
- unset($this->packageInfo[$key]);
- }
- /**
- * Generates an array result for the object.
- *
- * @return array
- */
- public function toArray()
- {
- return (array)array_merge([
- 'name' => $this->name,
- 'version' => $this->getVersion(),
- 'path' => $this->path
- ], $this->packageInfo);
- }
- public function getEnabled()
- {
- return app('plugins')->isEnabled($this->name);
- }
- public function app()
- {
- if (!isset($this->pluginApp)) {
- $pluginApplicationClass = $this->namespace . '\\' . 'PluginApplication';
- if (class_exists($pluginApplicationClass)) {
- $this->pluginApp = new $pluginApplicationClass($this);
- } else {
- $this->pluginApp = new PluginApplication($this);
- }
- }
- return $this->pluginApp;
- }
- }
|