Skip to content

Commit 4bc0395

Browse files
committed
wip
1 parent e256b13 commit 4bc0395

15 files changed

+286
-28
lines changed

config/generators.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* have to implement Webfactor\Laravel\Generators\Contracts\MakeServiceInterface.
77
*/
88
'services' => [
9+
Webfactor\Laravel\Generators\Services\MakeMigrationService::class,
10+
/*Webfactor\Laravel\Generators\Services\MakeLanguageService::class,
911
Webfactor\Laravel\Generators\Services\MakeBackpackCrudModelService::class,
1012
Webfactor\Laravel\Generators\Services\MakeBackpackCrudControllerService::class,
1113
Webfactor\Laravel\Generators\Services\MakeBackpackCrudRequestService::class,
12-
Webfactor\Laravel\Generators\Services\MakeMigrationService::class,
1314
Webfactor\Laravel\Generators\Services\MakeFactoryService::class,
1415
Webfactor\Laravel\Generators\Services\MakeSeederService::class,
16+
Webfactor\Laravel\Generators\Services\MakeRouteService::class,*/
1517
],
1618
];

src/Commands/MakeBackpackCrudController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ protected function replaceNameStrings(&$stub, $name)
8383
$table = str_plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'));
8484

8585
$stub = str_replace('DummyTable', $table, $stub);
86+
$stub = str_replace('dummy_name', snake_case($this->argument('name')), $stub);
8687
$stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub);
8788

8889
return $this;
@@ -99,7 +100,9 @@ protected function buildClass($name)
99100
{
100101
$stub = $this->files->get($this->getStub());
101102

102-
return $this->replaceNamespace($stub, $name)->replaceNameStrings($stub, $name)->replaceClass($stub, $name);
103+
return $this->replaceNamespace($stub, $name)
104+
->replaceNameStrings($stub, $name)
105+
->replaceClass($stub, $name);
103106
}
104107

105108
/**

src/Commands/MakeEntity.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,37 @@
44

55
use Illuminate\Console\Command;
66
use Webfactor\Laravel\Generators\MakeServices;
7+
use Webfactor\Laravel\Generators\MigrationSchema;
78

89
class MakeEntity extends Command
910
{
1011
/**
11-
* The name and signature of the console command.
12+
* The name of the entity beeing created.
1213
*
1314
* @var string
1415
*/
15-
protected $signature = 'make:entity {entity} {--schema=}';
16+
public $entity;
17+
1618
/**
17-
* The console command description.
19+
* The migration schema object.
20+
*
21+
* @var MigrationSchema
22+
*/
23+
public $schema;
24+
25+
/**
26+
* The name and signature of the console command.
1827
*
1928
* @var string
2029
*/
21-
protected $description = 'Make Entity';
30+
protected $signature = 'make:entity {entity} {--schema="name:string"} {--migrate}';
31+
2232
/**
23-
* Create a new command instance.
33+
* The console command description.
2434
*
25-
* @return void
35+
* @var string
2636
*/
27-
public function __construct()
28-
{
29-
parent::__construct();
30-
}
37+
protected $description = 'Make Entity';
3138

3239
/**
3340
* Execute the console command.
@@ -36,6 +43,9 @@ public function __construct()
3643
*/
3744
public function handle()
3845
{
46+
$this->entity = $this->argument('entity');
47+
$this->schema = new MigrationSchema($this->option('schema'));
48+
3949
(new MakeServices($this))->call();
4050
}
4151
}

src/Contracts/MakeServiceAbstract.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
abstract class MakeServiceAbstract
88
{
99
protected $command;
10-
protected $entity;
1110

1211
public function __construct(MakeEntity $command)
1312
{
1413
$this->command = $command;
15-
$this->entity = $command->argument('entity');
1614
}
1715
}

src/MigrationField.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators;
4+
5+
class MigrationField
6+
{
7+
protected $name;
8+
9+
protected $type;
10+
11+
protected $nullable = false;
12+
13+
protected $unique = false;
14+
15+
protected $default = null;
16+
17+
protected $foreign = null;
18+
19+
public function __construct(string $field)
20+
{
21+
$this->parse($field);
22+
}
23+
24+
private function parse(string $field)
25+
{
26+
$params = collect(explode(':', $field));
27+
28+
$this->name = $params->pull(0);
29+
$this->type = $params->pull(1);
30+
31+
foreach ($params as $param) {
32+
$this->fillObject($param);
33+
}
34+
}
35+
36+
private function fillObject(string $param)
37+
{
38+
if ($param == 'nullable') {
39+
return $this->nullable = true;
40+
}
41+
42+
if ($param == 'unique') {
43+
return $this->unique = true;
44+
}
45+
46+
if ($param == 'foreign') {
47+
return $this->foreign = true;
48+
}
49+
50+
if (starts_with($param, 'default(')) {
51+
preg_match('/\((.*)\)/', $param, $match);
52+
53+
return $this->default = $match[1];
54+
}
55+
}
56+
}

src/MigrationSchema.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators;
4+
5+
class MigrationSchema
6+
{
7+
protected $schema;
8+
9+
protected $original;
10+
11+
public function __construct(string $schema)
12+
{
13+
$this->original = $schema;
14+
$this->schema = collect();
15+
16+
$this->parse();
17+
}
18+
19+
private function parse()
20+
{
21+
foreach ($this->parseSchemaString($this->original) as $field) {
22+
$this->schema->push($this->parseField($field));
23+
}
24+
}
25+
26+
private function parseSchemaString(string $schema)
27+
{
28+
return explode(',', $schema);
29+
}
30+
31+
private function parseField(string $field)
32+
{
33+
return new MigrationField($field);
34+
}
35+
}

src/Services/MakeBackpackCrudControllerService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MakeBackpackCrudControllerService extends MakeServiceAbstract implements M
1010
public function make()
1111
{
1212
$this->command->call('make:crud-controller', [
13-
'name' => $this->getName($this->entity),
13+
'name' => $this->getName($this->command->entity),
1414
]);
1515
}
1616

src/Services/MakeBackpackCrudModelService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MakeBackpackCrudModelService extends MakeServiceAbstract implements MakeSe
1010
public function make()
1111
{
1212
$this->command->call('make:crud-model', [
13-
'name' => $this->getName($this->entity),
13+
'name' => $this->getName($this->command->entity),
1414
]);
1515
}
1616

src/Services/MakeBackpackCrudRequestService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MakeBackpackCrudRequestService extends MakeServiceAbstract implements Make
1010
public function make()
1111
{
1212
$this->command->call('make:crud-request', [
13-
'name' => $this->getName($this->entity),
13+
'name' => $this->getName($this->command->entity),
1414
]);
1515
}
1616

src/Services/MakeFactoryService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ class MakeFactoryService extends MakeServiceAbstract implements MakeServiceInter
1010
public function make()
1111
{
1212
$this->command->call('make:factory', [
13-
'name' => $this->getName($this->entity),
13+
'name' => $this->getName($this->command->entity),
14+
'--model' => 'Models\\' . $this->getModelName($this->command->entity),
1415
]);
1516
}
1617

1718
public function getName(string $entity): string
1819
{
1920
return ucfirst($entity) . 'Factory';
2021
}
22+
23+
public function getModelName(string $entity): string
24+
{
25+
return ucfirst($entity);
26+
}
2127
}

0 commit comments

Comments
 (0)