Skip to content

Commit 199c06e

Browse files
author
Andrea Civita
committed
Merge branch 'hotfix/hotfix-languages'
2 parents 63545ef + d721e80 commit 199c06e

File tree

9 files changed

+57
-58
lines changed

9 files changed

+57
-58
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
*.iml
33
vendor/
44
composer.lock
5-
.php_cs.cache
5+
.php_cs.cache
6+
.phpunit.result.cache

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: php
33
php:
44
- 7.3
55
- 7.4
6-
-8.0
6+
- 8.0
77

88
before_script:
99
- composer self-update

src/Commands/ApiCrudGenerator.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AndreaCivita\ApiCrudGenerator\Core\Generator;
66
use Doctrine\DBAL\Driver\PDOException;
77
use Illuminate\Console\Command;
8+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
89
use Illuminate\Database\QueryException;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\Schema;
@@ -37,15 +38,15 @@ class ApiCrudGenerator extends Command
3738
*
3839
* Generator support instance
3940
*
40-
* @var \AndreaCivita\ApiCrudGenerator\Core\Generator
41+
* @var Generator
4142
*/
4243
protected $generator;
4344

4445

4546
/**
4647
* The String support instance
4748
*
48-
* @var \Illuminate\Support\Str
49+
* @var Str
4950
*/
5051
protected $str;
5152

@@ -58,7 +59,7 @@ class ApiCrudGenerator extends Command
5859
/**
5960
* Schema support instance
6061
*
61-
* @var \Illuminate\Support\Facades\Schema $schema
62+
* @var Schema $schema
6263
*/
6364
protected $schema;
6465

@@ -80,10 +81,9 @@ public function __construct(Generator $generator, Str $str, Schema $schema)
8081
/**
8182
* Execute the console command.
8283
*
83-
* @return mixed
84-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
84+
* @return int
8585
*/
86-
public function handle()
86+
public function handle() : int
8787
{
8888
// Checking interactive mode
8989
if ($this->option('interactive') == "") {
@@ -113,7 +113,6 @@ public function handle()
113113

114114
/**
115115
* Handle all-db generation
116-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
117116
*/
118117
protected function all()
119118
{
@@ -126,18 +125,16 @@ protected function all()
126125
in_array('created_at', $columns) ? $timestamps = true : $timestamps = false;
127126
$this->generate($name, $table, $timestamps);
128127
}
129-
}
130-
catch (QueryException $exception) {
128+
} catch (QueryException $exception) {
131129
$this->error("Error: " . $exception->getMessage());
132130
}
133131
}
134132

135133

