PackageInfo.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/20
  6. * Time: 上午11:31
  7. */
  8. namespace common\components;
  9. use common\models\Module;
  10. use yii\base\BaseObject;
  11. use Yii;
  12. use yii\helpers\Json;
  13. /**
  14. * @property string $author
  15. * @property string $version
  16. * @property string $name
  17. * @property string $title
  18. * @property string $description
  19. * @property string $keywords
  20. * @property string $package
  21. */
  22. abstract class PackageInfo extends BaseObject
  23. {
  24. public $info = [
  25. 'author' => '',
  26. 'version' => '',
  27. 'name' => '',
  28. 'title' => '',
  29. 'desc' => ''
  30. ];
  31. final public function checkInfo(){
  32. $info_check_keys = ['id','name','description','author','version'];
  33. foreach ($info_check_keys as $value) {
  34. if(!array_key_exists($value, $this->info))
  35. return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * @var string 配置文件名
  41. */
  42. public $configFile = '';
  43. public function init()
  44. {
  45. if (empty($this->configFile)) {
  46. $class = new \ReflectionClass($this);
  47. $this->configFile = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'config.php';
  48. }
  49. }
  50. private $_config = [];
  51. /**
  52. * 获取插件初始配置
  53. * @return array|mixed
  54. */
  55. final public function getInitConfig()
  56. {
  57. if (is_file($this->configFile)) {
  58. $this->_config = include $this->configFile;
  59. }
  60. return $this->_config;
  61. }
  62. /**
  63. * 获取插件当前配置
  64. * @return array|mixed
  65. */
  66. final public function getConfig()
  67. {
  68. $model = $this->getModel();
  69. $configs = Json::decode($model->config);
  70. $c = [];
  71. if (!empty($configs)) {
  72. foreach ($configs as $k => $config) {
  73. $c[$config['name']] = $config['value'];
  74. }
  75. }
  76. return $c;
  77. }
  78. public function getPath()
  79. {
  80. $class = new \ReflectionClass($this);
  81. return dirname($class->getFileName());
  82. }
  83. public function getNamespace()
  84. {
  85. $class = new \ReflectionClass($this);
  86. return $class->getNamespaceName();
  87. }
  88. public function getPackage()
  89. {
  90. return $this->info['id'];
  91. }
  92. public function getName()
  93. {
  94. return isset($this->info['name']) ? $this->info['name'] : '';
  95. }
  96. public function getBootstrap()
  97. {
  98. return isset($this->info['bootstrap']) ? $this->info['bootstrap'] : '';
  99. }
  100. public function getAuthor()
  101. {
  102. return isset($this->info['author']) ? $this->info['author'] : '';
  103. }
  104. public function getVersion()
  105. {
  106. return isset($this->info['version']) ? $this->info['version'] : '';
  107. }
  108. public function getDescription()
  109. {
  110. return isset($this->info['description']) ? $this->info['description'] : '';
  111. }
  112. public function getKeywords()
  113. {
  114. return isset($this->info['keywords']) ? $this->info['keywords'] : '';
  115. }
  116. public function getScreenshot()
  117. {
  118. $class = new \ReflectionClass($this);
  119. $screenshot = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'screenshot.png';
  120. $url = "";
  121. if (file_exists($screenshot)) {
  122. list (, $url) = \Yii::$app->getAssetManager()->publish($screenshot);
  123. }
  124. return $url;
  125. }
  126. private $_model;
  127. /**
  128. * @return Module
  129. */
  130. public function getModel()
  131. {
  132. if ($this->_model == null) {
  133. $model = Module::findOne($this->getPackage());
  134. if ($model == null) {
  135. $model = new Module();
  136. $model->loadDefaultValues();
  137. $model->id = $this->getPackage();
  138. }
  139. $this->_model = $model;
  140. }
  141. return $this->_model;
  142. }
  143. public function getInstall()
  144. {
  145. return $this->getModel()->getInstall();
  146. }
  147. public function getOpen()
  148. {
  149. return $this->getModel()->getOpen();
  150. }
  151. public function canUninstall()
  152. {
  153. return $this->getModel()->install === true;
  154. }
  155. public function canClose()
  156. {
  157. return $this->getModel()->is_core == false;
  158. }
  159. }