Skip to content

Commit

Permalink
Refactor + small fixes (#37)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people authored Oct 23, 2021
1 parent 199c06e commit 6e32531
Show file tree
Hide file tree
Showing 27 changed files with 965 additions and 81 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
vendor/
composer.lock
.php_cs.cache
.phpunit.result.cache
.phpunit.result.cache
app/
database/
routes/
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"require": {
"php": "^7.3|^8.0",
"laravel/framework": "^8.0",
"doctrine/dbal": "^2.9"
"doctrine/dbal": "^2.9",
"illuminate/support": "^8.61"
},
"require-dev": {
"phpunit/phpunit": "^7|^8",
Expand Down
8 changes: 7 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<testsuite name="Api Crud Generator test-suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand All @@ -16,4 +22,4 @@
</exclude>
</whitelist>
</filter>
</phpunit>
</phpunit>
92 changes: 78 additions & 14 deletions src/Commands/ApiCrudGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
namespace AndreaCivita\ApiCrudGenerator\Commands;

use AndreaCivita\ApiCrudGenerator\Core\Generator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\ControllerGenerator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\FactoryGenerator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\ModelGenerator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\RequestGenerator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\ResourceGenerator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\RouteGenerator;
use AndreaCivita\ApiCrudGenerator\Core\Generators\TestGenerator;
use Doctrine\DBAL\Driver\PDOException;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
Expand Down Expand Up @@ -42,6 +49,42 @@ class ApiCrudGenerator extends Command
*/
protected $generator;

/**
* @var ControllerGenerator $controller
*/
protected $controller;

/**
* @var FactoryGenerator $factory
*/
protected $factory;

/**
* @var ModelGenerator $model
*/
protected $model;

/**
* @var RequestGenerator $request
*/
protected $request;

/**
* @var ResourceGenerator $resource
*/
protected $resource;

/**
* @var RouteGenerator $route
*/
protected $route;


/**
* @var TestGenerator $test
*/
protected $test;


/**
* The String support instance
Expand All @@ -66,14 +109,35 @@ class ApiCrudGenerator extends Command
/**
* Create a new command instance.
*
* @param Generator $generator
* @param ControllerGenerator $controllerGenerator
* @param FactoryGenerator $factoryGenerator
* @param ModelGenerator $modelGenerator
* @param RequestGenerator $requestGenerator
* @param ResourceGenerator $resourceGenerator
* @param RouteGenerator $routeGenerator
* @param TestGenerator $testGenerator
* @param Str $str
* @param Schema $schema
*/
public function __construct(Generator $generator, Str $str, Schema $schema)
{
public function __construct(
ControllerGenerator $controllerGenerator,
FactoryGenerator $factoryGenerator,
ModelGenerator $modelGenerator,
RequestGenerator $requestGenerator,
ResourceGenerator $resourceGenerator,
RouteGenerator $routeGenerator,
TestGenerator $testGenerator,
Str $str,
Schema $schema
) {
parent::__construct();
$this->generator = $generator;
$this->controller = $controllerGenerator;
$this->factory = $factoryGenerator;
$this->model = $modelGenerator;
$this->request = $requestGenerator;
$this->resource = $resourceGenerator;
$this->route = $routeGenerator;
$this->test = $testGenerator;
$this->str = $str;
$this->schema = $schema;
}
Expand All @@ -83,7 +147,7 @@ public function __construct(Generator $generator, Str $str, Schema $schema)
*
* @return int
*/
public function handle() : int
public function handle(): int
{
// Checking interactive mode
if ($this->option('interactive') == "") {
Expand Down Expand Up @@ -117,7 +181,7 @@ public function handle() : int
protected function all()
{
try {
$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
foreach ($tables as $table) {
$this->comment("Generating " . $table . " CRUD");
$columns = Schema::getColumnListing($table);
Expand All @@ -134,7 +198,7 @@ protected function all()
/**
* Generate CRUD in interactive mode
*/
protected function interactive() : void
protected function interactive(): void
{
$this->info("Welcome in Interactive mode");

Expand Down Expand Up @@ -170,25 +234,25 @@ protected function interactive() : void
*/
protected function generate(string $name, string $table, bool $timestamps)
{
$this->generator->controller($name, $table);
$this->controller->setData($name, $table)->generate();
$this->info("Generated Controller!");

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

$this->generator->request($name);
$this->request->setData($name)->generate();
$this->info("Generated Request!");

$this->generator->resource($name);
$this->resource->setData($name)->generate();
$this->info("Generated Resource!");

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

$this->generator->factory($name);
$this->factory->setData($name)->generate();
$this->info("Generated Factory!");

$this->generator->test($name);
$this->test->setData($name)->generate();
$this->info("Generated Test!");
}
}
4 changes: 3 additions & 1 deletion src/Core/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace AndreaCivita\ApiCrudGenerator\Core;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;

/**
* @deprecated
*/
class Generator
{

Expand Down
65 changes: 65 additions & 0 deletions src/Core/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace AndreaCivita\ApiCrudGenerator\Core\Generators;

use AndreaCivita\ApiCrudGenerator\Core\Stub;
use AndreaCivita\ApiCrudGenerator\Interfaces\Generator;
use Illuminate\Filesystem\Filesystem;

class ControllerGenerator implements Generator
{
/**
* @var string $name
*/
protected $name;

/**
* @var string $table
*/
protected $table;

/**
* @var Filesystem $fileSystem
*/
protected $fileSystem;

/**
* @var Stub $stub
*/
protected $stub;

/**
* @param Stub $stub
*/
public function __construct(Stub $stub)
{
$this->stub = $stub;
$this->fileSystem = $this->stub->getFilesystemInstance();
}

/**
* @param string $name
* @param string $table
* @return ControllerGenerator
*/
public function setData(string $name, string $table): ControllerGenerator
{
$this->name = $name;
$this->table = $table;
return $this;
}

/**
* @inheritDoc
*/
public function generate()
{
$content = $this->stub->parseStub('Controller', $this->name, ['table' => $this->table]);

if (!$this->fileSystem->exists("app/Http/Controllers/")) {
$this->fileSystem->makeDirectory("app/Http/Controllers/", 0755, true);
}

return $this->fileSystem->put("app/Http/Controllers/{$this->name}Controller.php", $content);
}
}
58 changes: 58 additions & 0 deletions src/Core/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace AndreaCivita\ApiCrudGenerator\Core\Generators;

use AndreaCivita\ApiCrudGenerator\Core\Stub;
use AndreaCivita\ApiCrudGenerator\Interfaces\Generator;
use Illuminate\Filesystem\Filesystem;

class FactoryGenerator implements Generator
{
/**
* @var string $name
*/
protected $name;


/**
* @var Filesystem $fileSystem
*/
protected $fileSystem;

/**
* @var Stub $stub
*/
protected $stub;

/**
* @param Stub $stub
*/
public function __construct(Stub $stub)
{
$this->stub = $stub;
$this->fileSystem = $this->stub->getFilesystemInstance();
}

/**
* @param string $name
* @return $this
*/
public function setData(string $name): FactoryGenerator
{
$this->name = $name;
return $this;
}

/**
* @inheritDoc
*/
public function generate()
{
$content = $this->stub->parseStub('Factory', $this->name);

if (!$this->fileSystem->exists("database/factories/")) {
$this->fileSystem->makeDirectory("database/factories/", 0755, true);
}
return $this->fileSystem->put("database/factories/{$this->name}Factory.php", $content);
}
}
Loading

0 comments on commit 6e32531

Please sign in to comment.