136134
/**
137135
* Generate CRUD in interactive mode
138-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
139136
*/
140-
protected function interactive()
137+
protected function interactive() : void
141138
{
142139
$this->info("Welcome in Interactive mode");
143140

@@ -170,26 +167,27 @@ protected function interactive()
170167
* @param $name string Model Name
171168
* @param $table string Table Name
172169
* @param $timestamps boolean
173-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
174170
*/
175-
protected function generate($name, $table, $timestamps)
171+
protected function generate(string $name, string $table, bool $timestamps)
176172
{
177-
$this->generator->controller($name);
173+
$this->generator->controller($name, $table);
178174
$this->info("Generated Controller!");
175+
179176
$this->generator->model($name, $table, $timestamps);
180177
$this->info("Generated Model!");
178+
181179
$this->generator->request($name);
182180
$this->info("Generated Request!");
181+
183182
$this->generator->resource($name);
184183
$this->info("Generated Resource!");
185-
if ($this->passport) {
186-
$this->generator->secureRoutes($name);
187-
} else {
188-
$this->generator->routes($name);
189-
}
184+
185+
$this->passport ? $this->generator->secureRoutes($name) : $this->generator->routes($name);
190186
$this->info("Generated routes!");
187+
191188
$this->generator->factory($name);
192189
$this->info("Generated Factory!");
190+
193191
$this->generator->test($name);
194192
$this->info("Generated Test!");
195193
}

src/Core/Generator.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AndreaCivita\ApiCrudGenerator\Core;
44

5+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
56
use Illuminate\Filesystem\Filesystem;
67
use Illuminate\Support\Str;
78

@@ -11,21 +12,21 @@ class Generator
1112
/**
1213
* The filesystem instance.
1314
*
14-
* @var \Illuminate\Filesystem\Filesystem
15+
* @var Filesystem
1516
*/
1617
protected $files;
1718

1819
/**
1920
* The String support instance
2021
*
21-
* @var \Illuminate\Support\Str
22+
* @var Str
2223
*/
2324
protected $str;
2425

2526
/**
2627
* The Stub support instance
2728
*
28-
* @var \AndreaCivita\ApiCrudGenerator\Core\Stub;
29+
* @var Stub;
2930
*/
3031
protected $stub;
3132

@@ -44,7 +45,7 @@ public function __construct(Filesystem $files, Str $str, Stub $stub)
4445
* @param $timestamps boolean set timestamps true | false
4546
* @return bool|int
4647
*/
47-
public function model($name, $table, $timestamps)
48+
public function model(string $name, string $table, bool $timestamps)
4849
{
4950
$table === "default" ? $table = strtolower($this->str->plural($name)) : null;
5051

@@ -68,23 +69,23 @@ public function model($name, $table, $timestamps)
6869
* Create controller from controller.stub
6970
*
7071
* @param $name string name of model class
72+
* @param $table string name of db table
7173
* @return bool|int
7274
*/
73-
public function controller($name)
75+
public function controller(string $name, string $table)
7476
{
75-
$content = $this->stub->parseStub('Controller', $name);
77+
$content = $this->stub->parseStub('Controller', $name, ['table' => $table]);
7678

7779
return $this->files->put("app/Http/Controllers/{$name}Controller.php", $content);
7880
}
7981

8082
/**
8183
* Generate Request from request.stub
8284
*
83-
* @param $name
85+
* @param $name string
8486
* @return bool|int
85-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
8687
*/
87-
public function request($name)
88+
public function request(string $name)
8889
{
8990
$content = $this->stub->parseStub('Request', $name);
9091

@@ -99,7 +100,6 @@ public function request($name)
99100
*
100101
* @param $name
101102
* @return bool|int
102-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
103103
*/
104104
public function resource($name)
105105
{
@@ -114,11 +114,10 @@ public function resource($name)
114114
/**
115115
* Generate factory from Factory.stub
116116
*
117-
* @param $name
117+
* @param $name string
118118
* @return int
119-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
120119
*/
121-
public function factory($name)
120+
public function factory(string $name): int
122121
{
123122
$content = $this->stub->parseStub('Factory', $name);
124123

@@ -133,7 +132,6 @@ public function factory($name)
133132
*
134133
* @param $name
135134
* @return int
136-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
137135
*/
138136
public function routes($name)
139137
{
@@ -143,11 +141,10 @@ public function routes($name)
143141
}
144142

145143
/**
146-
* @param $name
144+
* @param $name string
147145
* @return int
148-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
149146
*/
150-
public function secureRoutes($name)
147+
public function secureRoutes(string $name) : int
151148
{
152149
$content = $this->stub->parseStub('Passport-Routes', $name);
153150

@@ -157,11 +154,10 @@ public function secureRoutes($name)
157154
/**
158155
* Generate unit test
159156
*
160-
* @param $name
157+
* @param $name string
161158
* @return int
162-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
163159
*/
164-
public function test($name)
160+
public function test(string $name) : int
165161
{
166162
$content = $this->stub->parseStub('Test', $name);
167163

src/Core/Stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ protected function getStub($type)
6969
* @param $args array additional placeholders to replace
7070
* @return mixed
7171
*/
72-
public function parseStub($stub, $name, $args = [])
72+
public function parseStub(string $stub, string $name, array $args = [])
7373
{
7474
$toParse = array_merge([
7575
'modelName' => $name,
76-
'modelNamePluralLowerCase' => strtolower($this->str->plural($name)),
76+
'modelNamePluralLowerCase' => $args['table'] ?? strtolower($this->str->plural($name)),
7777
'modelNameSingularLowerCase' => strtolower($name)
7878
], $args);
7979

src/stubs/Controller.stub

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ namespace App\Http\Controllers;
55
use App\Models\{{modelName}};
66
use App\Http\Requests\{{modelName}}Request;
77
use App\Http\Resources\{{modelName}}Resource;
8+
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
9+
use Illuminate\Http\JsonResponse;
10+
811

912
class {{modelName}}Controller extends Controller
1013
{
1114
/**
1215
* Display a listing of the resource.
1316
*
14-
* @return \Illuminate\Http\Response
17+
* @return AnonymousResourceCollection
1518
*/
1619
public function index()
1720
{
@@ -23,8 +26,8 @@ class {{modelName}}Controller extends Controller
2326
/**
2427
* Store a newly created resource in storage.
2528
*
26-
* @param \App\Http\Requests\{{modelName}}Request $request
27-
* @return \Illuminate\Http\Response
29+
* @param {{modelName}}Request $request
30+
* @return {{modelName}}Resource
2831
*/
2932
public function store({{modelName}}Request $request)
3033
{
@@ -36,8 +39,8 @@ class {{modelName}}Controller extends Controller
3639
/**
3740
* Display the specified resource.
3841
*
39-
* @param \App\Models\{{modelName}} ${{modelNameSingularLowerCase}}
40-
* @return \Illuminate\Http\Response
42+
* @param {{modelName}} ${{modelNameSingularLowerCase}}
43+
* @return {{modelName}}Resource
4144
*/
4245
public function show({{modelName}} ${{modelNameSingularLowerCase}})
4346
{
@@ -49,7 +52,7 @@ class {{modelName}}Controller extends Controller
4952
*
5053
* @param \App\Http\Requests\{{modelName}}Request $request
5154
* @param \App\Models\{{modelName}} ${{modelNameSingularLowerCase}}
52-
* @return \Illuminate\Http\Response
55+
* @return {{modelName}}Resource
5356
*/
5457
public function update({{modelName}}Request $request, {{modelName}} ${{modelNameSingularLowerCase}})
5558
{
@@ -62,7 +65,7 @@ class {{modelName}}Controller extends Controller
6265
* Remove the specified resource from storage.
6366
*
6467
* @param \App\Models\{{modelName}} ${{modelNameSingularLowerCase}}
65-
* @return \Illuminate\Http\Response
68+
* @return JsonResponse
6669
*/
6770
public function destroy({{modelName}} ${{modelNameSingularLowerCase}})
6871
{

src/stubs/Factory.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Illuminate\Database\Eloquent\Factories\Factory;
4-
use Illuminate\Support\Str;
4+
use App\Models\{{modelName}};
55

66
class {{modelName}}Factory extends Factory
77
{
@@ -10,7 +10,7 @@ class {{modelName}}Factory extends Factory
1010
*
1111
* @var string
1212
*/
13-
protected $model = \App\Models\{{modelName}}::class;
13+
protected $model = {{modelName}}::class;
1414

1515
/**
1616
* Define the model's default state.

src/stubs/Resource.stub

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace App\Http\Resources;
44

5+
use Illuminate\Http\Request;
56
use Illuminate\Http\Resources\Json\JsonResource;
67

78
class {{modelName}}Resource extends JsonResource
89
{
910
/**
1011
* Transform the resource into an array.
1112
*
12-
* @param \Illuminate\Http\Request $request
13+
* @param Request $request
1314
* @return array
1415
*/
1516
public function toArray($request)

0 commit comments

Comments
 (0)