Skip to content

Commit f3add26

Browse files
authored
Complete refactor (#12)
* Create CONTRIBUTING.MD * Use laravel filesystem * Fix cs * Update README.md * Fix bastianjoel pr errors Fixing truble into pull request * switched to STR str_plural * Removed unused dependencies * Fix timestamp not working * Refactoring PHPDoc & Added Str dependency Refactored PHPDoc and added dependency injection for \Illuminate\Support\String * Some style fix * Refactoring ApiCrudGenerator class Refactoring ApiCrudGenerator class for keeping number of methods under 10. This should help in fixing. Added Stub Class (Helper for parse & get stub) Added Generator Class (Helper for generating files) * Missing str dependency * Updated readme * Fix readme authors
1 parent 75a254d commit f3add26

File tree

12 files changed

+525
-223
lines changed

12 files changed

+525
-223
lines changed

CONTRIBUTING.MD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Contributing to Api Crud Generator
2+
Thank you for your interest in making Api-Crud-Generator better! There are several ways you can get involved.
3+
4+
## Reporting issues and suggesting new features
5+
If Api-Crud-Generator is not working properly, please open an issue and mark it as Bug. Please add steps to reproduce the bug.
6+
If you want to suggest new ideas, please mark it as enhancement.
7+
8+
## Finding issues you can help with
9+
10+
Looking for something to work on?
11+
[Issues marked *good first issue*](https://github.com/AndreaCivita/Laravel-ApiCrudGenerator/labels/good%20first%20issue)
12+
are a good place to start.
13+
14+
You can also check [the *help wanted* tag](https://github.com/AndreaCivita/Laravel-ApiCrudGenerator/labels/help%20wanted)
15+
to find other issues to help with.
16+
17+
18+
## Making changes to the code
19+
20+
Follow this steps for contributing:
21+
0) If you are fixing an issue, please assign to yourself, else go to step 1
22+
1) Fork this repo
23+
2) Apply all changes
24+
3) Write Tests. Test will help us so much!
25+
4) Make a pull request.
26+
27+
All pull request will be merged with 'dev' branch. The `master` branch should always be in a healthy state which is ready for release
28+
29+
### Submitting a pull request and participating in code review
30+
Writing a good description for your pull request is crucial to help reviewers and future
31+
maintainers understand your change. More detail is better.
32+
- [Link the issue you're addressing in the pull request](https://github.com/blog/957-introducing-issue-mentions).
33+
- Describe *why* the change is being made and *why* you've chosen a particular solution.
34+
- Describe any manual testing you performed to validate your change.
35+

README.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
# Laravel | API CRUD Generator
1+
<a href="https://codeclimate.com/github/andreacivita/Laravel-ApiCrudGenerator/maintainability"><img src="https://api.codeclimate.com/v1/badges/e22398ed005890048cb5/maintainability" /></a>
2+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/9f34629292c94cbbb29cc6de75465b24)](https://app.codacy.com/app/andreacivita/Laravel-ApiCrudGenerator?utm_source=github.com&utm_medium=referral&utm_content=andreacivita/Laravel-ApiCrudGenerator&utm_campaign=Badge_Grade_Dashboard)
3+
[![Latest Stable Version](https://poser.pugx.org/andreacivita/api-crud-generator/v/stable)](https://packagist.org/packages/andreacivita/api-crud-generator)
4+
[![License](https://poser.pugx.org/andreacivita/api-crud-generator/license)](https://packagist.org/packages/andreacivita/api-crud-generator)
25

3-
## DEV Branch with experimental features
6+
# Laravel | API CRUD Generator
47

58
This Generator package provides generators of Models, Controllers, Request, Routes & Tests for a painless development.
69

@@ -42,7 +45,7 @@ No further options required. Your setup is complete!
4245

4346
### Interactive mode
4447

45-
You can manage a single table with interactive mode or manually (see below).
48+
You can manage a single table with interactive mode or manually (see [next paragraph](https://github.com/andreacivita/Laravel-ApiCrudGenerator#managing-a-single-db-table)).
4649

4750
Just run
4851

@@ -87,36 +90,58 @@ This will set 'timestamps=true' in Model class.
8790

8891
## ROUTING
8992

90-
I developed this package for myself. So, default generation of API Routes will follow this schema:
93+
Routes will follow Route::resource() Schema (default routing schema provided by Laravel).
94+
9195
Example: i'm generating Car crud
9296

9397
| Route | Method | Operation |
9498
| ------------- |:----------------:| ----------------:|
95-
| cars | GET | Get all cars |
99+
| car | GET | Get all cars |
96100
| car/{id} | GET | Find car by id |
97-
| car | PUT | Insert a new car |
98-
| car/{id} | PATCH | Update car by id |
101+
| car | POST | Insert a new car |
102+
| car/{id} | PUT / PATCH | Update car by id |
99103
| car/{id} | DELETE | Delete car by id |
100104

105+
101106
Remember that all api routes have 'api/' prefix.
102107

103108
## TESTING
104109

105-
When created CRUD structure (Controllers, Models, Request & Routes), this package generate Unit test file. <br>
106-
**IMPORTANT** : Testing insert and update functions are not yet implemented.
110+
When created CRUD structure (Controllers, Models, Request, Resource, Factory & Routes), this package generate Feature test file. <br>
111+
112+
<hr>
113+
114+
## SETUP GENERATED CRUD
115+
116+
### FACTORIES
107117

118+
Tests now require factory class to manipulate data.
119+
You should provide data schema into your factory class (using Faker), so you'll be able to test easily your API
120+
121+
### VALIDATORS
122+
123+
Write better code will helps you so much! So, default behavior of Controllers is to force you to use validated data.
124+
You have to set all validation rules into your FormRequest class.
125+
126+
E.g. For Car Crud, you will set rules into your App\Request\CarRequest.php
127+
128+
<hr>
108129
## CONTRIBUTING
109130

110-
This package is covered by LGPL license. You are able to do whatever you want with this code.
131+
This package is covered by MIT license. You are able to do whatever you want with this code.
111132

112133
Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.
113134
You can see issues or enhancement and assign task for contributing :)
114135

115-
**Remember that all modifications must be under the same license (or GNU GPLv3).**
116136

117137
## How can I thank you?
118138

119139
Star this repo or follow me on GitHub. And, if you want, you can share this link! :)
120140

141+
<hr>
142+
143+
## AUTHORS
121144

145+
This package has been originally developed by [Andrea Civita](https://github.com/andreacivita)<br>
146+
A special thanks goes to [Bastianjoel](https://github.com/bastianjoel) for it's pull request
122147

src/Commands/ApiCrudGenerator.php

Lines changed: 40 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace AndreaCivita\ApiCrudGenerator\Commands;
44

5+
use AndreaCivita\ApiCrudGenerator\Generator;
56
use Illuminate\Console\Command;
67
use Illuminate\Database\QueryException;
7-
use Illuminate\Support\Facades\Artisan;
88
use Illuminate\Support\Facades\DB;
9-
use Illuminate\Support\Facades\File;
109
use Illuminate\Support\Facades\Schema;
10+
use Illuminate\Support\Str;
1111

1212
class ApiCrudGenerator extends Command
1313
{
@@ -31,20 +31,40 @@ class ApiCrudGenerator extends Command
3131
*/
3232
protected $description = 'Create CRUD operations';
3333

34+
/**
35+
*
36+
* Generator support instance
37+
*
38+
* @var \AndreaCivita\ApiCrudGenerator\Generator
39+
*/
40+
protected $generator;
41+
42+
43+
/**
44+
* The String support instance
45+
*
46+
* @var \Illuminate\Support\Str
47+
*/
48+
protected $str;
49+
3450
/**
3551
* Create a new command instance.
3652
*
37-
* @return void
53+
* @param Generator $generator
54+
* @param Str $str
3855
*/
39-
public function __construct()
56+
public function __construct(Generator $generator, Str $str)
4057
{
4158
parent::__construct();
59+
$this->generator = $generator;
60+
$this->str = $str;
4261
}
4362

4463
/**
4564
* Execute the console command.
4665
*
4766
* @return mixed
67+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
4868
*/
4969
public function handle()
5070
{
@@ -68,140 +88,10 @@ public function handle()
6888
return 0;
6989
}
7090

71-
/**
72-
* Get the file from the stub
73-
* @param $type
74-
* @return bool|string
75-
*/
76-
protected function getStub($type)
77-
{
78-
return file_get_contents(resource_path("stubs/$type.stub"));
79-
}
80-
81-
82-
/**
83-
* Generate model class from stubs
84-
* @param $name string name of model class
85-
* @param $table string name of DB table
86-
* @param $timestamps boolean set timestamps true | false
87-
*/
88-
protected function model($name, $table, $timestamps)
89-
{
90-
$table === "default" ? $table = strtolower(str_plural($name)) : null;
91-
$timeDeclaration = 'public $timestamps = false;';
92-
if ($timestamps == "true")
93-
$timeDeclaration = 'public $timestamps = true;';
94-
$modelTemplate = str_replace(
95-
[
96-
'{{modelName}}',
97-
'{{tableDeclaration}}',
98-
'{{timestamps}}'
99-
],
100-
[
101-
$name,
102-
$table,
103-
$timeDeclaration,
104-
],
105-
$this->getStub('Model')
106-
);
107-
108-
if (!file_exists($path = app_path('/Model')))
109-
mkdir($path, 0777, true);
110-
111-
112-
file_put_contents(app_path("Model/{$name}.php"), $modelTemplate);
113-
}
114-
115-
/**
116-
* Create controller from controller.stub
117-
* @param $name
118-
*/
119-
protected function controller($name)
120-
{
121-
$controllerTemplate = str_replace(
122-
[
123-
'{{modelName}}',
124-
'{{modelNamePluralLowerCase}}',
125-
'{{modelNameSingularLowerCase}}'
126-
],
127-
[
128-
$name,
129-
strtolower(str_plural($name)),
130-
strtolower($name)
131-
],
132-
$this->getStub('Controller')
133-
);
134-
135-
file_put_contents(app_path("/Http/Controllers/{$name}Controller.php"), $controllerTemplate);
136-
}
137-
138-
/**
139-
* Generate Request from request.stub
140-
* @param $name
141-
*/
142-
protected function request($name)
143-
{
144-
$requestTemplate = str_replace(
145-
['{{modelName}}'],
146-
[$name],
147-
$this->getStub('Request')
148-
);
149-
150-
if (!file_exists($path = app_path('/Http/Requests')))
151-
mkdir($path, 0777, true);
152-
153-
file_put_contents(app_path("/Http/Requests/{$name}Request.php"), $requestTemplate);
154-
}
155-
156-
/**
157-
* Generate routes
158-
* @param $name
159-
*/
160-
protected function routes($name, $table)
161-
{
162-
$table === "default" ? $table = strtolower(str_plural($name)) : null;
163-
$requestTemplate = str_replace(
164-
[
165-
'{{modelName}}',
166-
'{{modelNamePluralLowerCase}}',
167-
'{{modelNameSingularLowerCase}}'
168-
],
169-
[
170-
$name,
171-
$table,
172-
strtolower($name)
173-
],
174-
$this->getStub('Routes')
175-
);
176-
File::append(base_path('routes/api.php'), $requestTemplate);
177-
}
178-
179-
/**
180-
* Generate unit test
181-
* @param $name
182-
* @param $table
183-
*/
184-
protected function test($name, $table)
185-
{
186-
187-
$testTemplate = str_replace(
188-
[
189-
'{{modelName}}',
190-
'{{modelNamePluralLowerCase}}',
191-
'{{modelNameSingularLowerCase}}',
192-
],
193-
[
194-
$name,
195-
$table,
196-
strtolower($name)
197-
],
198-
$this->getStub('Test')
199-
);
200-
File::append(base_path("tests/Unit/{$name}Test.php"), $testTemplate);
201-
}
20291

20392
/**
20493
* Generate CRUD in interactive mode
94+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
20595
*/
20696
protected function interactive()
20797
{
@@ -210,9 +100,9 @@ protected function interactive()
210100
$this->comment("This command will guide you through creating your CRUD");
211101
$name = $this->ask('What is name of your Model?');
212102
$name = ucwords($name);
213-
$table = $this->ask("Table name [" . strtolower(str_plural($name)) . "]:");
103+
$table = $this->ask("Table name [" . strtolower($this->str->plural($name)) . "]:");
214104
if ($table == "")
215-
$table = str_plural($name);
105+
$table = $this->str->plural($name);
216106
$table = strtolower($table);
217107
$choice = $this->choice('Do your table has timestamps column?', ['No', 'Yes'], 0);
218108
$choice === "Yes" ? $timestamps = true : $timestamps = false;
@@ -224,7 +114,7 @@ protected function interactive()
224114
$confirm = $this->ask("Press y to confirm, type N to restart");
225115
if ($confirm == "y") {
226116
$this->generate($name, $table, $timestamps);
227-
die;
117+
return;
228118
}
229119
$this->error("Aborted!");
230120

@@ -237,24 +127,30 @@ protected function interactive()
237127
* @param $name string Model Name
238128
* @param $table string Table Name
239129
* @param $timestamps boolean
130+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
240131
*/
241132
protected function generate($name, $table, $timestamps)
242133
{
243-
$this->controller($name);
134+
$this->generator->controller($name);
244135
$this->info("Generated Controller!");
245-
$this->model($name, $table, $timestamps);
136+
$this->generator->model($name, $table, $timestamps);
246137
$this->info("Generated Model!");
247-
$this->request($name);
138+
$this->generator->request($name);
248139
$this->info("Generated Request!");
249-
$this->routes($name, $table);
140+
$this->generator->resource($name);
141+
$this->info("Generated Resource!");
142+
$this->generator->routes($name);
250143
$this->info("Generated routes!");
251-
$this->test($name, $table);
144+
$this->generator->factory($name);
145+
$this->info("Generated Factory!");
146+
$this->generator->test($name);
252147
$this->info("Generated Test!");
253148
}
254149

255150

256151
/**
257152
* Handle all-db generation
153+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
258154
*/
259155
protected function all()
260156
{
@@ -264,7 +160,7 @@ protected function all()
264160
$this->comment("Generating " . $table->Tables_in_crud . " CRUD");
265161
$columns = Schema::getColumnListing($table->Tables_in_crud);
266162
$table = $table->Tables_in_crud;
267-
$name = ucwords(str_singular($table));
163+
$name = ucwords($this->str->singular($table));
268164
in_array('created_at', $columns) ? $timestamps = true : $timestamps = false;
269165
$this->generate($name, $table, $timestamps);
270166
}

0 commit comments

Comments
 (0)