Skip to content

Commit 9d33f06

Browse files
committed
=Add feature to allow generating resouce from existing MySql database. Also, add a feature to generate fields-file form existing MySQL database.
1 parent d121ceb commit 9d33f06

13 files changed

+933
-143
lines changed

README.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ For full documentation and screenshots please visit <a href="https://crestapps.c
55

66
## Introduction
77

8-
A very clean code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migration or request form!
8+
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.
99

1010
## Features
1111

12-
* Create full resource with a single command.
12+
* Create full resource 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.
1515
* Create named routes.
@@ -29,43 +29,41 @@ A very clean code generator for Laravel framework that will save you time! This
2929

3030
## Installation
3131

32-
> If you don't already 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.
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.
3333
3434

35-
Using the command line execute the following command
35+
36+
To download this package into your laravel project, use the command-line to execute the following command
3637
```
3738
composer require crestapps/laravel-code-generator --dev
3839
```
3940

40-
Open the `config/app.php` file in your project and do the following two steps
41-
First, look for the `providers` array. Add the following service provider to it.
41+
To bootstrap the packages into your project, open the `config/app.php` file in your project and follow the
42+
4243

44+
First, look for the `providers` array. Add the following line to bootstrap laravel-code-generator to the framework.
4345
```
44-
//Add this line to bootstrap laravel-code-generator to the framework
4546
CrestApps\CodeGenerator\CodeGeneratorServiceProvider::class,
47+
```
4648

47-
//Only add this line if one does not already exists.
48-
//The following line will bootstrap LaravelCollective to the framework.
49+
Add the following line to bootstrap "LaravelCollective Forms & HTML package" to the framework.
50+
```
4951
Collective\Html\HtmlServiceProvider::class,
5052
```
5153

52-
Second, look for the `aliases` array in the `config/app.php` file. Add the following code to it only if `LaravelCollective Forms & HTML` package was already installed.
54+
Second, look for the `aliases` array. Then, add the following two lines to create an aliase for "LaravelCollective Forms & HTML package".
5355
```
54-
//Only add these two line if one does not already exists.
55-
//The following two line will finish bootstraping LaravelCollective to the framework
5656
'Form' => Collective\Html\FormFacade::class,
5757
'Html' => Collective\Html\HtmlFacade::class,
5858
```
5959

6060

61-
Finally, publish the config and the default template to start generating awesome code.
62-
61+
Finally, execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
6362
```
6463
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider"
6564
```
6665

