|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Coderello\LaravelNovaLang\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use SplFileInfo; |
| 9 | + |
| 10 | +class NovaLangPublish extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'nova-lang:publish |
| 18 | + {locales : Comma separated list of languages} |
| 19 | + {--force : Override existing files}'; |
| 20 | + |
| 21 | + /** |
| 22 | + * The console command description. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $description = 'Publish Laravel Nova language files to resource folder.'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Filesystem |
| 30 | + */ |
| 31 | + protected $filesystem; |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a new command instance. |
| 35 | + * |
| 36 | + * @param Filesystem $filesystem |
| 37 | + */ |
| 38 | + public function __construct(Filesystem $filesystem) |
| 39 | + { |
| 40 | + $this->filesystem = $filesystem; |
| 41 | + |
| 42 | + parent::__construct(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute the console command. |
| 47 | + * |
| 48 | + * @return mixed |
| 49 | + */ |
| 50 | + public function handle() |
| 51 | + { |
| 52 | + $availableLocales = $this->getAvailableLocales(); |
| 53 | + |
| 54 | + $this->getRequestedLocales()->each(function (string $locale) use ($availableLocales) { |
| 55 | + if (! $availableLocales->contains($locale)) { |
| 56 | + $this->error(sprintf('Unfortunately, translations for [%s] locale doesn\'t exist. Feel free to send a PR to add them and help other people :)', $locale)); |
| 57 | + |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + $inputDirectory = $this->directoryFrom().'/'.$locale; |
| 62 | + |
| 63 | + $outputDirectory = $this->directoryTo().'/'.$locale; |
| 64 | + |
| 65 | + $inputFile = $inputDirectory.'.json'; |
| 66 | + |
| 67 | + $outputFile = $outputDirectory.'.json'; |
| 68 | + |
| 69 | + if (($this->filesystem->exists($outputDirectory) |
| 70 | + || $this->filesystem->exists($outputFile)) |
| 71 | + && ! $this->isForce()) { |
| 72 | + $this->error(sprintf('Translations for [%s] locale already exist.', $locale)); |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $this->filesystem->makeDirectory($outputDirectory, 0777, true, true); |
| 78 | + |
| 79 | + $this->filesystem->copyDirectory($inputDirectory, $outputDirectory); |
| 80 | + |
| 81 | + $this->filesystem->copy($inputFile, $outputFile); |
| 82 | + |
| 83 | + $this->info(sprintf('Translations for [%s] locale have been published successfully.', $locale)); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + protected function getRequestedLocales(): Collection |
| 88 | + { |
| 89 | + return collect(explode(',', $this->argument('locales'))); |
| 90 | + } |
| 91 | + |
| 92 | + protected function getAvailableLocales(): Collection |
| 93 | + { |
| 94 | + $localesByDirectories = collect($this->filesystem->directories($this->directoryFrom())) |
| 95 | + ->map(function (string $path) { |
| 96 | + return $this->filesystem->name($path); |
| 97 | + }); |
| 98 | + |
| 99 | + $localesByFiles = collect($this->filesystem->files($this->directoryFrom())) |
| 100 | + ->map(function (SplFileInfo $splFileInfo) { |
| 101 | + return str_replace('.'.$splFileInfo->getExtension(), '', $splFileInfo->getFilename()); |
| 102 | + }); |
| 103 | + |
| 104 | + return $localesByDirectories->intersect($localesByFiles)->values(); |
| 105 | + } |
| 106 | + |
| 107 | + protected function isForce(): bool |
| 108 | + { |
| 109 | + return $this->option('force'); |
| 110 | + } |
| 111 | + |
| 112 | + protected function directoryFrom(): string |
| 113 | + { |
| 114 | + return base_path('vendor/coderello/laravel-nova-lang/resources/lang'); |
| 115 | + } |
| 116 | + |
| 117 | + protected function directoryTo(): string |
| 118 | + { |
| 119 | + return resource_path('lang/vendor/nova'); |
| 120 | + } |
| 121 | +} |
0 commit comments