1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mcamara\LaravelLocalization\Commands; |
4
|
|
|
|
5
|
|
|
use Mcamara\LaravelLocalization\LaravelLocalization; |
6
|
|
|
use Mcamara\LaravelLocalization\Traits\TranslatedRouteCommandContext; |
7
|
|
|
use Illuminate\Contracts\Console\Kernel; |
8
|
|
|
use Illuminate\Foundation\Console\RouteListCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
|
11
|
|
|
class RouteTranslationsListCommand extends RouteListCommand |
12
|
|
|
{ |
13
|
|
|
use TranslatedRouteCommandContext; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'route:trans:list'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'List all registered routes for specific locales'; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Execute the console command. |
28
|
|
|
*/ |
29
|
|
|
public function handle() |
30
|
|
|
{ |
31
|
|
|
if (count($this->routes) == 0) { |
32
|
|
|
$this->error("Your application doesn't have any routes."); |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$locale = $this->argument('locale'); |
37
|
|
|
|
38
|
|
|
if ( ! $this->isSupportedLocale($locale)) { |
|
|
|
|
39
|
|
|
$this->error("Unsupported locale: '{$locale}'."); |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->routes = $this->getFreshApplicationRoutes($locale); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->displayRoutes($this->getRoutes()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Boot a fresh copy of the application and get the routes. |
50
|
|
|
* |
51
|
|
|
* @param string $locale |
52
|
|
|
* @return \Illuminate\Routing\RouteCollection |
53
|
|
|
*/ |
54
|
|
|
protected function getFreshApplicationRoutes($locale) |
55
|
|
|
{ |
56
|
|
|
$app = require $this->getBootstrapPath() . '/app.php'; |
57
|
|
|
|
58
|
|
|
$key = LaravelLocalization::ENV_ROUTE_KEY; |
59
|
|
|
|
60
|
|
|
putenv("{$key}={$locale}"); |
61
|
|
|
|
62
|
|
|
$app->make(Kernel::class)->bootstrap(); |
63
|
|
|
|
64
|
|
|
putenv("{$key}="); |
65
|
|
|
|
66
|
|
|
return $app['router']->getRoutes(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the console command arguments. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function getArguments() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
['locale', InputArgument::REQUIRED, 'The locale to list routes for.'], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.