Skip to content

Commit 6e32531

Browse files
andreacivitaAndrea CivitaMax VanRaymaxvrdev
authored
Refactor + small fixes (#37)
* Update .travis.yml * Revert "Update .travis.yml" This reverts commit 6c54da4. * Update GeneratorTest.php * feat: Hard Refactor (#29) * Updated stub-test * New Generators * Added dependency Support * Removed unused param & added setData * commands depends on new generators Old generator has been deprecated * Updated PHPUnit test suite * ControllerGenerator + tests * FactoryGenerator + test * ModelGenerator + test * RequestGenerator + test * ResourceGenerator + test * RouteGenerator + tests * fix: typo in ModelGenerator * TestGenerator + test * Stub + test * updated .gitignore * Php-cs-fixer Co-authored-by: Andrea Civita <[email protected]> * Namespace Added to Factory Stub (#33) (#36) Co-authored-by: Max VanRay <[email protected]> * - Optimized all the stubs and docblocks (#35) * - Optimized all the stubs and docblocks * Add Import to Model Stub * Remove the import from the Model Stub Clean up the imports on the Controller Stub Related to issue #32 Co-authored-by: [email protected] <[email protected]> Co-authored-by: Andrea Civita <[email protected]> Co-authored-by: Max VanRay <[email protected]> Co-authored-by: Max VanReynegom <[email protected]>
1 parent 199c06e commit 6e32531

27 files changed

+965
-81
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
vendor/
44
composer.lock
55
.php_cs.cache
6-
.phpunit.result.cache
6+
.phpunit.result.cache
7+
app/
8+
database/
9+
routes/

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"require": {
1313
"php": "^7.3|^8.0",
1414
"laravel/framework": "^8.0",
15-
"doctrine/dbal": "^2.9"
15+
"doctrine/dbal": "^2.9",
16+
"illuminate/support": "^8.61"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "^7|^8",

phpunit.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<testsuite name="Api Crud Generator test-suite">
55
<directory suffix="Test.php">./tests</directory>
66
</testsuite>
7+
<testsuite name="Unit">
8+
<directory suffix="Test.php">./tests/Unit</directory>
9+
</testsuite>
10+
<testsuite name="Feature">
11+
<directory suffix="Test.php">./tests/Feature</directory>
12+
</testsuite>
713
</testsuites>
814
<filter>
915
<whitelist processUncoveredFilesFromWhitelist="true">
@@ -16,4 +22,4 @@
1622
</exclude>
1723
</whitelist>
1824
</filter>
19-
</phpunit>
25+
</phpunit>

src/Commands/ApiCrudGenerator.php

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
namespace AndreaCivita\ApiCrudGenerator\Commands;
44

55
use AndreaCivita\ApiCrudGenerator\Core\Generator;
6+
use AndreaCivita\ApiCrudGenerator\Core\Generators\ControllerGenerator;
7+
use AndreaCivita\ApiCrudGenerator\Core\Generators\FactoryGenerator;
8+
use AndreaCivita\ApiCrudGenerator\Core\Generators\ModelGenerator;
9+
use AndreaCivita\ApiCrudGenerator\Core\Generators\RequestGenerator;
10+
use AndreaCivita\ApiCrudGenerator\Core\Generators\ResourceGenerator;
11+
use AndreaCivita\ApiCrudGenerator\Core\Generators\RouteGenerator;
12+
use AndreaCivita\ApiCrudGenerator\Core\Generators\TestGenerator;
613
use Doctrine\DBAL\Driver\PDOException;
714
use Illuminate\Console\Command;
815
use Illuminate\Contracts\Filesystem\FileNotFoundException;
@@ -42,6 +49,42 @@ class ApiCrudGenerator extends Command
4249
*/
4350
protected $generator;
4451

52+
/**
53+
* @var ControllerGenerator $controller
54+
*/
55+
protected $controller;
56+
57+
/**
58+
* @var FactoryGenerator $factory
59+
*/
60+
protected $factory;
61+
62+
/**
63+
* @var ModelGenerator $model
64+
*/
65+
protected $model;
66+
67+
/**
68+
* @var RequestGenerator $request
69+
*/
70+
protected $request;
71+
72+
/**
73+
* @var ResourceGenerator $resource
74+
*/
75+
protected $resource;
76+
77+
/**
78+
* @var RouteGenerator $route
79+
*/
80+
protected $route;
81+
82+
83+
/**
84+
* @var TestGenerator $test
85+
*/
86+
protected $test;
87+
4588

4689
/**
4790
* The String support instance
@@ -66,14 +109,35 @@ class ApiCrudGenerator extends Command
66109
/**
67110
* Create a new command instance.
68111
*
69-
* @param Generator $generator
112+
* @param ControllerGenerator $controllerGenerator
113+
* @param FactoryGenerator $factoryGenerator
114+
* @param ModelGenerator $modelGenerator
115+
* @param RequestGenerator $requestGenerator
116+
* @param ResourceGenerator $resourceGenerator
117+
* @param RouteGenerator $routeGenerator
118+
* @param TestGenerator $testGenerator
70119
* @param Str $str
71120
* @param Schema $schema
72121
*/
73-
public function __construct(Generator $generator, Str $str, Schema $schema)
74-
{
122+
public function __construct(
123+
ControllerGenerator $controllerGenerator,
124+
FactoryGenerator $factoryGenerator,
125+
ModelGenerator $modelGenerator,
126+
RequestGenerator $requestGenerator,
127+
ResourceGenerator $resourceGenerator,
128+
RouteGenerator $routeGenerator,
129+
TestGenerator $testGenerator,
130+
Str $str,
131+
Schema $schema
132+
) {
75133
parent::__construct();
76-
$this->generator = $generator;
134+
$this->controller = $controllerGenerator;
135+
$this->factory = $factoryGenerator;
136+
$this->model = $modelGenerator;
137+
$this->request = $requestGenerator;
138+
$this->resource = $resourceGenerator;
139+
$this->route = $routeGenerator;
140+
$this->test = $testGenerator;
77141
$this->str = $str;
78142
$this->schema = $schema;
79143
}
@@ -83,7 +147,7 @@ public function __construct(Generator $generator, Str $str, Schema $schema)
83147
*
84148
* @return int
85149
*/
86-
public function handle() : int
150+
public function handle(): int
87151
{
88152
// Checking interactive mode
89153
if ($this->option('interactive') == "") {
@@ -117,7 +181,7 @@ public function handle() : int
117181
protected function all()
118182
{
119183
try {
120-
$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
184+
$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
121185
foreach ($tables as $table) {
122186
$this->comment("Generating " . $table . " CRUD");
123187
$columns = Schema::getColumnListing($table);
@@ -134,7 +198,7 @@ protected function all()
134198
/**
135199
* Generate CRUD in interactive mode
136200
*/
137-
protected function interactive() : void
201+
protected function interactive(): void
138202
{
139203
$this->info("Welcome in Interactive mode");
140204

@@ -170,25 +234,25 @@ protected function interactive() : void
170234
*/
171235
protected function generate(string $name, string $table, bool $timestamps)
172236
{
173-
$this->generator->controller($name, $table);
237+
$this->controller->setData($name, $table)->generate();
174238
$this->info("Generated Controller!");
175239

176-
$this->generator->model($name, $table, $timestamps);
240+
$this->model->setData($name, $table, $timestamps)->generate();
177241
$this->info("Generated Model!");
178242

179-
$this->generator->request($name);
243+
$this->request->setData($name)->generate();
180244
$this->info("Generated Request!");
181245

182-
$this->generator->resource($name);
246+
$this->resource->setData($name)->generate();
183247
$this->info("Generated Resource!");
184248

185-
$this->passport ? $this->generator->secureRoutes($name) : $this->generator->routes($name);
249+
$this->route->setData($name, $this->passport)->generate();
186250
$this->info("Generated routes!");
187251

188-
$this->generator->factory($name);
252+
$this->factory->setData($name)->generate();
189253
$this->info("Generated Factory!");
190254

191-
$this->generator->test($name);
255+
$this->test->setData($name)->generate();
192256
$this->info("Generated Test!");
193257
}
194258
}

src/Core/Generator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace AndreaCivita\ApiCrudGenerator\Core;
44

5-
use Illuminate\Contracts\Filesystem\FileNotFoundException;
65
use Illuminate\Filesystem\Filesystem;
76
use Illuminate\Support\Str;
87

8+
/**
9+
* @deprecated
10+
*/
911
class Generator
1012
{
1113

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace AndreaCivita\ApiCrudGenerator\Core\Generators;
4+
5+
use AndreaCivita\ApiCrudGenerator\Core\Stub;
6+
use AndreaCivita\ApiCrudGenerator\Interfaces\Generator;
7+
use Illuminate\Filesystem\Filesystem;
8+
9+
class ControllerGenerator implements Generator
10+
{
11+
/**
12+
* @var string $name
13+
*/
14+
protected $name;
15+
16+
/**
17+
* @var string $table
18+
*/
19+
protected $table;
20+
21+
/**
22+
* @var Filesystem $fileSystem
23+
*/
24+
protected $fileSystem;
25+
26+
/**
27+
* @var Stub $stub
28+
*/
29+
protected $stub;
30+
31+
/**
32+
* @param Stub $stub
33+
*/
34+
public function __construct(Stub $stub)
35+
{
36+
$this->stub = $stub;
37+
$this->fileSystem = $this->stub->getFilesystemInstance();
38+
}
39+
40+
/**
41+
* @param string $name
42+
* @param string $table
43+
* @return ControllerGenerator
44+
*/
45+
public function setData(string $name, string $table): ControllerGenerator
46+
{
47+
$this->name = $name;
48+
$this->table = $table;
49+
return $this;
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function generate()
56+
{
57+
$content = $this->stub->parseStub('Controller', $this->name, ['table' => $this->table]);
58+
59+
if (!$this->fileSystem->exists("app/Http/Controllers/")) {
60+
$this->fileSystem->makeDirectory("app/Http/Controllers/", 0755, true);
61+
}
62+
63+
return $this->fileSystem->put("app/Http/Controllers/{$this->name}Controller.php", $content);
64+
}
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace AndreaCivita\ApiCrudGenerator\Core\Generators;
4+
5+
use AndreaCivita\ApiCrudGenerator\Core\Stub;
6+
use AndreaCivita\ApiCrudGenerator\Interfaces\Generator;
7+
use Illuminate\Filesystem\Filesystem;
8+
9+
class FactoryGenerator implements Generator
10+
{
11+
/**
12+
* @var string $name
13+
*/
14+
protected $name;
15+
16+
17+
/**
18+
* @var Filesystem $fileSystem
19+
*/
20+
protected $fileSystem;
21+
22+
/**
23+
* @var Stub $stub
24+
*/
25+
protected $stub;
26+
27+
/**
28+
* @param Stub $stub
29+
*/
30+
public function __construct(Stub $stub)
31+
{
32+
$this->stub = $stub;
33+
$this->fileSystem = $this->stub->getFilesystemInstance();
34+
}
35+
36+
/**
37+
* @param string $name
38+
* @return $this
39+
*/
40+
public function setData(string $name): FactoryGenerator
41+
{
42+
$this->name = $name;
43+
return $this;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function generate()
50+
{
51+
$content = $this->stub->parseStub('Factory', $this->name);
52+
53+
if (!$this->fileSystem->exists("database/factories/")) {
54+
$this->fileSystem->makeDirectory("database/factories/", 0755, true);
55+
}
56+
return $this->fileSystem->put("database/factories/{$this->name}Factory.php", $content);
57+
}
58+
}

0 commit comments

Comments
 (0)