Skip to content

Commit 1fce4a2

Browse files
committed
[EH] add filling columns and fields from migration schema / wip
1 parent 59e88e1 commit 1fce4a2

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

src/MigrationField.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,22 @@ public function makeValidationRule(): string
115115

116116
return $rule;
117117
}
118+
119+
public function makeColumn()
120+
{
121+
return [
122+
'name' => 'title',
123+
'type' => 'text',
124+
'label' => 'Title',
125+
];
126+
}
127+
128+
public function makeField()
129+
{
130+
return [
131+
'name' => 'title',
132+
'type' => 'text',
133+
'label' => 'Title',
134+
];
135+
}
118136
}

src/Services/BackpackCrudControllerService.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,62 @@
44

55
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
66
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7+
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
78

89
class BackpackCrudControllerService extends ServiceAbstract implements ServiceInterface
910
{
1011
protected $relativeToBasePath = 'app/Http/Controllers/Admin';
1112

13+
protected $columns = [];
14+
15+
protected $fields = [];
16+
1217
public function call()
1318
{
1419
$this->command->call('make:crud-controller', [
1520
'name' => $this->getName($this->command->entity),
1621
]);
1722

1823
$this->addLatestFileToIdeStack();
24+
$this->fillColumnsAndFieldsInGeneratedControllerFromSchema();
1925
}
2026

2127
public function getName(string $entity): string
2228
{
2329
return ucfirst($entity);
2430
}
31+
32+
private function fillColumnsAndFieldsInGeneratedControllerFromSchema()
33+
{
34+
$controllerFile = end($this->command->filesToBeOpened);
35+
36+
$controller = $this->filesystem->get($controllerFile);
37+
$controller = str_replace('__columns__', $this->getColumnsFromSchema(), $controller);
38+
$controller = str_replace('__fields__', $this->getFieldsFromSchema(), $controller);
39+
$this->filesystem->put($controllerFile, $controller);
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
private function getColumnsFromSchema()
46+
{
47+
$this->command->schema->getStructure()->each(function ($field) {
48+
array_push($this->columns, $field->makeColumn());
49+
});
50+
51+
return ShortSyntaxArray::parse($this->columns);
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
private function getFieldsFromSchema()
58+
{
59+
$this->command->schema->getStructure()->each(function ($field) {
60+
array_push($this->fields, $field->makeField());
61+
});
62+
63+
return ShortSyntaxArray::parse($this->fields);
64+
}
2565
}

stubs/crud-controller.stub

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,13 @@ class DummyClassCrudController extends CrudController
2929
|--------------------------------------------------------------------------
3030
*/
3131

32-
$this->crud->setFromDb();
32+
// ------ CRUD COLUMNS
33+
34+
$this->crud->addColumns(__columns__);
3335

3436
// ------ CRUD FIELDS
35-
// $this->crud->addField($options, 'update/create/both');
36-
// $this->crud->addFields($array_of_arrays, 'update/create/both');
37-
// $this->crud->removeField('name', 'update/create/both');
38-
// $this->crud->removeFields($array_of_names, 'update/create/both');
3937

40-
// ------ CRUD COLUMNS
41-
// $this->crud->addColumn(); // add a single column, at the end of the stack
42-
// $this->crud->addColumns(); // add multiple columns, at the end of the stack
43-
// $this->crud->removeColumn('column_name'); // remove a column from the stack
44-
// $this->crud->removeColumns(['column_name_1', 'column_name_2']); // remove an array of columns from the stack
45-
// $this->crud->setColumnDetails('column_name', ['attribute' => 'value']); // adjusts the properties of the passed in column (by name)
46-
// $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);
38+
$this->crud->addFields(__fields__);
4739

4840
// ------ CRUD BUTTONS
4941
// possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others;

0 commit comments

Comments
 (0)