Skip to content
This repository was archived by the owner on Jan 10, 2020. It is now read-only.

Commit e6924f9

Browse files
committed
New relationships support
CRUD now supports relationships
1 parent 4641d2c commit e6924f9

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

src/Console/Crud.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ class Crud extends Command
5555
*
5656
* @var string
5757
*/
58-
protected $signature = 'laracogs:crud {table} {--api} {--migration} {--bootstrap} {--semantic} {--schema=} {--serviceOnly}';
58+
protected $signature = 'laracogs:crud {table}
59+
{--api}
60+
{--migration}
61+
{--bootstrap}
62+
{--semantic}
63+
{--serviceOnly}
64+
{--schema= : Basic schema support ie: id,increments,name:string,parent_id:integer}
65+
{--relationships= : Define the relationship ie: hasOne|App\Comment|comment,hasOne|App\Rating|rating or relation|class|column (without the _id)}';
5966

6067
/**
6168
* The console command description.
@@ -97,6 +104,7 @@ public function handle()
97104
'template_source' => '',
98105
'bootstrap' => false,
99106
'semantic' => false,
107+
'relationships' => null,
100108
'schema' => null,
101109
'_sectionPrefix_' => '',
102110
'_sectionTablePrefix_' => '',
@@ -149,6 +157,7 @@ public function handle()
149157
'template_source' => '',
150158
'bootstrap' => false,
151159
'semantic' => false,
160+
'relationships' => null,
152161
'schema' => null,
153162
'_sectionPrefix_' => strtolower($section).'.',
154163
'_sectionTablePrefix_' => strtolower($section).'_',
@@ -218,6 +227,10 @@ public function handle()
218227
$config['template_source'] = __DIR__.'/../Templates';
219228
}
220229

230+
if ($this->option('relationships')) {
231+
$config['relationships'] = $this->option('relationships');
232+
}
233+
221234
try {
222235
$this->line('Building repository...');
223236
$crudGenerator->createRepository($config);
@@ -296,6 +309,19 @@ public function handle()
296309
}
297310
}
298311

312+
if ($this->option('relationships')) {
313+
foreach (explode(',', $config['relationships']) as $relationshipExpression) {
314+
$relationship = explode('|', $relationshipExpression);
315+
if (in_array($relationship[0], ['hasOne'])) {
316+
if (! isset($relationship[2])) {
317+
$relationship[2] = strtolower(end(explode('\\', $relationship[1])));
318+
}
319+
320+
$parsedTable .= "\t\t\t\$table->integer('$relationship[2]_id');\n";
321+
}
322+
}
323+
}
324+
299325
$migrationData = str_replace("\$table->increments('id');", $parsedTable, $migrationData);
300326
file_put_contents($file->getPathname(), $migrationData);
301327
}

src/Generators/CrudGenerator.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct()
2525
public function createController($config)
2626
{
2727
if (! is_dir($config['_path_controller_'])) mkdir($config['_path_controller_'], 0777, true);
28-
28+
2929
$request = file_get_contents($config['template_source'].'/Controller.txt');
3030

3131
foreach ($config as $key => $value) {
@@ -54,6 +54,16 @@ public function createRepository($config)
5454
$model = str_replace('// _camel_case_ table data', $this->prepareTableDefinition($config['schema']), $model);
5555
}
5656

57+
if (! empty($config['relationships'])) {
58+
$relationships = [];
59+
60+
foreach (explode(',', $config['relationships']) as $relationshipExpression) {
61+
$relationships[] = explode('|', $relationshipExpression);
62+
}
63+
64+
$model = str_replace('// _camel_case_ relationships', $this->prepareModelRelationships($relationships), $model);
65+
}
66+
5767
foreach ($config as $key => $value) {
5868
$repo = str_replace($key, $value, $repo);
5969
$model = str_replace($key, $value, $model);
@@ -316,6 +326,35 @@ public function prepareTableExample($table)
316326
return $tableExample;
317327
}
318328

329+
/**
330+
* Prepare a models relationships
331+
*
332+
* @param array $relationships
333+
* @return string
334+
*/
335+
public function prepareModelRelationships($relationships)
336+
{
337+
$relationshipMethods = '';
338+
339+
foreach ($relationships as $relation) {
340+
if (! isset($relation[2])) {
341+
$relation[2] = strtolower(end(explode('\\', $relation[1])));
342+
}
343+
344+
$method = str_singular($relation[2]);
345+
346+
if (stristr($relation[0], 'many')) {
347+
$method = str_plural($relation[2]);
348+
}
349+
350+
$relationshipMethods .= "\n\tpublic function ".$method."() {";
351+
$relationshipMethods .= "\n\t\treturn \$this->$relation[0]($relation[1]::class);";
352+
$relationshipMethods .= "\n\t}";
353+
}
354+
355+
return $relationshipMethods;
356+
}
357+
319358
/**
320359
* Create an example by type for table definitions
321360
* @param string $type

src/Templates/Repository/Model.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ class _camel_case_ extends Model
2020
// create rules
2121
];
2222

23+
// _camel_case_ relationships
24+
2325
}

tests/CrudApiGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function setUp()
1313
$this->config = [
1414
'bootstrap' => false,
1515
'semantic' => false,
16+
'relationships' => null,
1617
'schema' => null,
1718
'_path_facade_' => vfsStream::url('Facades'),
1819
'_path_service_' => vfsStream::url('Services'),

tests/CrudGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function setUp()
1313
$this->config = [
1414
'bootstrap' => false,
1515
'semantic' => false,
16+
'relationships' => null,
1617
'schema' => null,
1718
'_path_facade_' => vfsStream::url('Facades'),
1819
'_path_service_' => vfsStream::url('Services'),

0 commit comments

Comments
 (0)