123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- 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
- {
- /**
- * The console command name.
- *
- * @var string
- */
- 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}';
- /**
- * The console command description.
- *
- * @var string
- */
- 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";
- /**
- * @var \Illuminate\Filesystem\Filesystem
- */
- protected $files;
- /**
- * @var array
- */
- protected $tags = [];
- public function __construct(Filesystem $files)
- {
- parent::__construct();
- $this->files = $files;
- }
- public function handle()
- {
- $options = [];
- if ($this->option('force')) {
- $options['--force'] = true;
- }
- $tags = $this->getTags();
- foreach ($tags as $tag) {
- $this->call('vendor:publish', $options + ['--tag' => $tag]);
- }
- foreach ($this->tags as $tag) {
- $this->publishTag($tag);
- }
- $this->call('view:clear');
- }
- protected function getTags()
- {
- $tags = [];
- if ($this->option('lang')) {
- $this->tags[] = 'dcat-admin-lang';
- }
- if ($this->option('migrations')) {
- $tags[] = 'dcat-admin-migrations';
- }
- if ($this->option('assets')) {
- $tags[] = 'dcat-admin-assets';
- }
- if ($this->option('config')) {
- $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>');
- }
- }
|