nicolasbeauvais /
laravel-transcribe
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace NicolasBeauvais\Transcribe\Commands; |
||||
| 4 | |||||
| 5 | use Illuminate\Console\Command; |
||||
| 6 | use Illuminate\Support\Arr; |
||||
| 7 | use Illuminate\Support\Str; |
||||
| 8 | use NicolasBeauvais\Transcribe\Manager; |
||||
| 9 | |||||
| 10 | class RenameCommand extends Command |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * The name and signature of the console command. |
||||
| 14 | * |
||||
| 15 | * @var string |
||||
| 16 | */ |
||||
| 17 | protected $signature = 'transcribe:rename {oldKey} {newKey}'; |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * The name and signature of the console command. |
||||
| 21 | * |
||||
| 22 | * @var string |
||||
| 23 | */ |
||||
| 24 | protected $description = 'Rename the given key.'; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * The Languages manager instance. |
||||
| 28 | * |
||||
| 29 | * @var \NicolasBeauvais\Transcribe\Manager |
||||
| 30 | */ |
||||
| 31 | private $manager; |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * Array of files grouped by filename. |
||||
| 35 | * |
||||
| 36 | * @var array |
||||
| 37 | */ |
||||
| 38 | protected $files; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * ListCommand constructor. |
||||
| 42 | * |
||||
| 43 | * @param \NicolasBeauvais\Transcribe\Manager $manager |
||||
| 44 | * |
||||
| 45 | * @return void |
||||
| 46 | */ |
||||
| 47 | public function __construct(Manager $manager) |
||||
| 48 | { |
||||
| 49 | parent::__construct(); |
||||
| 50 | |||||
| 51 | $this->manager = $manager; |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * Execute the console command. |
||||
| 56 | * |
||||
| 57 | * @return mixed |
||||
| 58 | */ |
||||
| 59 | public function handle() |
||||
| 60 | { |
||||
| 61 | $this->renameKey(); |
||||
| 62 | |||||
| 63 | $this->listFilesContainingOldKey(); |
||||
| 64 | |||||
| 65 | $this->info('The key at '.$this->argument('oldKey').' was renamed to '.$this->argument('newKey').' successfully!'); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
Are you sure
$this->argument('oldKey') of type string|array can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 66 | } |
||||
| 67 | |||||
| 68 | /** |
||||
| 69 | * Rename the given oldKey to the newKey. |
||||
| 70 | * |
||||
| 71 | * @return void |
||||
| 72 | */ |
||||
| 73 | private function renameKey() |
||||
| 74 | { |
||||
| 75 | try { |
||||
| 76 | list($file, $key) = explode('.', $this->argument('oldKey'), 2); |
||||
|
0 ignored issues
–
show
It seems like
$this->argument('oldKey') can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 77 | } catch (\ErrorException $e) { |
||||
| 78 | $this->error('Could not recognize the key you want to rename.'); |
||||
| 79 | |||||
| 80 | return; |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | if (Str::contains($this->argument('newKey'), '.')) { |
||||
|
0 ignored issues
–
show
It seems like
$this->argument('newKey') can also be of type array; however, parameter $haystack of Illuminate\Support\Str::contains() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 84 | $this->error('Please provide the new key must not contain a dot.'); |
||||
| 85 | |||||
| 86 | return; |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | $newKey = preg_replace('/(\w+)$/i', $this->argument('newKey'), $key); |
||||
| 90 | |||||
| 91 | $files = $this->manager->files()[$file]; |
||||
| 92 | |||||
| 93 | $currentValues = []; |
||||
| 94 | |||||
| 95 | foreach ($files as $languageKey => $filePath) { |
||||
| 96 | $content = Arr::dot($this->manager->getFileContent($filePath)); |
||||
| 97 | |||||
| 98 | $currentValues[$languageKey] = isset($content[$key]) ? $content[$key] : ''; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | $this->manager->removeKey($file, $key); |
||||
| 102 | |||||
| 103 | $this->manager->fillKeys( |
||||
| 104 | $file, |
||||
| 105 | [$newKey => $currentValues] |
||||
| 106 | ); |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | /** |
||||
| 110 | * Show a table with application files containing the old key. |
||||
| 111 | * |
||||
| 112 | * @return void |
||||
| 113 | */ |
||||
| 114 | private function listFilesContainingOldKey() |
||||
| 115 | { |
||||
| 116 | if ($files = $this->getFilesContainingOldKey()) { |
||||
| 117 | $this->info('Renamed key was found in '.count($files).' file(s).'); |
||||
| 118 | |||||
| 119 | $this->table(['Encounters', 'File'], $this->getTableRows($files)); |
||||
| 120 | } |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | /** |
||||
| 124 | * Get an array of application files containing the old key. |
||||
| 125 | * |
||||
| 126 | * @return array |
||||
| 127 | */ |
||||
| 128 | private function getFilesContainingOldKey() |
||||
| 129 | { |
||||
| 130 | $affectedFiles = []; |
||||
| 131 | |||||
| 132 | foreach ($this->manager->getAllViewFilesWithTranslations() as $file => $keys) { |
||||
| 133 | foreach ($keys as $key) { |
||||
| 134 | if ($key == $this->argument('oldKey')) { |
||||
| 135 | $affectedFiles[$file][] = $key; |
||||
| 136 | } |
||||
| 137 | } |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | return $affectedFiles; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * Get table rows for the list of files containing the old key. |
||||
| 145 | * |
||||
| 146 | * @param array $files |
||||
| 147 | * |
||||
| 148 | * @return array |
||||
| 149 | */ |
||||
| 150 | private function getTableRows($files) |
||||
| 151 | { |
||||
| 152 | $rows = []; |
||||
| 153 | |||||
| 154 | foreach ($files as $file => $keys) { |
||||
| 155 | $rows[] = [count($keys), $file]; |
||||
| 156 | } |
||||
| 157 | |||||
| 158 | return $rows; |
||||
| 159 | } |
||||
| 160 | } |
||||
| 161 |