1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelLang\Commands; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelLang\Contracts\TransPublisher; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class PublishCommand |
11
|
|
|
* |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class PublishCommand extends AbstractCommand |
15
|
|
|
{ |
16
|
|
|
/* ----------------------------------------------------------------- |
17
|
|
|
| Properties |
18
|
|
|
| ----------------------------------------------------------------- |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The name and signature of the console command. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $signature = 'trans:publish |
27
|
|
|
{locale : The language to publish the translations.} |
28
|
|
|
{--force : Force to override the translations} |
29
|
|
|
{--inline : Publish the inline translations} |
30
|
|
|
{--json : Include json translations file}'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The console command description. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $description = 'Publish the [locale] translations.'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The TransPublisher instance. |
41
|
|
|
* |
42
|
|
|
* @var \Arcanedev\LaravelLang\Contracts\TransPublisher |
43
|
|
|
*/ |
44
|
|
|
private $publisher; |
45
|
|
|
|
46
|
|
|
/* ----------------------------------------------------------------- |
47
|
|
|
| Constructor |
48
|
|
|
| ----------------------------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new console command instance. |
53
|
|
|
* |
54
|
|
|
* @param \Arcanedev\LaravelLang\Contracts\TransPublisher $publisher |
55
|
|
|
*/ |
56
|
48 |
|
public function __construct(TransPublisher $publisher) |
57
|
|
|
{ |
58
|
48 |
|
$this->publisher = $publisher; |
59
|
48 |
|
$this->name = 'trans:publish'; |
60
|
|
|
|
61
|
48 |
|
parent::__construct(); |
62
|
48 |
|
} |
63
|
|
|
|
64
|
|
|
/* ----------------------------------------------------------------- |
65
|
|
|
| Main Methods |
66
|
|
|
| ----------------------------------------------------------------- |
67
|
|
|
*/ |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Execute the console command. |
71
|
|
|
*/ |
72
|
36 |
|
public function handle(): void |
73
|
|
|
{ |
74
|
36 |
|
$this->copyright(); |
75
|
|
|
|
76
|
36 |
|
$locale = (string) $this->argument('locale'); |
77
|
|
|
|
78
|
36 |
|
if ($this->publisher->isDefault($locale)) { |
79
|
6 |
|
$this->info("The locale [{$locale}] is a default lang and it's shipped with laravel."); |
80
|
6 |
|
$this->line(''); |
81
|
6 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
30 |
|
$this->publish($locale, [ |
85
|
30 |
|
'force' => (bool) $this->option('force'), |
86
|
30 |
|
'inline' => (bool) $this->option('inline'), |
87
|
30 |
|
'json' => (bool) $this->option('json'), |
88
|
|
|
]); |
89
|
|
|
|
90
|
30 |
|
$this->line(''); |
91
|
30 |
|
} |
92
|
|
|
|
93
|
|
|
/* ----------------------------------------------------------------- |
94
|
|
|
| Other Methods |
95
|
|
|
| ----------------------------------------------------------------- |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Publish the translations. |
100
|
|
|
* |
101
|
|
|
* @param string $locale |
102
|
|
|
* @param array $options |
103
|
|
|
*/ |
104
|
30 |
|
private function publish(string $locale, array $options): void |
105
|
|
|
{ |
106
|
30 |
|
$this->info("Publishing the [{$locale}] translations..."); |
107
|
|
|
|
108
|
30 |
|
$results = $this->publisher->publish($locale, $options); |
109
|
|
|
|
110
|
30 |
|
$this->showResults(['Published translations'], $results['published']); |
111
|
30 |
|
$this->showResults(['Skipped translations'], $results['skipped']); |
112
|
30 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Show the results. |
116
|
|
|
* |
117
|
|
|
* @param array $headers |
118
|
|
|
* @param array $results |
119
|
|
|
*/ |
120
|
30 |
|
public function showResults(array $headers, array $results): void |
121
|
|
|
{ |
122
|
30 |
|
if (empty($results)) |
123
|
30 |
|
return; |
124
|
|
|
|
125
|
30 |
|
$this->table($headers, array_map(function($result) { |
126
|
30 |
|
return [$result]; |
127
|
30 |
|
}, $results)); |
128
|
30 |
|
} |
129
|
|
|
} |
130
|
|
|
|