Plugin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\common\services;
  3. use ArrayAccess;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use app\common\helpers\Url;
  7. use Illuminate\Support\Facades\Redis;
  8. /**
  9. * @property string $name
  10. * @property string $description
  11. * @property string $title
  12. * @property array $author
  13. */
  14. class Plugin implements Arrayable, ArrayAccess
  15. {
  16. /**
  17. * @var PluginApplication
  18. */
  19. protected $pluginApp;
  20. /**
  21. * The full directory of this plugin.
  22. *
  23. * @var string
  24. */
  25. protected $path;
  26. /**
  27. * The directory name where the plugin installed.
  28. *
  29. * @var string
  30. */
  31. protected $dirname;
  32. /**
  33. * package.json of the package.
  34. *
  35. * @var array
  36. */
  37. protected $packageInfo;
  38. /**
  39. * Whether the plugin is installed.
  40. *
  41. * @var bool
  42. */
  43. protected $installed = true;
  44. /**
  45. * Whether the plugin is enabled.
  46. *
  47. * @var bool
  48. */
  49. protected $enabled = false;
  50. /**
  51. * @param $path
  52. * @param array $packageInfo
  53. */
  54. public function __construct($path)
  55. {
  56. $this->path = $path;
  57. $this->packageInfo = $this->getPackageInfo();
  58. }
  59. public function getPackageInfo()
  60. {
  61. $package = Redis::get('plugin-package' . array_last(explode('/', $this->path)));
  62. if (!$package) {
  63. $package = app('files')->get($this->path . "/package.json");
  64. Redis::setex('plugin-package' . array_last(explode('/', $this->path)), 120, $package);
  65. }
  66. return json_decode($package, true);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function __get($name)
  72. {
  73. return $this->packageInfoAttribute($name);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function __isset($name)
  79. {
  80. return isset($this->{$name}) || $this->packageInfoAttribute(snake_case($name, '-'));
  81. }
  82. /**
  83. * Dot notation getter for composer.json attributes.
  84. *
  85. * @see https://laravel.com/docs/5.1/helpers#arrays
  86. *
  87. * @param $name
  88. * @return mixed
  89. */
  90. public function packageInfoAttribute($name)
  91. {
  92. if (!array_key_exists($name, $this->packageInfo)) {
  93. return null;
  94. }
  95. return $this->packageInfo[$name];
  96. }
  97. public function assets($relativeUri)
  98. {
  99. return Url::shopUrl("plugins/{$this->getDirname()}/$relativeUri");
  100. }
  101. /**
  102. * @param bool $installed
  103. * @return Plugin
  104. */
  105. public function setInstalled($installed)
  106. {
  107. $this->installed = $installed;
  108. return $this;
  109. }
  110. public function getId()
  111. {
  112. if (!isset($this->id)) {
  113. $idConfig = array_first(\app\common\modules\shop\ShopConfig::current()->get('plugin'), function ($v) {
  114. return $v['name'] == $this->name;
  115. });
  116. if ($idConfig) {
  117. return $idConfig['id'];
  118. } else {
  119. return '';
  120. }
  121. }
  122. return $this->id;
  123. }
  124. /**
  125. * @return bool
  126. */
  127. public function isInstalled()
  128. {
  129. return $this->installed;
  130. }
  131. public function getDirname()
  132. {
  133. return $this->name;
  134. }
  135. public function setDirname($dirname)
  136. {
  137. $this->dirname = $dirname;
  138. return $this;
  139. }
  140. public function getNameSpace()
  141. {
  142. return $this->namespace;
  143. }
  144. public function setNameSpace($namespace)
  145. {
  146. $this->namespace = $namespace;
  147. return $this;
  148. }
  149. public function getViewPath($name)
  150. {
  151. return $this->getViewPathByFileName("$name.tpl");
  152. }
  153. public function getViewPathByFileName($filename)
  154. {
  155. return $this->path . "/views/$filename";
  156. }
  157. public function getConfigView()
  158. {
  159. return $this->hasConfigView() ? view()->file($this->getViewPathByFileName(Arr::get($this->packageInfo, 'config'))) : null;
  160. }
  161. public function hasConfigView()
  162. {
  163. $filename = Arr::get($this->packageInfo, 'config');
  164. return $filename && file_exists($this->getViewPathByFileName($filename));
  165. }
  166. /**
  167. * @param string $version
  168. * @return Plugin
  169. */
  170. public function setVersion($version)
  171. {
  172. $this->version = $version;
  173. return $this;
  174. }
  175. /**
  176. * @return string
  177. */
  178. public function getVersion()
  179. {
  180. return $this->version;
  181. }
  182. /**
  183. * @param bool $enabled
  184. * @return Plugin
  185. */
  186. public function setEnabled($enabled)
  187. {
  188. $this->enabled = $enabled;
  189. return $this;
  190. }
  191. /**
  192. * @return bool
  193. */
  194. public function isEnabled()
  195. {
  196. return app('plugins')->isEnabled($this->name);
  197. }
  198. /**
  199. * @return string
  200. */
  201. public function getPath()
  202. {
  203. return $this->path;
  204. }
  205. /**
  206. * Determine if the given option option exists.
  207. *
  208. * @param string $key
  209. * @return bool
  210. */
  211. public function offsetExists($key)
  212. {
  213. return Arr::has($this->packageInfo, $key);
  214. }
  215. /**
  216. * Get a option option.
  217. *
  218. * @param string $key
  219. * @return mixed
  220. */
  221. public function offsetGet($key)
  222. {
  223. return $this->packageInfoAttribute($key);
  224. }
  225. /**
  226. * Set a option option.
  227. * @param mixed $key
  228. * @param mixed $value
  229. * @return array
  230. */
  231. public function offsetSet($key, $value)
  232. {
  233. return Arr::set($this->packageInfo, $key, $value);
  234. }
  235. /**
  236. * Unset a option option.
  237. *
  238. * @param string $key
  239. * @return void
  240. */
  241. public function offsetUnset($key)
  242. {
  243. unset($this->packageInfo[$key]);
  244. }
  245. /**
  246. * Generates an array result for the object.
  247. *
  248. * @return array
  249. */
  250. public function toArray()
  251. {
  252. return (array)array_merge([
  253. 'name' => $this->name,
  254. 'version' => $this->getVersion(),
  255. 'path' => $this->path
  256. ], $this->packageInfo);
  257. }
  258. public function getEnabled()
  259. {
  260. return app('plugins')->isEnabled($this->name);
  261. }
  262. public function app()
  263. {
  264. if (!isset($this->pluginApp)) {
  265. $pluginApplicationClass = $this->namespace . '\\' . 'PluginApplication';
  266. if (class_exists($pluginApplicationClass)) {
  267. $this->pluginApp = new $pluginApplicationClass($this);
  268. } else {
  269. $this->pluginApp = new PluginApplication($this);
  270. }
  271. }
  272. return $this->pluginApp;
  273. }
  274. }