AddonHelper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use League\Flysystem\Filesystem;
  6. use League\Flysystem\Adapter\Local;
  7. /**
  8. * Class AddonHelper
  9. * @package common\helpers
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class AddonHelper
  13. {
  14. /**
  15. * 获取配置文件
  16. *
  17. * @param $name
  18. * @return bool|string
  19. */
  20. public static function getConfigClass($name)
  21. {
  22. if (!class_exists($class = static::getAddonConfig($name))) {
  23. return false;
  24. }
  25. return $class;
  26. }
  27. /**
  28. * 获取插件配置
  29. *
  30. * @param $name
  31. * @return string
  32. */
  33. public static function getAddonConfig($name)
  34. {
  35. return static::getAddonRoot($name) . "AddonConfig";
  36. }
  37. /**
  38. * 获取插件的命名空间
  39. *
  40. * @param $name
  41. * @return string
  42. */
  43. public static function getAddonRoot($name)
  44. {
  45. return "addons" . "\\" . $name . "\\";
  46. }
  47. /**
  48. * 获取插件的根目录目录
  49. *
  50. * @param $name
  51. * @return string
  52. */
  53. public static function getAddonRootPath($name)
  54. {
  55. return Yii::getAlias('@addons') . "/{$name}/";
  56. }
  57. /**
  58. * 验证插件目录是否存在
  59. *
  60. * @param $name
  61. * @return bool
  62. */
  63. public static function has($name)
  64. {
  65. if (!is_dir(static::getAddonRootPath($name))) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * @param $name
  72. * @return string
  73. * @throws \League\Flysystem\FileExistsException
  74. * @throws \League\Flysystem\FileNotFoundException
  75. */
  76. public static function getAddonIcon($name)
  77. {
  78. $adapter = new Local(Yii::getAlias('@root'));
  79. $filesystem = new Filesystem($adapter);
  80. $localIconPath = static::getAddonRoot($name) . 'icon.jpg';
  81. if ($filesystem->has($localIconPath)) {
  82. $md5 = md5(Json::encode($filesystem->getMetadata($localIconPath)));
  83. $newPath = '/assets/tmp/' . $md5 . '.jpg';
  84. $newLocalIconPath = 'web/backend' . $newPath;
  85. if (!$filesystem->has($newLocalIconPath)) {
  86. $filesystem->copy($localIconPath, $newLocalIconPath);
  87. }
  88. // 后台独立域名
  89. if ($backendUrl = Yii::getAlias('@backendUrl')) {
  90. return $backendUrl . $newPath;
  91. }
  92. return '/backend' . $newPath;
  93. }
  94. return false;
  95. }
  96. /**
  97. * 获取生成asset的资源文件目录
  98. *
  99. * @param string $assets
  100. * @return string
  101. */
  102. public static function filePath($assets = '')
  103. {
  104. if (!$assets) {
  105. $assets = [];
  106. $assets[] = 'addons';
  107. $assets[] = Yii::$app->params['addon']['name'];
  108. $assets[] = Yii::$app->params['realAppId'];
  109. $assets[] = 'assets';
  110. $assets[] = 'AppAsset';
  111. $assets = implode('\\', $assets);
  112. }
  113. if (!isset(Yii::$app->view->assetBundles[$assets])) {
  114. /* @var $assets \yii\web\AssetBundle */
  115. $assets::register(Yii::$app->view);
  116. }
  117. return Yii::$app->view->assetBundles[$assets]->baseUrl . '/';
  118. }
  119. /**
  120. * 获取资源文件
  121. *
  122. * @return string
  123. */
  124. public static function file($path, $assets = '')
  125. {
  126. return self::filePath($assets) . $path;
  127. }
  128. /**
  129. * @param $path
  130. * @param array $options
  131. * @param string $assets
  132. * @return string
  133. */
  134. public static function jsFile($path, $options = [], $assets = '')
  135. {
  136. return Html::jsFile(self::filePath($assets) . $path, $options);
  137. }
  138. /**
  139. * @param $path
  140. * @param array $options
  141. * @param string $assets
  142. * @return string
  143. */
  144. public static function cssFile($path, $options = [], $assets = '')
  145. {
  146. return Html::cssFile(self::filePath($assets) . $path, $options);
  147. }
  148. }
粤ICP备19079148号