Browse Source

强制发布文件时不覆盖语言包的menu.php以及global.php

jqh 4 years ago
parent
commit
b3c6599bfa
1 changed files with 128 additions and 12 deletions
  1. 128 12
      src/Console/PublishCommand.php

+ 128 - 12
src/Console/PublishCommand.php

@@ -3,6 +3,12 @@
 namespace Dcat\Admin\Console;
 
 use Illuminate\Console\Command;
+use Illuminate\Filesystem\Filesystem;
+use Illuminate\Support\ServiceProvider;
+use Illuminate\Support\Str;
+use League\Flysystem\Adapter\Local as LocalAdapter;
+use League\Flysystem\Filesystem as Flysystem;
+use League\Flysystem\MountManager;
 
 class PublishCommand extends Command
 {
@@ -11,11 +17,11 @@ class PublishCommand extends Command
      *
      * @var string
      */
-    protected $signature = 'admin:publish 
-    {--force : Overwrite any existing files} 
-    {--lang : Publish language files} 
-    {--assets : Publish assets files} 
-    {--migrations : Publish migrations files} 
+    protected $signature = 'admin:publish
+    {--force : Overwrite any existing files}
+    {--lang : Publish language files}
+    {--assets : Publish assets files}
+    {--migrations : Publish migrations files}
     {--config : Publish configuration files}';
 
     /**
@@ -26,13 +32,25 @@ class PublishCommand extends Command
     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";
 
     /**
-     * Execute the console command.
-     *
-     * @return void
+     * @var \Illuminate\Filesystem\Filesystem
+     */
+    protected $files;
+
+    /**
+     * @var array
      */
+    protected $tags = [];
+
+    public function __construct(Filesystem $files)
+    {
+        parent::__construct();
+
+        $this->files = $files;
+    }
+
     public function handle()
     {
-        $options = ['--provider' => 'Dcat\Admin\AdminServiceProvider'];
+        $options = [];
 
         if ($this->option('force')) {
             $options['--force'] = true;
@@ -44,8 +62,8 @@ class PublishCommand extends Command
             $this->call('vendor:publish', $options + ['--tag' => $tag]);
         }
 
-        if (! $tags) {
-            $this->call('vendor:publish', $options);
+        foreach ($this->tags as $tag) {
+            $this->publishTag($tag);
         }
 
         $this->call('view:clear');
@@ -56,7 +74,7 @@ class PublishCommand extends Command
         $tags = [];
 
         if ($this->option('lang')) {
-            $tags[] = 'dcat-admin-lang';
+            $this->tags[] = 'dcat-admin-lang';
         }
         if ($this->option('migrations')) {
             $tags[] = 'dcat-admin-migrations';
@@ -68,6 +86,104 @@ class PublishCommand extends Command
             $tags[] = 'dcat-admin-config';
         }
 
+        // 设置默认标签.
+        if (! $tags && ! $this->tags) {
+            $this->tags[] = 'dcat-admin-lang';
+            $tags = [
+                'dcat-admin-migrations',
+                'dcat-admin-assets',
+                'dcat-admin-config',
+            ];
+        }
+
         return $tags;
     }
+
+    protected function publishTag($tag)
+    {
+        $published = false;
+
+        foreach ($this->pathsToPublish($tag) as $from => $to) {
+            $this->publishItem($from, $to);
+
+            $published = true;
+        }
+
+        if ($published) {
+            $this->info('Publishing complete.');
+        } else {
+            $this->error('Unable to locate publishable resources.');
+        }
+    }
+
+    protected function pathsToPublish($tag)
+    {
+        return ServiceProvider::pathsToPublish(null, $tag);
+    }
+
+    protected function publishItem($from, $to)
+    {
+        if ($this->files->isFile($from)) {
+            return $this->publishFile($from, $to);
+        } elseif ($this->files->isDirectory($from)) {
+            return $this->publishDirectory($from, $to);
+        }
+
+        $this->error("Can't locate path: <{$from}>");
+    }
+
+    protected function publishFile($from, $to)
+    {
+        if (! $this->files->exists($to) || $this->option('force')) {
+            $this->createParentDirectory(dirname($to));
+
+            $this->files->copy($from, $to);
+
+            $this->status($from, $to, 'File');
+        }
+    }
+
+    protected function publishDirectory($from, $to)
+    {
+        $this->moveManagedFiles(new MountManager([
+            'from' => new Flysystem(new LocalAdapter($from)),
+            'to' => new Flysystem(new LocalAdapter($to)),
+        ]));
+
+        $this->status($from, $to, 'Directory');
+    }
+
+    protected function moveManagedFiles($manager)
+    {
+        foreach ($manager->listContents('from://', true) as $file) {
+            if (
+                $file['type'] === 'file'
+                && (! $manager->has('to://'.$file['path']) || $this->option('force'))
+                && ! $this->isExceptPath($manager, $file['path'])
+            ) {
+                $manager->put('to://'.$file['path'], $manager->read('from://'.$file['path']));
+            }
+        }
+    }
+
+    protected function isExceptPath($manager, $path)
+    {
+        return $manager->has('to://'.$path) && Str::contains($path, ['/menu.php', '/global.php']);
+    }
+
+    protected function createParentDirectory($directory)
+    {
+        if (! $this->files->isDirectory($directory)) {
+            $this->files->makeDirectory($directory, 0755, true);
+        }
+    }
+
+    protected function status($from, $to, $type)
+    {
+        $from = str_replace(base_path(), '', realpath($from));
+
+        $to = str_replace(base_path(), '', realpath($to));
+
+        $this->line('<info>Copied '.$type.'</info> <comment>['.$from.']</comment> <info>To</info> <comment>['.$to.']</comment>');
+    }
 }