Skip to content

Commit 3282cbd

Browse files
committed
=Fixes a bug in converting MySQL Parser. Also, addes a new option for the command that generated fields-file
1 parent ecb6515 commit 3282cbd

File tree

11 files changed

+150
-107
lines changed

11 files changed

+150
-107
lines changed

README.md

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# An awesome code generator for laravel framework - with client-side validation
22

3-
For full documentation and screenshots please visit <a href="https://crestapps.com/laravel-code-generator/docs/1.0" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
3+
For full documentation and screenshots please visit <a href="https://crestapps.com/laravel-code-generator/docs/1.1" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
44

55

66
## Introduction
@@ -9,7 +9,7 @@ A clean code generator for Laravel framework that will save you time! This aweso
99

1010
## Features
1111

12-
* Create full resource using a single command with/without <strong>migration</strong> or from <strong>existing database</strong>.
12+
* Create full resources using a single command with/without <strong>migration</strong> or from <strong>existing database</strong>.
1313
* Create standard CRUD controllers with simple or form-request validation.
1414
* Create model with relations.
1515
* Create named routes.
@@ -20,57 +20,42 @@ A clean code generator for Laravel framework that will save you time! This aweso
2020
* Auto multiple-response storing in the database.
2121
* Create form-request.
2222
* Customizable view’s templates to enable you to change the standard look and feel of your application.
23-
* Create view's layouts.
24-
* Change the template at run time.
23+
* Create view's layouts with and without client-side validation.
24+
* Change the template at run time to generate different views.
2525
* Create code to upload file.
26+
* Ability to generate views with and without Laravel-Collective.
2627
* Lots of documentation.
2728

2829

29-
3030
## Installation
3131

32-
> If you don't have LaravelCollective Forms & HTML package installed, it will be installed for you. However you'll still have to update your config/app.php to bootstrap the package to the framework.
33-
34-
3532

3633
To download this package into your laravel project, use the command-line to execute the following command
3734
```
3835
composer require crestapps/laravel-code-generator --dev
3936
```
4037

41-
To bootstrap the packages into your project, open the `config/app.php` file in your project and follow the
38+
To bootstrap the packages into your project, open the `config/app.php` file in your project. Then, look for the providers array.
4239

40+
Add the following line to bootstrap laravel-code-generator to the framework.
4341

44-
First, look for the `providers` array. Add the following line to bootstrap laravel-code-generator to the framework.
4542
```
4643
CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class,
4744
```
4845

49-
Add the following line to bootstrap "LaravelCollective Forms & HTML package" to the framework.
50-
```
51-
Collective\Html\HtmlServiceProvider::class,
52-
```
53-
54-
Second, look for the `aliases` array. Then, add the following two lines to create an aliase for "LaravelCollective Forms & HTML package".
55-
```
56-
'Form' => Collective\Html\FormFacade::class,
57-
'Html' => Collective\Html\HtmlFacade::class,
58-
```
59-
60-
6146
Finally, execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
6247
```
63-
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider"
48+
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default
6449
```
6550

66-
> A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of cource you can use your own layout. You'll only need to include [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") in your layout for the default templates to work properly. Additionally, you can chose to you design your own templetes using a different framework.
51+
> A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of cource you can use your own layout. You'll only need to include [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") in your layout for the default templates to work properly. Additionally, you can chose to you design your own templetes using a different or no css framework.
6752
6853

6954
## Available Commands
7055

71-
> The command in wetween the square brackets [] must be replaced with a variable of your choice.
56+
> The command in between the square brackets [] must be replaced with a variable of your choice.
7257
73-
* php artisan create:resource [model-name]
58+
* php artisan create:resources [model-name]
7459
* php artisan create:layout [application-name]
7560
* php artisan create:controller [controller-name]
7661
* php artisan create:model [model-name]
@@ -86,7 +71,7 @@ php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServ
8671
* php artisan create:language [language-file-name]
8772
* php artisan create:fields-file [table-name]
8873

89-
> Full documentation available at [CrestApps.com](https://crestapps.com/laravel-code-generator/docs/1.0 "Laravel Code Generator Documentation").
74+
> Full documentation available at [CrestApps.com](https://crestapps.com/laravel-code-generator/docs/1.1 "Laravel Code Generator Documentation").
9075
9176

9277
## License

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"name": "crestapps/laravel-code-generator",
33
"license": "MIT",
4-
"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 on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application",
5-
"version": "v1.0.5",
4+
"description": "A clean code generator for Laravel framework that will save you time!
5+
This awesome tool will help you generate resources like views, controllers, routes, migrations, languages or request-forms!
6+
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.",
7+
"version": "v1.1.0",
68
"keywords": [
79
"laravel","crud","crud generator",
810
"laravel crud generator","laravel crud builder",

src/CodeGeneratorServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function register()
5555
'CrestApps\CodeGenerator\Commands\CreateFormRequestCommand',
5656
'CrestApps\CodeGenerator\Commands\CreateRoutesCommand',
5757
'CrestApps\CodeGenerator\Commands\CreateMigrationCommand',
58-
'CrestApps\CodeGenerator\Commands\CreateResourceCommand',
58+
'CrestApps\CodeGenerator\Commands\CreateResourcesCommand',
5959
'CrestApps\CodeGenerator\Commands\CreateViewLayoutCommand',
6060
'CrestApps\CodeGenerator\Commands\CreateLayoutCommand',
6161
'CrestApps\CodeGenerator\Commands\CreateFieldsFileCommand'

src/commands/CreateResourceCommand.php renamed to src/Commands/CreateResourcesCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use CrestApps\CodeGenerator\Support\Helpers;
77
use CrestApps\CodeGenerator\Traits\CommonCommand;
88

9-
class CreateResourceCommand extends Command
9+
class CreateResourcesCommand extends Command
1010
{
1111
use CommonCommand;
1212

@@ -15,7 +15,7 @@ class CreateResourceCommand extends Command
1515
*
1616
* @var string
1717
*/
18-
protected $signature = 'create:resource
18+
protected $signature = 'create:resources
1919
{model-name : The model name that this resource will represent.}
2020
{--controller-name= : The name of the controler.}
2121
{--controller-directory= : The directory where the controller should be created under. }

src/DatabaseParsers/MysqlParser.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,36 @@ protected function setOptions(Field & $field, $type)
6666
{
6767
if( ($options = $this->getOptions($type)) !== null )
6868
{
69-
foreach($options as $option)
69+
if(empty($this->languages))
7070
{
71-
$field->addOption($this->getLabelName($option), $this->tableName, true, $this->locale, $option);
71+
return $this->addOptionsFor($field, $options, true, $this->locale);
7272
}
73+
74+
foreach($this->languages as $language)
75+
{
76+
$this->addOptionsFor($field, $options, false, $language);
77+
}
78+
79+
}
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* Adds options for a giving field.
86+
*
87+
* @param CrestApps\CodeGenerator\Support\Field $field
88+
* @param array $options
89+
* @param bool $isPlain
90+
* @param string $locale
91+
*
92+
* @return $this
93+
*/
94+
protected function addOptionsFor(Field & $field, array $options, $isPlain, $locale)
95+
{
96+
foreach($options as $option)
97+
{
98+
$field->addOption($this->getLabelName($option), $this->tableName, $isPlain, $locale, $option);
7399
}
74100

75101
return $this;

src/DatabaseParsers/ParserBase.php

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use CrestApps\CodeGenerator\Support\Field;
55
use CrestApps\CodeGenerator\Support\FieldOptimizer;
6-
76
use Exception;
87
use App;
98
use DB;
@@ -38,19 +37,28 @@ abstract class ParserBase
3837
*/
3938
protected $fields;
4039

40+
/**
41+
* The langugaes to create labels form.
42+
*
43+
* @var array
44+
*/
45+
protected $langugaes;
46+
4147
/**
4248
* Creates a new field instance.
4349
*
4450
* @param string $tableName
4551
* @param string $databaseName
52+
* @param array $langugaes
4653
*
4754
* @return void
4855
*/
49-
public function __construct($tableName, $databaseName)
56+
public function __construct($tableName, $databaseName, array $languages = [])
5057
{
5158
$this->tableName = $tableName;
5259
$this->databaseName = $databaseName;
53-
$this->locale = App::getLocale();
60+
$this->languages = $languages;
61+
$this->locale = App::getLocale();
5462
}
5563

5664
/**
@@ -75,20 +83,6 @@ public function getFields()
7583
return $this->fields;
7684
}
7785

78-
/**
79-
* Gets column meta info from the information schema.
80-
*
81-
* @return array
82-
*/
83-
abstract protected function getColumn();
84-
85-
/**
86-
* Gets the field after transfering it from a giving query object.
87-
*
88-
* @return CrestApps\CodeGenerator\Support\Field;
89-
*/
90-
abstract protected function getTransfredField($column);
91-
9286
/**
9387
* Gets array of field after transfering each column meta into field.
9488
*
@@ -231,12 +225,34 @@ protected function setDefault(Field & $field, $default)
231225
*/
232226
protected function setLabel(Field & $field, $name)
233227
{
234-
235-
$field->addLabel( $this->getLabelName($name), $this->tableName, true, $this->locale);
228+
if(empty($this->languages))
229+
{
230+
$field->addLabel($this->getLabelName($name), $this->tableName, true, $this->locale);
236231

237-
return $this;
232+
return $this;
233+
}
234+
235+
return $this->addLabelForLanguages($field, $name);
238236
}
239237

238+
/**
239+
* Add labels for a giving field.
240+
*
241+
* @param CrestApps\CodeGenerator\Support\Field $field
242+
* @param string $name
243+
*
244+
* @return $this
245+
*/
246+
protected function addLabelForLanguages(Field $field, $name)
247+
{
248+
foreach($this->languages as $language)
249+
{
250+
$field->addLabel($this->getLabelName($name), $this->tableName, false, $language);
251+
}
252+
253+
return $this;
254+
}
255+
240256
/**
241257
* Set the keys for a giving field.
242258
*
@@ -320,4 +336,18 @@ protected function setComment(Field & $field, $comment)
320336

321337
return $this;
322338
}
339+
340+
/**
341+
* Gets column meta info from the information schema.
342+
*
343+
* @return array
344+
*/
345+
abstract protected function getColumn();
346+
347+
/**
348+
* Gets the field after transfering it from a giving query object.
349+
*
350+
* @return CrestApps\CodeGenerator\Support\Field;
351+
*/
352+
abstract protected function getTransfredField($column);
323353
}

src/commands/CreateFieldsFileCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CreateFieldsFileCommand extends Command
2424
{table-name : The dtabase table name to fetch the field from.}
2525
{--database-name= : The database name the table is stored in.}
2626
{--fields-filename= : The destination file name to create.}
27+
{--translation-for= : A comma seperated string of languages to create fields for.}
2728
{--force : This option will override the view if one already exists.}';
2829

2930
/**
@@ -99,7 +100,7 @@ protected function getFields()
99100

100101
if($driver == 'mysql')
101102
{
102-
return (new MysqlParser($this->getTableName(), $this->getDatabaseName()))->getFields();
103+
return (new MysqlParser($this->getTableName(), $this->getDatabaseName(), $this->getLangugaes()))->getFields();
103104
}
104105
else
105106
{
@@ -149,4 +150,14 @@ protected function getTableName()
149150
{
150151
return trim($this->argument('table-name'));
151152
}
153+
154+
/**
155+
* Gets the languages to create lang keys for.
156+
*
157+
* @return array
158+
*/
159+
protected function getLangugaes()
160+
{
161+
return Helpers::convertStringToArray($this->option('translation-for'));
162+
}
152163
}

src/support/Field.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ public function toArray()
348348
{
349349
return [
350350
'name' => $this->name,
351-
'labels' => $this->labelsToJson($this->getLabels()),
351+
'labels' => $this->labelsToRaw($this->getLabels()),
352352
'html-type' => $this->htmlType,
353-
'options' => $this->optionsToJson($this->getOptions()),
353+
'options' => $this->optionsToRaw($this->getOptions()),
354354
'html-value' => $this->htmlValue,
355355
'validation' => implode('|', $this->validationRules),
356356
'is-on-index' => $this->isOnIndexView,
@@ -384,19 +384,34 @@ public function toJson()
384384
return json_encode($this->toArray());
385385
}
386386

387-
protected function labelsToJson(array $labels)
387+
/**
388+
* Gets current labels in a raw format.
389+
*
390+
* @return mix (string|object)
391+
*/
392+
protected function labelsToRaw(array $labels)
388393
{
389394
$final = [];
390395

391396
foreach($labels as $label)
392397
{
398+
if($label->isPlain)
399+
{
400+
return $label->text;
401+
}
402+
393403
$final[$label->lang] = $label->text;
394404
}
395405

396406
return (object) $final;
397407
}
398408

399-
protected function optionsToJson(array $options)
409+
/**
410+
* Gets current Options into a raw format.
411+
*
412+
* @return object
413+
*/
414+
protected function optionsToRaw(array $options)
400415
{
401416
$final = [];
402417

0 commit comments

Comments
 (0)