7
7
8
8
class CreateJSRoutesCommand extends Command
9
9
{
10
- /**
11
- * The name and signature of the console command.
12
- *
13
- * @var string
14
- */
15
- protected $ signature = "route:tojs
16
- { --name=routes.js : Name of the output file. }
17
- { --p|path= : Path of the output file. }
18
- { --i|include= : List of comma separated route names to include (overrides exclude and methods). }
19
- { --e|exclude= : List of comma separated route names to exclude (overrides methods). }
20
- { --m|methods= : List of comma separated methods accepted by filter. Empty for include all methods. }
21
- { --f|force : Overwrite existing routes by default. } " ;
22
-
23
- /**
24
- * The console command description.
25
- *
26
- * @var string
27
- */
28
- protected $ description = "Create object with routes, and a function for its JS use. Uses key from configuration file 'jsroutes'. " ;
29
-
30
- /**
31
- * Create a new command instance.
32
- *
33
- * @return void
34
- */
35
- public function __construct ()
36
- {
37
- parent ::__construct ();
38
- }
39
-
40
- /**
41
- * Execute the console command.
42
- *
43
- * @return mixed
44
- */
45
- public function handle ()
46
- {
47
- $ routes = $ this ->getRoutesArray ();
48
-
49
- $ jsonFlags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ;
50
-
51
- $ content = 'const routes = ' ;
52
- $ content .= json_encode ($ routes , $ jsonFlags );
53
- $ content .= "; \n\n" ;
54
-
55
- $ content .= file_get_contents (
56
- __DIR__ . '/../assets/js/routeFunction.js '
57
- );
58
-
59
- $ fileName = $ this ->option ('name ' ) ?? config ('app.jsroutes.name ' );
60
- if ($ this ->createFile ($ fileName , $ content )) {
61
- $ this ->info ("$ fileName created " );
62
- }
63
- }
64
-
65
- public function getRoutesArray (): array
66
- {
67
- return collect (
68
- app ('router ' )->getRoutes ()->getRoutesByName ()
69
- )->filter (function ($ route , $ key ) {
70
- return $ this ->includeRoute ($ route , $ key );
71
- })->map (function ($ route ) {
72
- return [
73
- 'uri ' => $ route ->uri
74
- ];
75
- })->toArray ();
76
- }
77
-
78
- public function createFile ($ fileName , $ contents )
79
- {
80
- if (
81
- file_exists ($ file = $ this ->getJSPath ($ fileName )) &&
82
- !$ this ->option ('force ' )
83
- ) {
84
- if (
85
- !$ this ->confirm (
86
- "The [ $ fileName] file already exists. Do you want to replace it? "
87
- )
88
- ) {
89
- $ this ->error ('Error ' );
90
- return false ;
91
- }
92
- }
93
-
94
- file_put_contents ($ file , $ contents );
95
- return true ;
96
- }
97
-
98
- private function includeRoute ($ route , $ routeName )
99
- {
10
+ /**
11
+ * The name and signature of the console command.
12
+ *
13
+ * @var string
14
+ */
15
+ protected $ signature = "route:tojs
16
+ { --name=routes.js : Name of the output file. }
17
+ { --p|path= : Path of the output file. }
18
+ { --i|include= : List of comma separated route names to include (overrides exclude and methods). }
19
+ { --e|exclude= : List of comma separated route names to exclude (overrides methods). }
20
+ { --m|methods= : List of comma separated methods accepted by filter. Empty for include all methods. }
21
+ { --f|force : Overwrite existing routes by default. } " ;
22
+
23
+ /**
24
+ * The console command description.
25
+ *
26
+ * @var string
27
+ */
28
+ protected $ description = "Create object with routes, and a function for its JS use. Uses key from configuration file 'jsroutes'. " ;
29
+
30
+ /**
31
+ * Create a new command instance.
32
+ *
33
+ * @return void
34
+ */
35
+ public function __construct ()
36
+ {
37
+ parent ::__construct ();
38
+ }
39
+
40
+ /**
41
+ * Execute the console command.
42
+ *
43
+ * @return mixed
44
+ */
45
+ public function handle ()
46
+ {
47
+ $ routes = $ this ->getRoutesArray ();
48
+
49
+ $ jsonFlags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ;
50
+
51
+ $ content = 'const routes = ' ;
52
+ $ content .= json_encode ($ routes , $ jsonFlags );
53
+ $ content .= "; \n\n" ;
54
+
55
+ $ content .= file_get_contents (
56
+ __DIR__ . '/../assets/js/routeFunction.js '
57
+ );
58
+
59
+ $ fileName = $ this ->option ('name ' ) ?? config ('app.jsroutes.name ' );
60
+ if ($ this ->createFile ($ fileName , $ content )) {
61
+ $ this ->info ("$ fileName created " );
62
+ }
63
+ }
64
+
65
+ public function getRoutesArray (): array
66
+ {
67
+ return collect (
68
+ app ('router ' )->getRoutes ()->getRoutesByName ()
69
+ )->filter (function ($ route , $ key ) {
70
+ return $ this ->includeRoute ($ route , $ key );
71
+ })->map (function ($ route ) {
72
+ return [
73
+ 'uri ' => $ route ->uri
74
+ ];
75
+ })->toArray ();
76
+ }
77
+
78
+ public function createFile ($ fileName , $ contents )
79
+ {
80
+ if (
81
+ file_exists ($ file = $ this ->getJSPath ($ fileName )) &&
82
+ !$ this ->option ('force ' )
83
+ ) {
84
+ if (
85
+ !$ this ->confirm (
86
+ "The [ $ fileName] file already exists. Do you want to replace it? "
87
+ )
88
+ ) {
89
+ $ this ->error ('Error ' );
90
+ return false ;
91
+ }
92
+ }
93
+
94
+ file_put_contents ($ file , $ contents );
95
+ return true ;
96
+ }
97
+
98
+ private function includeRoute ($ route , $ routeName )
99
+ {
100
100
$ include = $ this ->getOption ('include ' );
101
101
102
- if (!empty ($ include )) {
102
+ if (!empty ($ include )) {
103
103
return Str::is ($ include , $ routeName );
104
104
}
105
105
106
106
$ exclude = $ this ->getOption ('exclude ' );
107
107
108
- if (!empty ($ exclude )) {
108
+ if (!empty ($ exclude )) {
109
109
return !Str::is ($ exclude , $ routeName );
110
110
};
111
111
112
112
$ methods = $ this ->getOption ('methods ' );
113
113
114
- if (!empty ($ methods )) {
115
- foreach ($ route ->methods as $ method ) {
116
- if (Str::is ($ methods , $ method )) {
114
+ if (!empty ($ methods )) {
115
+ foreach ($ route ->methods as $ method ) {
116
+ if (Str::is ($ methods , $ method )) {
117
117
return true ;
118
118
}
119
119
}
@@ -122,28 +122,28 @@ private function includeRoute($route, $routeName)
122
122
}
123
123
124
124
return true ;
125
- }
126
-
127
- protected function getJSPath ($ fileName )
128
- {
129
- $ path = $ this ->getOption (
130
- 'path ' ,
131
- config ('js.path ' )[0 ] ?? resource_path ('js ' )
132
- );
133
-
134
- return implode (DIRECTORY_SEPARATOR , [$ path , $ fileName ]);
135
- }
136
-
137
- protected function getOption (string $ key , $ default = null )
138
- {
139
- if ($ this ->option ($ key )) {
140
- return explode (', ' , $ this ->option ($ key ));
141
- }
142
-
143
- //Retro compatibility
144
- if (empty (config ('jsroutes ' )))
125
+ }
126
+
127
+ protected function getJSPath ($ fileName )
128
+ {
129
+ $ path = $ this ->getOption (
130
+ 'path ' ,
131
+ config ('js.path ' )[0 ] ?? resource_path ('js ' )
132
+ );
133
+
134
+ return implode (DIRECTORY_SEPARATOR , [$ path , $ fileName ]);
135
+ }
136
+
137
+ protected function getOption (string $ key , $ default = null )
138
+ {
139
+ if ($ this ->option ($ key )) {
140
+ return explode (', ' , $ this ->option ($ key ));
141
+ }
142
+
143
+ //Retro compatibility
144
+ if (empty (config ('jsroutes ' )))
145
145
return config ("app.jsroutes. $ key " , $ default );
146
146
else
147
147
return config ("jsroutes. $ key " , $ default );
148
- }
148
+ }
149
149
}
0 commit comments