1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\JsRoutes\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Routing\Router; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @see https://ideas.hexbridge.com/how-to-use-laravel-routes-in-javascript-4d9c484a0d97 |
12
|
|
|
* @see https://medium.com/@jonan.pineda/thank-you-for-this-post-d1d32c991757 |
13
|
|
|
*/ |
14
|
|
|
class GenerateJsRoutesCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'js-routes:generate |
22
|
|
|
{--p|path=resources/js/routes.json : JS routes file path.}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Generate routes for Javascript.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Application router. |
33
|
|
|
* |
34
|
|
|
* @var \Illuminate\Routing\Router |
35
|
|
|
*/ |
36
|
|
|
protected $router; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* File system. |
40
|
|
|
* |
41
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
42
|
|
|
*/ |
43
|
|
|
protected $file; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new command instance. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Routing\Router $router |
49
|
|
|
* @param \Illuminate\Filesystem\Filesystem $file |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Router $router, Filesystem $file) |
54
|
1 |
|
{ |
55
|
|
|
parent::__construct(); |
56
|
1 |
|
$this->router = $router; |
57
|
1 |
|
$this->file = $file; |
58
|
1 |
|
} |
59
|
1 |
|
|
60
|
|
|
/** |
61
|
|
|
* Execute the console command. |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function handle() |
66
|
1 |
|
{ |
67
|
|
|
$this->info('Generating routes for Javascript...'); |
68
|
1 |
|
|
69
|
|
|
$jsRoutes = collect($this->router->getRoutes()) |
70
|
1 |
|
->filter(function ($route) { |
71
|
|
|
return ! $this->matches(config('js-routes.excluded'), $route->uri); |
72
|
1 |
|
}) |
73
|
1 |
|
->reduce(function ($jsRoutes, $route) { |
74
|
|
|
$jsRoutes[$route->getName()] = $route->uri; |
75
|
1 |
|
|
76
|
1 |
|
return $jsRoutes; |
77
|
|
|
}, []); |
78
|
1 |
|
|
79
|
|
|
$path = $this->option('path'); |
80
|
1 |
|
|
81
|
|
|
$this->file->put($path, json_encode($jsRoutes, JSON_PRETTY_PRINT)); |
82
|
1 |
|
|
83
|
1 |
|
$this->info("Routes saved to '{$path}'."); |
84
|
|
|
|
85
|
|
|
$this->info("Run 'npm run dev' to update routes cache."); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Perform a regular expression match. |
90
|
|
|
* |
91
|
|
|
* @param array $patterns |
92
|
|
|
* @param string $subject |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
protected function matches($patterns, $subject) |
97
|
|
|
{ |
98
|
|
|
$isMatched = false; |
99
|
|
|
|
100
|
|
|
foreach ($patterns as $pattern) { |
101
|
|
|
if (preg_match($pattern, $subject)) { |
102
|
|
|
$isMatched = true; |
103
|
|
|
|
104
|
|
|
break; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $isMatched; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|