PublishCommand.php 4.9 KB

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