|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | + |
| 7 | +require_once base_path('app/Console/Commands/Helpers/GenerateLanguageFile.php'); |
| 8 | + |
| 9 | +class AddExpression extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'locale:add |
| 17 | + {language : the language to add eg. en} |
| 18 | + {key : the key of the expression eg. general.home} |
| 19 | + {value : the localized value of the expression} |
| 20 | + {--F|force : Whether confirmation is required to overwrite values}'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The console command description. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $description = 'Add an expression to a language file'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Create a new command instance. |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + parent::__construct(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Execute the console command. |
| 41 | + * |
| 42 | + * @return mixed |
| 43 | + */ |
| 44 | + public function handle() |
| 45 | + { |
| 46 | + $language = $this->argument('language'); |
| 47 | + $file = explode('.', $this->argument('key'))[0]; |
| 48 | + $expression_key = explode('.', $this->argument('key'))[1]; |
| 49 | + $expression_value = $this->argument('value'); |
| 50 | + $reviewed = $this->option('force'); |
| 51 | + $expressions = require base_path('resources/lang/'.$language.'/'.$file.'.php'); |
| 52 | + if (! ($reviewed)) { |
| 53 | + if (isset($expressions[$expression_key])) { |
| 54 | + if ($this->confirm('Do you want to override '.$expressions[$expression_key].' to '.$expression_value.'?')) { |
| 55 | + $reviewed = true; |
| 56 | + } |
| 57 | + } else { |
| 58 | + if ($this->confirm('Do you want to set '.$expression_key.' as '.$expression_value.'?')) { |
| 59 | + $reviewed = true; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + if ($reviewed) { |
| 64 | + $expressions[$expression_key] = $expression_value; |
| 65 | + if (! (ksort($expressions))) { |
| 66 | + $this->error('Sorting '.$file_in.' failed.'); |
| 67 | + } |
| 68 | + $file_write = fopen(base_path('resources/lang/'.$language.'/'.$file.'.php'), 'w'); |
| 69 | + if (! (generate_file($file_write, $expressions))) { |
| 70 | + $this->error('Writing to '.$file.' failed.'); |
| 71 | + } |
| 72 | + $this->comment('Set '.$expression_key.' to '.$expression_value.'.'); |
| 73 | + } else { |
| 74 | + $this->comment('Nothing changed.'); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments