Skip to content

Commit c1a6ea8

Browse files
committed
Moves the templates folder out of src. Adds the ability to specify which field to use as title
1 parent 3ed308e commit c1a6ea8

File tree

91 files changed

+348
-63
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+348
-63
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "crestapps/laravel-code-generator",
33
"license": "MIT",
44
"description": "A clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms! It is extremely flexible and customizable to cover many use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.",
5-
"version": "v1.1.1",
5+
"version": "v1.2.0",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",

config/codegenerator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@
9797
*/
9898
'languages_path' => base_path('resources/lang'),
9999

100+
/*
101+
|--------------------------------------------------------------------------
102+
| Key phrases that are will be used to determine if a field name should be used for header.
103+
|--------------------------------------------------------------------------
104+
*/
105+
'common_header_names' => ['title','name','label','header'],
106+
107+
/*
108+
|--------------------------------------------------------------------------
109+
| Key phrases that are will be used to determine if a field should be a primary key.
110+
|--------------------------------------------------------------------------
111+
*/
112+
'common_id_names' => ['id'],
113+
100114
/*
101115
|--------------------------------------------------------------------------
102116
| The data-value to Eloquent method mapping.

src/CodeGeneratorServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public function boot()
2525
], 'default');
2626

2727
$this->publishes([
28-
__DIR__ . '/Templates/default' => base_path('resources\\codegenerator-templates\\default'),
28+
__DIR__ . '/../templates/default' => base_path('resources\\codegenerator-templates\\default'),
2929
], 'default');
3030

3131
$this->publishes([
32-
__DIR__ . '/Templates/default-collective' => base_path('resources\\codegenerator-templates\\default-collective'),
32+
__DIR__ . '/../templates/default-collective' => base_path('resources\\codegenerator-templates\\default-collective'),
3333
], 'collective-template');
3434

3535
$this->createDirectory(base_path('resources\\codegenerator-files'));

src/Commands/CreateEditViewCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function handleCreateView()
5959
->replaceCommonTemplates($stub, $input)
6060
->replaceFileUpload($stub, $fields)
6161
->replacePrimaryKey($stub, $this->getPrimaryKeyName($fields))
62+
->replaceModelHeader($stub, $this->getHeaderFieldAccessor($fields, $input->modelName))
6263
->createViewFile($stub, $destenationFile)
6364
->info('Edit view view was crafted successfully.');
6465
}

src/Commands/CreateShowViewCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function handleCreateView()
5959
$this->replaceCommonTemplates($stub, $input)
6060
->replacePrimaryKey($stub, $this->getPrimaryKeyName($fields))
6161
->replaceTableRows($stub, $htmlCreator->getShowRowsHtmlField())
62+
->replaceModelHeader($stub, $this->getHeaderFieldAccessor($fields, $input->modelName))
6263
->createViewFile($stub, $destenationFile)
6364
->info('Show view was crafted successfully.');
6465
}

src/DatabaseParsers/ParserBase.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use App;
66
use Exception;
77
use CrestApps\CodeGenerator\Models\Field;
8-
use CrestApps\CodeGenerator\Support\FieldOptimizer;
8+
use CrestApps\CodeGenerator\Models\FieldMapper;
9+
use CrestApps\CodeGenerator\Support\FieldsOptimizer;
910

1011
abstract class ParserBase
1112
{
@@ -92,16 +93,13 @@ public function getFields()
9293
*/
9394
protected function transfer(array $columns)
9495
{
95-
$fields = [];
96+
$fields = array_map(function($field){
97+
return new FieldMapper($field, null);
98+
}, $this->getTransfredField($column));
9699

97-
foreach($columns as $column)
98-
{
99-
$optimizer = new FieldOptimizer($this->getTransfredField($column));
100-
101-
$fields[] = $optimizer->optimize()->getField();
102-
}
100+
$optimizer = new FieldsOptimizer($fields);
103101

104-
return $fields;
102+
return $optimizer->optimize()->getFields();
105103
}
106104

