Skip to content

Commit ad5ecdf

Browse files
committed
[RF] refactor to be more flexible and add --schema option
1 parent f491c15 commit ad5ecdf

17 files changed

+267
-89
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $ composer require webfactor/laravel-generators
1919
## Usage
2020

2121
``` bash
22-
php artisan make:crud entity
22+
php artisan make:entity {entity_name}
2323
```
2424

2525
Use *singular* for entity. This will automatically create (while respecting naming conventions):

config/generators.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* Services that should be applied by make:entity as default. The classes for 'service'
6+
* have to implement Webfactor\Laravel\Generators\Contracts\MakeServiceInterface and
7+
* conversion uses Webfactor\Laravel\Generators\Contracts\ConversionNameInterface.
8+
*/
9+
'services' => [
10+
Webfactor\Laravel\Generators\Services\MakeMigrationService::class => [
11+
'name_conversion' => Webfactor\Laravel\Generators\Services\Conversion\MigrationName::class,
12+
'schema' => 'name:string',
13+
],
14+
Webfactor\Laravel\Generators\Services\MakeFactoryService::class => [
15+
'name_conversion' => Webfactor\Laravel\Generators\Services\Conversion\FactoryName::class,
16+
],
17+
Webfactor\Laravel\Generators\Services\MakeSeederService::class => [
18+
'name_conversion' => Webfactor\Laravel\Generators\Services\Conversion\SeederName::class,
19+
],
20+
Webfactor\Laravel\Generators\Services\MakeBackpackCrudService::class => [
21+
'name_conversion' => Webfactor\Laravel\Generators\Services\Conversion\BackpackCrudName::class,
22+
],
23+
],
24+
];

src/Commands/MakeCrudEntity.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/Commands/MakeEntity.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Webfactor\Laravel\Generators\MakeServices;
7+
8+
class MakeEntity extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:entity {entity} {--schema=}';
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Make Entity';
22+
/**
23+
* Create a new command instance.
24+
*
25+
* @return void
26+
*/
27+
public function __construct()
28+
{
29+
parent::__construct();
30+
}
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return mixed
36+
*/
37+
public function handle()
38+
{
39+
(new MakeServices($this))->call();
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
interface ConversionNameInterface
6+
{
7+
public static function getName(string $entityName);
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
use Webfactor\Laravel\Generators\Commands\MakeEntity;
6+
7+
abstract class MakeServiceAbstract
8+
{
9+
protected $command;
10+
protected $options;
11+
protected $entity;
12+
13+
public function __construct(MakeEntity $command, array $options)
14+
{
15+
$this->command = $command;
16+
$this->options = $options;
17+
$this->entity = $command->argument('entity');
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
interface MakeServiceInterface
6+
{
7+
public function make();
8+
}

src/GeneratorsServiceProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Webfactor\Laravel\Generators;
44

55
use Illuminate\Support\ServiceProvider;
6-
use Webfactor\Laravel\Generators\Commands\MakeCrudEntity;
6+
use Webfactor\Laravel\Generators\Commands\MakeEntity;
77

88
class GeneratorsServiceProvider extends ServiceProvider
99
{
@@ -16,9 +16,17 @@ public function boot()
1616
{
1717
if ($this->app->runningInConsole()) {
1818
$this->commands([
19-
MakeCrudEntity::class,
19+
MakeEntity::class,
2020
]);
21+
22+
$this->publishes([
23+
__DIR__ . '/../config/generators.php' => config_path('webfactor/generators.php'),
24+
], 'config');
2125
}
26+
27+
$this->mergeConfigFrom(
28+
__DIR__ . '/../config/generators.php', 'webfactor.generators'
29+
);
2230
}
2331

2432
/**

src/MakeServices.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators;
4+
5+
use Webfactor\Laravel\Generators\Commands\MakeEntity;
6+
use Webfactor\Laravel\Generators\Contracts\MakeServiceInterface;
7+
use Webfactor\Laravel\Generators\Services\MakeMigrationService;
8+
9+
class MakeServices
10+
{
11+
private $services;
12+
13+
private $command;
14+
15+
public function __construct(MakeEntity $command)
16+
{
17+
$this->command = $command;
18+
19+
$this->services = config('webfactor.generators.services', []);
20+
}
21+
22+
public function call()
23+
{
24+
foreach ($this->services as $service => $options) {
25+
$this->callService(new $service($this->command, $options));
26+
}
27+
}
28+
29+
private function callService(MakeServiceInterface $service)
30+
{
31+
$service->make();
32+
}
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Services\Conversion;
4+
5+
use Webfactor\Laravel\Generators\Contracts\ConversionNameInterface;
6+
7+
class BackpackCrudName implements ConversionNameInterface
8+
{
9+
public static function getName(string $entity)
10+
{
11+
return ucfirst($entity);
12+
}
13+
}

0 commit comments

Comments
 (0)