Skip to content

Commit 47fac9a

Browse files
authored
Merge pull request #23 from MarwaMourad15/master
Generate full forms
2 parents 773b644 + cc80c9e commit 47fac9a

File tree

4 files changed

+88
-18
lines changed

4 files changed

+88
-18
lines changed

src/Controllers/ScaffoldController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function store(Request $request)
5555
// 2. Create controller.
5656
if (in_array('controller', $request->get('create'))) {
5757
$paths['controller'] = (new ControllerCreator($request->get('controller_name')))
58-
->create($request->get('model_name'));
58+
->create($request->get('model_name'), $request->get('fields'));
5959
}
6060

6161
// 3. Create migration.

src/Scaffold/ControllerCreator.php

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class ControllerCreator
1818
*/
1919
protected $files;
2020

21+
protected $DummyGridField = '';
22+
23+
protected $DummyShowField = '';
24+
25+
protected $DummyFormField = '';
26+
2127
/**
2228
* ControllerCreator constructor.
2329
*
@@ -40,14 +46,20 @@ public function __construct($name, $files = null)
4046
*
4147
* @return string
4248
*/
43-
public function create($model)
49+
public function create($model, $fields)
4450
{
4551
$path = $this->getpath($this->name);
4652

4753
if ($this->files->exists($path)) {
4854
throw new \Exception("Controller [$this->name] already exists!");
4955
}
5056

57+
$this->generateGridField($fields);
58+
59+
$this->generateShowField($fields);
60+
61+
$this->generateFormField($fields);
62+
5163
$stub = $this->files->get($this->getStub());
5264

5365
$this->files->put($path, $this->replace($stub, $this->name, $model));
@@ -67,8 +79,8 @@ protected function replace($stub, $name, $model)
6779
$stub = $this->replaceClass($stub, $name);
6880

6981
return str_replace(
70-
['DummyModelNamespace', 'DummyModel'],
71-
[$model, class_basename($model)],
82+
['DummyModelNamespace', 'DummyModel', 'DummyGridField', 'DummyShowField', 'DummyFormField'],
83+
[$model, class_basename($model), $this->DummyGridField, $this->DummyShowField, $this->DummyFormField],
7284
$stub
7385
);
7486
}
@@ -125,4 +137,59 @@ public function getStub()
125137
{
126138
return __DIR__.'/stubs/controller.stub';
127139
}
140+
141+
public function generateFormField($fields = [])
142+
{
143+
$fields = array_filter($fields, function ($field) {
144+
return isset($field['name']) && !empty($field['name']);
145+
});
146+
147+
if (empty($fields)) {
148+
throw new \Exception('Table fields can\'t be empty');
149+
}
150+
151+
foreach ($fields as $field) {
152+
$rows[] = "\$form->text('{$field['name']}', '{$field['name']}');\n";
153+
}
154+
155+
$this->DummyFormField = trim(implode(str_repeat(' ', 8), $rows), "\n");
156+
157+
return $this;
158+
}
159+
160+
public function generateShowField($fields = [])
161+
{
162+
$fields = array_filter($fields, function ($field) {
163+
return isset($field['name']) && !empty($field['name']);
164+
});
165+
166+
if (empty($fields)) {
167+
throw new \Exception('Table fields can\'t be empty');
168+
}
169+
foreach ($fields as $field) {
170+
$rows[] = "\$show->{$field['name']}('{$field['name']}');\n";
171+
}
172+
173+
$this->DummyShowField = trim(implode(str_repeat(' ', 8), $rows), "\n");
174+
175+
return $this;
176+
}
177+
178+
public function generateGridField($fields = [])
179+
{
180+
$fields = array_filter($fields, function ($field) {
181+
return isset($field['name']) && !empty($field['name']);
182+
});
183+
184+
if (empty($fields)) {
185+
throw new \Exception('Table fields can\'t be empty');
186+
}
187+
foreach ($fields as $field) {
188+
$rows[] = "\$grid->{$field['name']}('{$field['name']}');\n";
189+
}
190+
191+
$this->DummyGridField = trim(implode(str_repeat(' ', 8), $rows), "\n");
192+
193+
return $this;
194+
}
128195
}

src/Scaffold/stubs/controller.stub

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class DummyClass extends Controller
2323
public function index(Content $content)
2424
{
2525
return $content
26-
->header('Index')
27-
->description('description')
26+
->header(trans('admin.index'))
27+
->description(trans('admin.description'))
2828
->body($this->grid());
2929
}
3030

@@ -38,8 +38,8 @@ class DummyClass extends Controller
3838
public function show($id, Content $content)
3939
{
4040
return $content
41-
->header('Detail')
42-
->description('description')
41+
->header(trans('admin.detail'))
42+
->description(trans('admin.description'))
4343
->body($this->detail($id));
4444
}
4545

@@ -53,8 +53,8 @@ class DummyClass extends Controller
5353
public function edit($id, Content $content)
5454
{
5555
return $content
56-
->header('Edit')
57-
->description('description')
56+
->header(trans('admin.edit'))
57+
->description(trans('admin.description'))
5858
->body($this->form()->edit($id));
5959
}
6060

@@ -67,8 +67,8 @@ class DummyClass extends Controller
6767
public function create(Content $content)
6868
{
6969
return $content
70-
->header('Create')
71-
->description('description')
70+
->header(trans('admin.create'))
71+
->description(trans('admin.description'))
7272
->body($this->form());
7373
}
7474

@@ -82,8 +82,9 @@ class DummyClass extends Controller
8282
$grid = new Grid(new DummyModel);
8383

8484
$grid->id('ID');
85-
$grid->created_at('Created at');
86-
$grid->updated_at('Updated at');
85+
DummyGridField
86+
$grid->created_at(trans('admin.created_at'));
87+
$grid->updated_at(trans('admin.updated_at'));
8788

8889
return $grid;
8990
}
@@ -99,8 +100,9 @@ class DummyClass extends Controller
99100
$show = new Show(DummyModel::findOrFail($id));
100101

101102
$show->id('ID');
102-
$show->created_at('Created at');
103-
$show->updated_at('Updated at');
103+
DummyShowField
104+
$show->created_at(trans('admin.created_at'));
105+
$show->updated_at(trans('admin.updated_at'));
104106

105107
return $show;
106108
}
@@ -115,8 +117,9 @@ class DummyClass extends Controller
115117
$form = new Form(new DummyModel);
116118

117119
$form->display('ID');
118-
$form->display('Created at');
119-
$form->display('Updated at');
120+
DummyFormField
121+
$form->display(trans('admin.created_at'));
122+
$form->display(trans('admin.updated_at'));
120123

121124
return $form;
122125
}

src/Scaffold/stubs/create.stub

100755100644
File mode changed.

0 commit comments

Comments
 (0)