Skip to content

Commit ca1f30e

Browse files
committed
[EH] add basic columns and fields generation
1 parent 2e95f20 commit ca1f30e

File tree

3 files changed

+121
-13
lines changed

3 files changed

+121
-13
lines changed

src/Schemas/CrudColumn.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas;
4+
5+
class CrudColumn
6+
{
7+
private $type;
8+
9+
private $field;
10+
11+
public function __construct(MigrationField $migrationField)
12+
{
13+
$this->type = $migrationField->getType();
14+
$this->field = $migrationField->getField();
15+
}
16+
17+
public function generateColumn(): array
18+
{
19+
return [
20+
'name' => $this->getField(),
21+
'type' => $this->getType(),
22+
];
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getField()
29+
{
30+
return $this->field;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getType()
37+
{
38+
return $this->type;
39+
}
40+
}

src/Schemas/CrudField.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas;
4+
5+
class CrudField
6+
{
7+
private $type;
8+
9+
private $field;
10+
11+
public function __construct(MigrationField $migrationField)
12+
{
13+
$this->type = $migrationField->getType();
14+
$this->field = $migrationField->getField();
15+
}
16+
17+
public function generateField(): array
18+
{
19+
return [
20+
'name' => $this->getField(),
21+
'type' => $this->getType(),
22+
];
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getField()
29+
{
30+
return $this->field;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getType()
37+
{
38+
return $this->type;
39+
}
40+
}

src/Services/BackpackCrudControllerService.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
66
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
77
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
8+
use Webfactor\Laravel\Generators\Schemas\CrudColumn;
9+
use Webfactor\Laravel\Generators\Schemas\CrudField;
810

911
class BackpackCrudControllerService extends ServiceAbstract implements ServiceInterface
1012
{
@@ -21,6 +23,10 @@ public function call()
2123
]);
2224

2325
$this->addLatestFileToIdeStack();
26+
27+
$this->setColumnsFromSchema();
28+
$this->setFieldsFromSchema();
29+
2430
$this->fillColumnsAndFieldsInGeneratedControllerFromSchema();
2531
}
2632

@@ -29,37 +35,59 @@ public function getName(string $entity): string
2935
return ucfirst($entity);
3036
}
3137

32-
private function fillColumnsAndFieldsInGeneratedControllerFromSchema()
38+
private function fillColumnsAndFieldsInGeneratedControllerFromSchema(): void
3339
{
3440
$controllerFile = end($this->command->filesToBeOpened);
3541

3642
$controller = $this->filesystem->get($controllerFile);
37-
$controller = str_replace('__columns__', $this->getColumnsFromSchema(), $controller);
38-
$controller = str_replace('__fields__', $this->getFieldsFromSchema(), $controller);
43+
$controller = str_replace('__columns__', $this->getColumnsAsString(), $controller);
44+
$controller = str_replace('__fields__', $this->getfieldsAsString(), $controller);
3945
$this->filesystem->put($controllerFile, $controller);
4046
}
4147

48+
private function setColumnsFromSchema(): void
49+
{
50+
$this->command->schema->getStructure()->each(function ($field) {
51+
array_push($this->columns, new CrudColumn($field));
52+
});
53+
}
54+
55+
private function setFieldsFromSchema(): void
56+
{
57+
$this->command->schema->getStructure()->each(function ($field) {
58+
array_push($this->fields, new CrudField($field));
59+
});
60+
}
61+
4262
/**
4363
* @return string
4464
*/
45-
private function getColumnsFromSchema()
65+
private function getColumnsAsString(): string
4666
{
47-
$this->command->schema->getStructure()->each(function ($field) {
48-
array_push($this->columns, $field->makeColumn());
49-
});
67+
$columnsArray = [];
5068

51-
return ShortSyntaxArray::parse($this->columns);
69+
foreach ($this->columns as $crudColumn) {
70+
if ($column = $crudColumn->generateColumn()) {
71+
array_push($columnsArray, $column);
72+
}
73+
}
74+
75+
return ShortSyntaxArray::parse($columnsArray);
5276
}
5377

5478
/**
5579
* @return string
5680
*/
57-
private function getFieldsFromSchema()
81+
private function getfieldsAsString(): string
5882
{
59-
$this->command->schema->getStructure()->each(function ($field) {
60-
array_push($this->fields, $field->makeField());
61-
});
83+
$fieldsArray = [];
84+
85+
foreach ($this->fields as $crudfield) {
86+
if ($field = $crudfield->generateField()) {
87+
array_push($fieldsArray, $field);
88+
}
89+
}
6290

63-
return ShortSyntaxArray::parse($this->fields);
91+
return ShortSyntaxArray::parse($fieldsArray);
6492
}
6593
}

0 commit comments

Comments
 (0)