67-
> 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 and not use [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") altogether.
68-
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.
6967
7068

7169
## Available Commands
@@ -86,11 +84,12 @@ php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServ
8684
* php artisan create:migration [table-name]
8785
* php artisan create:form-request [class-name]
8886
* php artisan create:language [language-file-name]
87+
* php artisan create:fields-file [table-name]
8988

9089
> Full documentation available at [CrestApps.com](https://crestapps.com/laravel-code-generator/docs/1.0 "Laravel Code Generator Documentation").
9190
9291

9392
## License
9493

95-
The Laravel-CodeGenerator is open-sourced software licensed under the <a href="https://opensource.org/licenses/MIT" target="_blank" title="MIT license">MIT license</a>
94+
The "LaravelCode Generator" package is an open-sourced software licensed under the <a href="https://opensource.org/licenses/MIT" target="_blank" title="MIT license">MIT license</a>
9695

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "crestapps/laravel-code-generator",
33
"license": "MIT",
4-
"description": "An awesome code generator for laravel framework.",
5-
"version": "v1.0.3",
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.4",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",

config/codegenerator.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@
9292

9393
/*
9494
|--------------------------------------------------------------------------
95-
| The data-value to eloquent method mapping.
95+
| The data-value to Eloquent method mapping.
9696
|--------------------------------------------------------------------------
9797
|
98-
| In here you can add more keys to the array and eloquent method name as the value.
99-
| A list of eloquent methods can be found on this link https://laravel.com/docs/5.3/migrations#creating-columns
98+
| In here you can add more keys to the array and Eloquent method name as the value.
99+
| A list of Eloquent methods can be found on this link https://laravel.com/docs/5.3/migrations#creating-columns
100100
| The only time you really have to add more items here is if you don't like using the existing data-value that are used
101101
| with the code generator.
102102
*/
@@ -148,5 +148,56 @@
148148
'unsignedsmallint' => 'unsignedSmallInteger',
149149
'unsignedtinyinteger' => 'unsignedTinyInteger',
150150
'uuid' => 'uuid'
151+
],
152+
153+
154+
/*
155+
|--------------------------------------------------------------------------
156+
| Eloquent method to html-type mapping
157+
|--------------------------------------------------------------------------
158+
|
159+
| This is the mapping used to convert database-column into html field
160+
*/
161+
'eloquent_type_to_html_type' =>
162+
[
163+
'char' => 'text',
164+
'date' => 'text',
165+
'dateTime' => 'text',
166+
'dateTimeTz' => 'text',
167+
'bigIncrements' => 'number',
168+
'bigIncrements' => 'number',
169+
'binary' => 'textarea',
170+
'boolean' => 'radio',
171+
'decimal' => 'number',
172+
'double' => 'number',
173+
'enum' => 'select',
174+
'float' => 'number',
175+
'integer' => 'number',
176+
'integer' => 'number',
177+
'ipAddress' => 'text',
178+
'json' => 'checkbox',
179+
'jsonb' => 'checkbox',
180+
'longText' => 'textarea',
181+
'macAddress' => 'text',
182+
'mediumInteger' => 'number',
183+
'mediumText' => 'textarea',
184+
'string' => 'text',
185+
'text' => 'textarea',
186+
'time' => 'text',
187+
'timeTz' => 'text',
188+
'tinyInteger' => 'number',
189+
'tinyInteger' => 'number',
190+
'timestamp' => 'text',
191+
'timestampTz' => 'text',
192+
'unsignedBigInteger' => 'number',
193+
'unsignedBigInteger' => 'number',
194+
'unsignedInteger' => 'number',
195+
'unsignedInteger' => 'number',
196+
'unsignedMediumInteger' => 'number',
197+
'unsignedMediumInteger' => 'number',
198+
'unsignedSmallInteger' => 'number',
199+
'unsignedSmallInteger' => 'number',
200+
'unsignedTinyInteger' => 'number',
201+
'uuid' => 'text',
151202
]
152203
];

src/CodeGeneratorServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function register()
5353
'CrestApps\CodeGenerator\Commands\CreateMigrationCommand',
5454
'CrestApps\CodeGenerator\Commands\CreateResourceCommand',
5555
'CrestApps\CodeGenerator\Commands\CreateViewLayoutCommand',
56-
'CrestApps\CodeGenerator\Commands\CreateLayoutCommand'
56+
'CrestApps\CodeGenerator\Commands\CreateLayoutCommand',
57+
'CrestApps\CodeGenerator\Commands\CreateFieldsFileCommand'
5758
);
5859
}
5960

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace CrestApps\CodeGenerator\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use CrestApps\CodeGenerator\Support\GenerateFormViews;
7+
use CrestApps\CodeGenerator\Support\MysqlParser;
8+
use CrestApps\CodeGenerator\Support\Field;
9+
use CrestApps\CodeGenerator\Support\Helpers;
10+
use CrestApps\CodeGenerator\traits\CommonCommand;
11+
use DB;
12+
use File;
13+
use Exception;
14+
15+
class CreateFieldsFileCommand extends Command
16+
{
17+
use CommonCommand;
18+
19+
/**
20+
* The name and signature of the console command.
21+
*
22+
* @var string
23+
*/
24+
protected $signature = 'create:fields-file
25+
{table-name : The dtabase table name to fetch the field from.}
26+
{--database-name= : The database name the table is stored in.}
27+
{--fields-filename= : The destination file name to create.}
28+
{--force : This option will override the view if one already exists.}';
29+
30+
/**
31+
* The console command description.
32+
*
33+
* @var string
34+
*/
35+
protected $description = 'Create json fields file from existing database table.';
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* @return void
41+
*/
42+
public function handle()
43+
{
44+
$fields = $this->getFields();
45+
$final = [];
46+
foreach($fields as $field)
47+
{
48+
$final[] = $field->toArray();
49+
}
50+
51+
if(File::exists($this->getDestinationFullname()) && !$this->isForce())
52+
{
53+
throw new Exception('The file ' . $this->getFilename() . ' already exists. To override it try passing the --force option to override it.' );
54+
}
55+
56+
if($this->createFile($this->getDestinationFullname(), json_encode($final, JSON_PRETTY_PRINT)))
57+
{
58+
$this->info('A new Fields file have been created.');
59+
}
60+
else
61+
{
62+
$this->error('Something went wrong while trying to create the fields file.');
63+
}
64+
65+
}
66+
67+
/**
68+
* Create the destenation file.
69+
*
70+
* @param string $fullname
71+
* @param string $content
72+
*
73+
* @return bool
74+
*/
75+
protected function createFile($fullname, $content)
76+
{
77+
$this->createDirectory(dirname($fullname));
78+
79+
return File::put($fullname, $content);
80+
}
81+
82+
/**
83+
* Gets the full name of the file to be created.
84+
*
85+
* @return string
86+
*/
87+
protected function getDestinationFullname()
88+
{
89+
return $this->getFieldsFilePath() . $this->getFilename();
90+
}
91+
92+
/**
93+
* Gets the fields array.
94+
*
95+
* @return array
96+
*/
97+
protected function getFields()
98+
{
99+
$driver = strtolower(DB::getDriverName());
100+
101+
if($driver == 'mysql')
102+
{
103+
return (new MysqlParser($this->getTableName(), $this->getDatabaseName()))->getFields();
104+
}
105+
else
106+
{
107+
throw new Exception('The database driver user is not supported!');
108+
}
109+
110+
return [];
111+
112+
}
113+
114+
/**
115+
* Checks the options to see if the force command is provided
116+
*
117+
* @return bool
118+
*/
119+
protected function isForce()
120+
{
121+
return $this->option('force');
122+
}
123+
124+
/**
125+
* Gets the destenation filename.
126+
*
127+
* @return string
128+
*/
129+
protected function getFilename()
130+
{
131+
return Helpers::postFixWith(trim($this->option('fields-filename')) ?: $this->getTableName(), '.json');
132+
}
133+
134+
/**
135+
* Gets the database name to use.
136+
*
137+
* @return string
138+
*/
139+
protected function getDatabaseName()
140+
{
141+
return trim($this->option('database-name')) ?: DB::getConfig('database');
142+
}
143+
144+
/**
145+
* Gets the table name to use.
146+
*
147+
* @return string
148+
*/
149+
protected function getTableName()
150+
{
151+
return trim($this->argument('table-name'));
152+
}
153+
}

