Manager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. namespace Dcat\Admin\Extend;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Exception\AdminException;
  5. use Dcat\Admin\Exception\RuntimeException;
  6. use Dcat\Admin\Models\Extension as ExtensionModel;
  7. use Dcat\Admin\Models\Extension;
  8. use Dcat\Admin\Support\Composer;
  9. use Dcat\Admin\Support\Helper;
  10. use Dcat\Admin\Support\Zip;
  11. use Illuminate\Contracts\Container\Container;
  12. use Illuminate\Filesystem\Filesystem;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Str;
  15. use RecursiveDirectoryIterator;
  16. use RecursiveIteratorIterator;
  17. class Manager
  18. {
  19. use Note;
  20. /**
  21. * @var Container
  22. */
  23. protected $app;
  24. /**
  25. * @var ServiceProvider[]|Collection
  26. */
  27. protected $extensions;
  28. /**
  29. * @var array
  30. */
  31. protected $extensionPaths = [];
  32. /**
  33. * @var ExtensionModel[]|Collection
  34. */
  35. protected $settings;
  36. /**
  37. * @var Filesystem
  38. */
  39. protected $files;
  40. public function __construct(Container $app)
  41. {
  42. $this->app = $app;
  43. $this->extensions = new Collection();
  44. $this->files = app('files');
  45. }
  46. /**
  47. * 注册扩展.
  48. *
  49. * @return void
  50. */
  51. public function register()
  52. {
  53. $this->load();
  54. $this->extensions->each->register();
  55. }
  56. /**
  57. * 初始化扩展.
  58. */
  59. public function boot()
  60. {
  61. $this->extensions->each->boot();
  62. }
  63. /**
  64. * 判断扩展是否启用.
  65. *
  66. * @param string|null $name
  67. *
  68. * @return bool
  69. */
  70. public function enabled(?string $name)
  71. {
  72. return (bool) optional($this->settings()->get($name))->is_enabled;
  73. }
  74. /**
  75. * 启用或禁用扩展.
  76. *
  77. * @param string|null $name
  78. * @param bool $enable
  79. *
  80. * @return void
  81. */
  82. public function enable(?string $name, bool $enable = true)
  83. {
  84. $name = $this->getName($name);
  85. $extension = Extension::where('name', $name)->first();
  86. if (! $extension) {
  87. throw new RuntimeException(sprintf('Please install the extension(%s) first!', $name));
  88. }
  89. $extension->is_enabled = $enable;
  90. $extension->save();
  91. }
  92. /**
  93. * 加载扩展,注册自动加载规则.
  94. *
  95. * @return void
  96. */
  97. public function load()
  98. {
  99. foreach ($this->getExtensionDirectories() as $directory) {
  100. try {
  101. $this->loadExtension($directory);
  102. } catch (\Throwable $e) {
  103. $this->reportException($e);
  104. }
  105. }
  106. }
  107. /**
  108. * 获取扩展路径.
  109. *
  110. * @param string|ServiceProvider $name
  111. * @param string|null $path
  112. *
  113. * @return string|void
  114. *
  115. * @throws \ReflectionException
  116. */
  117. public function path($name, $path = null)
  118. {
  119. if (! $extension = $this->get($name)) {
  120. return;
  121. }
  122. return $extension->path($path);
  123. }
  124. /**
  125. * 获取扩展对象.
  126. *
  127. * @param string|ServiceProvider $name
  128. *
  129. * @return ServiceProvider|null
  130. */
  131. public function get($name)
  132. {
  133. if ($name instanceof ServiceProvider) {
  134. return $name;
  135. }
  136. return $this->extensions->get($this->formatName($name));
  137. }
  138. /**
  139. * 判断插件是否存在.
  140. *
  141. * @param string $name
  142. *
  143. * @return bool
  144. */
  145. public function has($name)
  146. {
  147. return $this->extensions->has($this->formatName($name));
  148. }
  149. /**
  150. * @param string $name
  151. *
  152. * @return mixed
  153. */
  154. protected function formatName($name)
  155. {
  156. if (! is_string($name)) {
  157. return $name;
  158. }
  159. return str_replace('/', '.', $name);
  160. }
  161. /**
  162. * 获取所有扩展.
  163. *
  164. * @return ServiceProvider[]|Collection
  165. */
  166. public function all()
  167. {
  168. return $this->extensions;
  169. }
  170. /**
  171. * 获取已启用的扩展.
  172. *
  173. * @return ServiceProvider[]|Collection
  174. */
  175. public function available()
  176. {
  177. return $this->all()->filter->enabled();
  178. }
  179. /**
  180. * 加载扩展.
  181. *
  182. * @param string $directory
  183. * @param bool $addPsr4
  184. *
  185. * @return ServiceProvider|null
  186. */
  187. public function loadExtension(string $directory, bool $addPsr4 = true)
  188. {
  189. if (array_key_exists($directory, $this->extensionPaths)) {
  190. return $this->extensionPaths[$directory];
  191. }
  192. $this->extensionPaths[$directory] = $serviceProvider = $this->resolveExtension($directory, $addPsr4);
  193. if ($serviceProvider) {
  194. $this->addExtension($serviceProvider);
  195. }
  196. return $serviceProvider;
  197. }
  198. /**
  199. * 获取扩展类实例.
  200. *
  201. * @param string $directory
  202. * @param bool $addPsr4
  203. *
  204. * @return ServiceProvider
  205. */
  206. public function resolveExtension(string $directory, bool $addPsr4 = true)
  207. {
  208. $composerProperty = Composer::parse($directory.'/composer.json');
  209. $serviceProvider = $composerProperty->get('extra.dcat-admin');
  210. $psr4 = $composerProperty->get('autoload.psr-4');
  211. if (! $serviceProvider || ! $psr4) {
  212. return;
  213. }
  214. if ($addPsr4) {
  215. $this->registerPsr4($directory, $psr4);
  216. }
  217. $serviceProvider = new $serviceProvider($this->app);
  218. return $serviceProvider->withComposerProperty($composerProperty);
  219. }
  220. /**
  221. * 获取扩展目录.
  222. *
  223. * @param string $dirPath
  224. *
  225. * @return array
  226. */
  227. public function getExtensionDirectories($dirPath = null)
  228. {
  229. $extensions = [];
  230. $dirPath = $dirPath ?: admin_extension_path();
  231. if (! is_dir($dirPath)) {
  232. return $extensions;
  233. }
  234. $it = new RecursiveIteratorIterator(
  235. new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS)
  236. );
  237. $it->setMaxDepth(2);
  238. $it->rewind();
  239. while ($it->valid()) {
  240. if ($it->getDepth() > 1 && $it->getFilename() === 'composer.json') {
  241. $extensions[] = dirname($it->getPathname());
  242. }
  243. $it->next();
  244. }
  245. return $extensions;
  246. }
  247. /**
  248. * 添加扩展.
  249. *
  250. * @param \Dcat\Admin\Extend\ServiceProvider $serviceProvider
  251. */
  252. public function addExtension(ServiceProvider $serviceProvider)
  253. {
  254. if (! $serviceProvider->getName()) {
  255. $json = dirname(Helper::guessClassFileName($serviceProvider)).'/composer.json';
  256. if (! is_file($json)) {
  257. throw new RuntimeException('Error extension "%s"', get_class($serviceProvider));
  258. }
  259. $serviceProvider->withComposerProperty(Composer::parse($json));
  260. }
  261. $this->extensions->put($serviceProvider->getName(), $serviceProvider);
  262. $this->app->instance($abstract = get_class($serviceProvider), $serviceProvider);
  263. $this->app->alias($abstract, $serviceProvider->getName());
  264. }
  265. /**
  266. * 获取扩展名称.
  267. *
  268. * @param $extension
  269. *
  270. * @return string
  271. */
  272. public function getName($extension)
  273. {
  274. if ($extension instanceof ServiceProvider) {
  275. return $extension->getName();
  276. }
  277. return $this->formatName($extension);
  278. }
  279. /**
  280. * 解压缩扩展包.
  281. */
  282. public function extract($filePath)
  283. {
  284. $filePath = is_file($filePath) ? $filePath : $this->getFilePath($filePath);
  285. if (! $this->checkZip($filePath)) {
  286. throw new RuntimeException(sprintf('Error extension file "%s".', $filePath));
  287. }
  288. if (! Zip::extract($filePath, admin_extension_path())) {
  289. throw new AdminException(sprintf('Unable to extract core file \'%s\'.', $filePath));
  290. }
  291. @unlink($filePath);
  292. }
  293. /**
  294. * 验证文件是否正确.
  295. *
  296. * @param string $filePath
  297. *
  298. * @return bool
  299. */
  300. public function checkZip($filePath)
  301. {
  302. // 创建临时目录.
  303. $tempPath = $this->makeTempDirectory();
  304. try {
  305. $filePath = is_file($filePath) ? $filePath : $this->getFilePath($filePath);
  306. if (! Zip::extract($filePath, $tempPath)) {
  307. throw new AdminException(sprintf('Unable to extract core file \'%s\'.', $filePath));
  308. }
  309. $extensions = $this->getExtensionDirectories($tempPath);
  310. } finally {
  311. $this->files->deleteDirectory($tempPath);
  312. }
  313. return count($extensions) === 1;
  314. }
  315. /**
  316. * 生成临时文件.
  317. *
  318. * @param string $fileCode A unique file code
  319. * @return string Full path on the disk
  320. */
  321. protected function getFilePath($fileCode)
  322. {
  323. $name = md5($fileCode).'.arc';
  324. return $this->makeTempDirectory('extensions').'/'.$name;
  325. }
  326. /**
  327. * 获取配置.
  328. *
  329. * @return ExtensionModel[]|Collection
  330. */
  331. public function settings()
  332. {
  333. if ($this->settings === null) {
  334. try {
  335. $this->settings = ExtensionModel::all()->keyBy('name');
  336. } catch (\Throwable $e) {
  337. $this->reportException($e);
  338. $this->settings = new Collection();
  339. }
  340. }
  341. return $this->settings;
  342. }
  343. /**
  344. * @return UpdateManager
  345. */
  346. public function updateManager()
  347. {
  348. return app('admin.extend.update');
  349. }
  350. /**
  351. * @return VersionManager
  352. */
  353. public function versionManager()
  354. {
  355. return app('admin.extend.version');
  356. }
  357. /**
  358. * 创建临时目录.
  359. *
  360. * @param string $dir
  361. *
  362. * @return string
  363. */
  364. protected function makeTempDirectory($dir = null)
  365. {
  366. $tempDir = storage_path('tmp/'.($dir ?: time().Str::random()));
  367. if (! is_dir($tempDir)) {
  368. if (! $this->files->makeDirectory($tempDir, 777, true)) {
  369. throw new RuntimeException(sprintf('Cannot write to directory "%s"', storage_path()));
  370. }
  371. }
  372. return $tempDir;
  373. }
  374. /**
  375. * 注册 PSR4 验证规则.
  376. *
  377. * @param string $directory
  378. * @param array $psr4
  379. */
  380. protected function registerPsr4($directory, array $psr4)
  381. {
  382. $classLoader = Admin::classLoader();
  383. foreach ($psr4 as $namespace => $path) {
  384. $path = $directory.'/'.trim($path, '/').'/';
  385. $classLoader->addPsr4($namespace, $path);
  386. }
  387. }
  388. /**
  389. * 上报异常.
  390. *
  391. * @param \Throwable $e
  392. */
  393. protected function reportException(\Throwable $e)
  394. {
  395. report($e);
  396. }
  397. }