107105
/**

src/HtmlGenerators/GenerateFormViews.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function getShowRowsHtmlField(array $fields = null)
113113
$row = $stub;
114114
$this->replaceFieldName($row, $field->name)
115115
->replaceModelName($row, $this->modelName)
116-
->replaceRowFieldValue($row, $this->getFieldValue($field))
116+
->replaceRawFieldValue($row, $this->getFieldValue($field))
117117
->replaceFieldTitle($row, $this->getTitle($field->getLabel(), true));
118118

119119
$rows .= $row . PHP_EOL;
@@ -173,7 +173,7 @@ public function getIndexBodyCells(array $fields = null)
173173

174174
$this->replaceFieldName($row, $field->name)
175175
->replaceModelName($row, $this->modelName)
176-
->replaceRowFieldValue($row, $this->getFieldValue($field))
176+
->replaceRawFieldValue($row, $this->getFieldValue($field))
177177
->replaceFieldTitle($row, $this->getTitle($field->getLabel(), true));
178178

179179
$rows .= $row . PHP_EOL;
@@ -183,7 +183,15 @@ public function getIndexBodyCells(array $fields = null)
183183
return $rows;
184184
}
185185

186-
protected function replaceRowFieldValue(& $stub, $value)
186+
/**
187+
* Replace the field'd raw valw fo the given stub.
188+
*
189+
* @param string $stub
190+
* @param string $value
191+
*
192+
* @return $this
193+
*/
194+
protected function replaceRawFieldValue(& $stub, $value)
187195
{
188196
$stub = str_replace('{{fieldValue}}', $value, $stub);
189197

src/Models/Field.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ class Field
154154
*/
155155
public $isMultipleAnswers = false;
156156

157+
/**
158+
* Makes the field bahaves as a header.
159+
*
160+
* @var bool
161+
*/
162+
public $isHeader = false;
163+
157164
/**
158165
* Field placeholder
159166
*

src/Models/FieldMapper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace CrestApps\CodeGenerator\Models;
4+
use CrestApps\CodeGenerator\Models\Field;
5+
6+
class FieldMapper
7+
{
8+
/**
9+
* The field to optimize
10+
*
11+
* @var array of CrestApps\CodeGenerator\Models\Field
12+
*/
13+
public $field;
14+
15+
/**
16+
* Create a new optimizer instance.
17+
*
18+
* @var array
19+
*/
20+
public $meta;
21+
22+
public function __construct(Field $field, array $meta = null)
23+
{
24+
$this->field = $field;
25+
$this->meta = $meta;
26+
}
27+
}

src/Support/FieldOptimizer.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace CrestApps\CodeGenerator\Support;
44
use CrestApps\CodeGenerator\Models\Field;
55
use CrestApps\CodeGenerator\Support\ValidationParser;
6+
use CrestApps\CodeGenerator\Support\OptimizerBase;
67

7-
class FieldOptimizer {
8+
class FieldOptimizer extends OptimizerBase
9+
{
810

911
/**
1012
* The field to optimize
@@ -100,7 +102,6 @@ protected function optimizeStringField()
100102
*/
101103
protected function optimizeRequiredField()
102104
{
103-
104105
if( !$this->parser->isRequired() || $this->parser->isNullable() || $this->parser->isConditionalRequired() )
105106
{
106107
$this->field->isNullable = true;
@@ -109,7 +110,7 @@ protected function optimizeRequiredField()
109110
return $this;
110111
}
111112

112-
/**
113+
/**
113114
* If the property name is "id" or if the field is primary or autoincrement.
114115
* Ensure, the datatype is set to be valid otherwise make it "int".
115116
* It also make sure the primary column does not appears on the views unless it specified
@@ -120,7 +121,7 @@ protected function optimizeRequiredField()
120121
*/
121122
protected function optimizePrimaryKey()
122123
{
123-
if( $this->isPrimaryField())
124+
if( $this->isPrimaryField($this->field))
124125
{
125126
if(!$this->isNumericField())
126127
{
@@ -159,16 +160,6 @@ protected function optimizePrimaryKey()
159160
return $this;
160161
}
161162

162-
/**
163-
* It checks if a giving field is a primary or not.
164-
*
165-
* @return bool
166-
*/
167-
protected function isPrimaryField()
168-
{
169-
return ($this->field->name == 'id' || $this->field->isAutoIncrement || $this->field->isPrimary);
170-
}
171-
172163
/**
173164
* It checks if the field is numeric type
174165
*

0 commit comments

Comments
 (0)