src/commands/CreateResourceCommand.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CreateResourceCommand extends Command
4141
{--engine-name= : A specific engine name.}
4242
{--layout-name=layouts.app : This will extract the validation into a request form class.}
4343
{--template-name= : The template name to use when generating the code.}
44+
{--table-exists : This option will attempt to fetch the field from existing database table.}
4445
{--force : This option will override the controller if one already exists.}';
4546

4647

@@ -60,6 +61,16 @@ public function handle()
6061
{
6162
$input = $this->getCommandInput();
6263

64+
if($input->tableExists)
65+
{
66+
$input->fields = null;
67+
$input->fieldsFile = $input->table . '.json';
68+
$input->withoutMigration = true;
69+
70+
$this->createFieldsFile($input);
71+
72+
}
73+
6374
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
6475

6576
if(empty($fields) || !isset($fields[0]))
@@ -108,6 +119,23 @@ protected function createMigration($input)
108119
return $this;
109120
}
110121

122+
/**
123+
* Execute the command that generate fields' file.
124+
*
125+
* @param object $input
126+
* @return $this
127+
*/
128+
protected function createFieldsFile($input)
129+
{
130+
$this->callSilent('create:fields-file',
131+
[
132+
'table-name' => $input->table,
133+
'--force' => $input->force
134+
]);
135+
136+
return $this;
137+
}
138+
111139
/**
112140
* Execute the command that generates a language
113141
*
@@ -259,11 +287,12 @@ protected function getCommandInput()
259287
$engineName = trim($this->option('engine-name'));
260288
$template = $this->getTemplateName();
261289
$layoutName = trim($this->option('layout-name')) ?: 'layouts.app';
290+
$tableExists = $this->option('table-exists');
262291

263292
return (object) compact('modelName','controllerName','viewsDirectory','prefix','perPage','fields','force',
264293
'languageFileName','fieldsFile','formRequest','modelDirectory','table','fillable','primaryKey',
265294
'relationships','useSoftDelete','withoutTimeStamps','controllerDirectory','withoutMigration',
266-
'migrationClass','connectionName','indexes','foreignKeys','engineName','layoutName','template');
295+
'migrationClass','connectionName','indexes','foreignKeys','engineName','layoutName','template','tableExists');
267296
}
268297

269298
}

0 commit comments

Comments
 (0)