|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RichanFongdasen\I18n\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Console\Kernel; |
|
6
|
|
|
use Illuminate\Foundation\Console\RouteListCommand; |
|
7
|
|
|
use RichanFongdasen\I18n\Traits\TranslatedRouteCommandContext; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
|
|
10
|
|
|
class RouteTranslationsListCommand extends RouteListCommand |
|
11
|
|
|
{ |
|
12
|
|
|
use TranslatedRouteCommandContext; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name = 'route:trans:list'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'List all registered routes for specific locales'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Execute the console command. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function handle() |
|
28
|
|
|
{ |
|
29
|
|
|
$locale = $this->argument('locale'); |
|
30
|
|
|
|
|
31
|
|
|
if (!$this->isSupportedLocale($locale)) { |
|
|
|
|
|
|
32
|
|
|
return $this->error("Unsupported locale: '{$locale}'."); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->displayRoutes($this->getLocaleRoutes($locale)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Compile the locale routes into a displayable format. |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function getLocaleRoutes($locale) |
|
44
|
|
|
{ |
|
45
|
|
|
$routes = $this->getFreshApplicationRoutes($locale); |
|
46
|
|
|
|
|
47
|
|
|
$routes = collect($routes)->map(function ($route) { |
|
48
|
|
|
return $this->getRouteInformation($route); |
|
49
|
|
|
})->filter()->all(); |
|
50
|
|
|
|
|
51
|
|
|
if ($sort = $this->option('sort')) { |
|
52
|
|
|
$routes = $this->sortRoutes($sort, $routes); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($this->option('reverse')) { |
|
56
|
|
|
$routes = array_reverse($routes); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->pluckColumns($routes); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Boot a fresh copy of the application and get the routes. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $locale |
|
66
|
|
|
* |
|
67
|
|
|
* @return \Illuminate\Routing\RouteCollection |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function getFreshApplicationRoutes($locale) |
|
70
|
|
|
{ |
|
71
|
|
|
$key = $this->getLocaleEnvKey(); |
|
72
|
|
|
putenv("{$key}={$locale}"); |
|
73
|
|
|
$app = require $this->getBootstrapPath().'/app.php'; |
|
74
|
|
|
$app->make(Kernel::class)->bootstrap(); |
|
75
|
|
|
$routes = $app['router']->getRoutes(); |
|
76
|
|
|
putenv("{$key}"); |
|
77
|
|
|
|
|
78
|
|
|
return $routes; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the console command arguments. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
|
|
|
|
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getArguments() |
|
87
|
|
|
{ |
|
88
|
|
|
return [ |
|
89
|
|
|
['locale', InputArgument::REQUIRED, 'The locale to list routes for.'], |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.