Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 707b0ac

Browse files
authored
Fix code style (#14)
1 parent 9be147a commit 707b0ac

File tree

1 file changed

+116
-116
lines changed

1 file changed

+116
-116
lines changed

src/Console/CreateJSRoutesCommand.php

Lines changed: 116 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -7,113 +7,113 @@
77

88
class CreateJSRoutesCommand extends Command
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-
{
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+
{
100100
$include = $this->getOption('include');
101101

102-
if(!empty($include)) {
102+
if (!empty($include)) {
103103
return Str::is($include, $routeName);
104104
}
105105

106106
$exclude = $this->getOption('exclude');
107107

108-
if(!empty($exclude)) {
108+
if (!empty($exclude)) {
109109
return !Str::is($exclude, $routeName);
110110
};
111111

112112
$methods = $this->getOption('methods');
113113

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)) {
117117
return true;
118118
}
119119
}
@@ -122,28 +122,28 @@ private function includeRoute($route, $routeName)
122122
}
123123

124124
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')))
145145
return config("app.jsroutes.$key", $default);
146146
else
147147
return config("jsroutes.$key", $default);
148-
}
148+
}
149149
}

0 commit comments

Comments
 (0)