PublishCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Dcat\Admin\Console;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Support\ServiceProvider;
  6. use Illuminate\Support\Str;
  7. use League\Flysystem\Adapter\Local as LocalAdapter;
  8. use League\Flysystem\Filesystem as Flysystem;
  9. use League\Flysystem\Local\LocalFilesystemAdapter;
  10. use League\Flysystem\MountManager;
  11. class PublishCommand extends Command
  12. {
  13. /**
  14. * The console command name.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'admin:publish
  19. {--force : Overwrite any existing files}
  20. {--lang : Publish language files}
  21. {--assets : Publish assets files}
  22. {--migrations : Publish migrations files}
  23. {--config : Publish configuration files}';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = "Re-publish dcat-admin's assets, configuration, language and migration files. If you want overwrite the existing files, you can add the `--force` option";
  30. /**
  31. * @var \Illuminate\Filesystem\Filesystem
  32. */
  33. protected $files;
  34. /**
  35. * @var array
  36. */
  37. protected $tags = [];
  38. public function __construct(Filesystem $files)
  39. {
  40. parent::__construct();
  41. $this->files = $files;
  42. }
  43. public function handle()
  44. {
  45. $options = [];
  46. if ($this->option('force')) {
  47. $options['--force'] = true;
  48. }
  49. $tags = $this->getTags();
  50. foreach ($tags as $tag) {
  51. $this->call('vendor:publish', $options + ['--tag' => $tag]);
  52. }
  53. foreach ($this->tags as $tag) {
  54. $this->publishTag($tag);
  55. }
  56. $this->call('view:clear');
  57. }
  58. protected function getTags()
  59. {
  60. $tags = [];
  61. if ($this->option('lang')) {
  62. $this->tags[] = 'dcat-admin-lang';
  63. }
  64. if ($this->option('migrations')) {
  65. $tags[] = 'dcat-admin-migrations';
  66. }
  67. if ($this->option('assets')) {
  68. $tags[] = 'dcat-admin-assets';
  69. }
  70. if ($this->option('config')) {
  71. $tags[] = 'dcat-admin-config';
  72. }
  73. // 设置默认标签.
  74. if (! $tags && ! $this->tags) {
  75. $this->tags[] = 'dcat-admin-lang';
  76. $tags = [
  77. 'dcat-admin-migrations',
  78. 'dcat-admin-assets',
  79. 'dcat-admin-config',
  80. ];
  81. }
  82. return $tags;
  83. }
  84. protected function publishTag($tag)
  85. {
  86. $published = false;
  87. foreach ($this->pathsToPublish($tag) as $from => $to) {
  88. $this->publishItem($from, $to);
  89. $published = true;
  90. }
  91. if ($published) {
  92. $this->info('Publishing complete.');
  93. } else {
  94. $this->error('Unable to locate publishable resources.');
  95. }
  96. }
  97. protected function pathsToPublish($tag)
  98. {
  99. return ServiceProvider::pathsToPublish(null, $tag);
  100. }
  101. protected function publishItem($from, $to)
  102. {
  103. if ($this->files->isFile($from)) {
  104. return $this->publishFile($from, $to);
  105. } elseif ($this->files->isDirectory($from)) {
  106. return $this->publishDirectory($from, $to);
  107. }
  108. $this->error("Can't locate path: <{$from}>");
  109. }
  110. protected function publishFile($from, $to)
  111. {
  112. if (! $this->files->exists($to) || $this->option('force')) {
  113. $this->createParentDirectory(dirname($to));
  114. $this->files->copy($from, $to);
  115. $this->status($from, $to, 'File');
  116. }
  117. }
  118. protected function publishDirectory($from, $to)
  119. {
  120. $localClass = class_exists(LocalAdapter::class) ? LocalAdapter::class : LocalFilesystemAdapter::class;
  121. $this->moveManagedFiles(new MountManager([
  122. 'from' => new Flysystem(new $localClass($from)),
  123. 'to' => new Flysystem(new $localClass($to)),
  124. ]));
  125. $this->status($from, $to, 'Directory');
  126. }
  127. protected function moveManagedFiles(MountManager $manager)
  128. {
  129. if (method_exists($manager, 'put')) {
  130. foreach ($manager->listContents('from://', true) as $file) {
  131. if (
  132. $file['type'] === 'file'
  133. && (! $manager->has('to://'.$file['path']) || $this->option('force'))
  134. && ! $this->isExceptPath($manager, $file['path'])
  135. ) {
  136. $manager->put('to://'.$file['path'], $manager->read('from://'.$file['path']));
  137. }
  138. }
  139. return;
  140. }
  141. foreach ($manager->listContents('from://', true) as $file) {
  142. $path = Str::after($file['path'], 'from://');
  143. if ($file['type'] === 'file' && (! $manager->fileExists('to://'.$path) || $this->option('force'))) {
  144. $manager->write('to://'.$path, $manager->read($file['path']));
  145. }
  146. }
  147. }
  148. protected function isExceptPath($manager, $path)
  149. {
  150. return $manager->has('to://'.$path) && Str::contains($path, ['/menu.php', '/global.php']);
  151. }
  152. protected function createParentDirectory($directory)
  153. {
  154. if (! $this->files->isDirectory($directory)) {
  155. $this->files->makeDirectory($directory, 0755, true);
  156. }
  157. }
  158. protected function status($from, $to, $type)
  159. {
  160. $from = str_replace(base_path(), '', realpath($from));
  161. $to = str_replace(base_path(), '', realpath($to));
  162. $this->line('<info>Copied '.$type.'</info> <comment>['.$from.']</comment> <info>To</info> <comment>['.$to.']</comment>');
  163. }
  164. }