Skip to content

Commit ea993e2

Browse files
committed
=Fix multiple bugs with the views. Add client-side validation. Add functions to cover every LaraelCollective methods. Add multipleSelect and checkbox auto handling.
1 parent 13ee99a commit ea993e2

40 files changed

+1457
-148
lines changed

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
# An awesome code generator for laravel framework
1+
# An awesome code generator for laravel framework - with client-side validation
22

3-
For full documentation 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.0" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
44

55

66
## Introduction
77

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, migration or request form!
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!
99

1010
## Features
1111

12-
* Customizable templates to enable you to change the standard look and feel of your application.
13-
* Create standard CRUD controllers with standard or form-request validation.
12+
* Create full resource with a single command.
13+
* Create standard CRUD controllers with simple or form-request validation.
1414
* Create model.
1515
* Create named routes.
1616
* Create standard CRUD views.
17-
* Create Form-Request
18-
* Create layouts for your views.
19-
* Create languages/Locale files.
20-
* Json support to define your fields.
17+
* Very flexible and rich with configurable options.
18+
* (Beta) Client-side validation.
19+
* File uploading handling.
20+
* Auto multiple-response storing in the database.
21+
* Create form-request.
22+
* 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.
25+
* Create code to upload file.
26+
* Lots of documentation.
2127

2228

2329

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": "An awesome code generator for laravel framework.",
5-
"version": "v1.0.2",
5+
"version": "v1.0.3",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",

src/CodeGeneratorServiceProvider.php

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

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

3131
$this->createDirectory(base_path('resources\\codegenerator-files'));
@@ -52,7 +52,8 @@ public function register()
5252
'CrestApps\CodeGenerator\Commands\CreateRoutesCommand',
5353
'CrestApps\CodeGenerator\Commands\CreateMigrationCommand',
5454
'CrestApps\CodeGenerator\Commands\CreateResourceCommand',
55-
'CrestApps\CodeGenerator\Commands\CreateViewLayoutCommand'
55+
'CrestApps\CodeGenerator\Commands\CreateViewLayoutCommand',
56+
'CrestApps\CodeGenerator\Commands\CreateLayoutCommand'
5657
);
5758
}
5859

src/commands/CreateControllerCommand.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function buildClass($name)
8686
->replaceFormRequestName($stub, $formRequestName)
8787
->replaceFormRequestFullName($stub, $this->getRequestsNamespace() . $formRequestName)
8888
->replacePaginationNumber($stub, $input->perPage)
89-
->processModelData($stub, $this->isContainMultipleAnswers($fields))
89+
//->processModelData($stub, $this->isContainMultipleAnswers($fields))
9090
->replaceFileSnippet($stub, $this->getFileReadySnippet($fields))
9191
->replaceFileMethod($stub, $this->getUploadFileMethod($fields))
9292
->replaceClass($stub, $name);
@@ -122,11 +122,12 @@ protected function getRequestsNamespace()
122122
*
123123
* @return string
124124
*/
125+
/*
125126
protected function getModelDataConversionMethod()
126127
{
127128
return $this->getStubContent('controller-request-parameters', $this->getTemplateName());
128129
}
129-
130+
8/
130131
/**
131132
* Checks if a giving fields array conatins at least one multiple answers
132133
*
@@ -135,6 +136,7 @@ protected function getModelDataConversionMethod()
135136
*
136137
* @return $this
137138
*/
139+
/*
138140
protected function processModelData(& $stub, $withMultipleAnswers)
139141
{
140142
if($withMultipleAnswers)
@@ -149,7 +151,7 @@ protected function processModelData(& $stub, $withMultipleAnswers)
149151
150152
return $this;
151153
}
152-
154+
*/
153155
/**
154156
* Checks if a giving fields array conatins at least one multiple answers
155157
*
@@ -243,12 +245,14 @@ protected function getCommandInput()
243245
*
244246
* @return $this
245247
*/
248+
/*
246249
protected function replaceModelDataMethod(&$stub, $method)
247250
{
248251
$stub = str_replace('{{modelDataMethod}}', $method, $stub);
249252
250253
return $this;
251254
}
255+
*/
252256

253257
/**
254258
* Replace the modelData for the given stub.
@@ -258,12 +262,14 @@ protected function replaceModelDataMethod(&$stub, $method)
258262
*
259263
* @return $this
260264
*/
265+
/*
261266
protected function replaceModelData(&$stub, $method)
262267
{
263268
$stub = str_replace('{{modelData}}', $method, $stub);
264269
265270
return $this;
266271
}
272+
*/
267273

268274
/**
269275
* Replace the modelFullName for the given stub.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace CrestApps\CodeGenerator\Commands;
4+
5+
use File;
6+
use Illuminate\Console\Command;
7+
use CrestApps\CodeGenerator\Support\Helpers;
8+
9+
use CrestApps\CodeGenerator\Traits\CommonCommand;
10+
use Exception;
11+
12+
class CreateLayoutCommand extends Command
13+
{
14+
15+
use CommonCommand;
16+
17+
/**
18+
* The name and signature of the console command.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'create:layout
23+
{application-name : The name of your application.}
24+
{--layout-filename=app : The layout file name to be created.}
25+
{--layout-directory=layouts : The directory of the layouts.}
26+
{--without-validation : This option will create a layout without client-side validation.}
27+
{--template-name= : The template name to use when generating the code.}
28+
{--force : Override existsing layout.}';
29+
30+
/**
31+
* The console command description.
32+
*
33+
* @var string
34+
*/
35+
protected $description = 'Create layout for the views.';
36+
37+
/**
38+
* Create a new command instance.
39+
*
40+
* @return void
41+
*/
42+
public function __construct()
43+
{
44+
parent::__construct();
45+
}
46+
47+
/**
48+
* Execute the console command.
49+
*
50+
* @return void
51+
*/
52+
public function handle()
53+
{
54+
$this->call('create:views-layout',
55+
[
56+
'application-name' => $this->argument('application-name'),
57+
'--layout-filename' => $this->option('layout-filename'),
58+
'--layout-directory' => $this->option('layout-directory'),
59+
'--without-validation' => $this->option('without-validation'),
60+
'--template-name' => $this->option('template-name'),
61+
'--force' => $this->option('force')
62+
]
63+
);
64+
65+
}
66+
67+
/**
68+
* Creates a new file.
69+
*
70+
* @param string $filename
71+
* @param string $content
72+
* @param bool $force
73+
*
74+
* @return $this
75+
*/
76+
protected function makeFile($filename, $content, $force)
77+
{
78+
if($this->fileExists($filename, $force))
79+
{
80+
throw new Exception('The destenation file already exists. To override the existing file, pass "--force" option.');
81+
}
82+
83+
if(!File::put($filename, $content))
84+
{
85+
throw new Exception('Unexpected error occurred while trying to create the file. please try your request again.');
86+
}
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* Checks if a file exists or not.
93+
* if the file exists and $force is set to true the method will return true.
94+
*
95+
* @param string $filename
96+
* @param bool $force
97+
*
98+
* @return bool
99+
*/
100+
protected function fileExists($filename, $force)
101+
{
102+
return $force ? false : File::exists($filename);
103+
}
104+
105+
/**
106+
* Gets the destenation path
107+
*
108+
* @param string $path
109+
*
110+
* @return string
111+
*/
112+
protected function getDestenationPath($path)
113+
{
114+
if(!empty($path) )
115+
{
116+
$path = Helpers::getPathWithSlash($path);
117+
}
118+
119+
return $this->getViewsPath() . $path;
120+
}
121+
122+
/**
123+
* Gets a clean user inputs.
124+
*
125+
* @return object
126+
*/
127+
protected function getCommandInput()
128+
{
129+
$appName = trim($this->argument('application-name'));
130+
$layoutFileName = Helpers::postFixWith(trim($this->option('layout-filename')) ?: 'layout-filename', '.blade.php');
131+
$layoutDirectory = trim($this->option('layout-directory'));
132+
$withoutValidation = $this->option('without-validation');
133+
$force = $this->option('force');
134+
135+
return (object) compact('appName','layoutFileName','layoutDirectory','withoutValidation','force');
136+
}
137+
138+
/**
139+
* Replace the applicationName fo the given stub.
140+
*
141+
* @param string $stub
142+
* @param string $appName
143+
*
144+
* @return $this
145+
*/
146+
protected function replaceApplicationName(&$stub, $appName)
147+
{
148+
$stub = str_replace('{{applicationName}}', $appName, $stub);
149+
150+
return $this;
151+
}
152+
}

src/commands/CreateMigrationCommand.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class CreateMigrationCommand extends Command
2828
{--fields= : The fields to create the validation rules from.}
2929
{--fields-file= : File name to import fields from.}
3030
{--template-name= : The template name to use when generating the code.}
31-
{--force : This option will override the form-request if one already exists.}';
31+
{--without-timestamps : Prevent Eloquent from maintaining both created_at and the updated_at properties.}
32+
{--force : This option will override the migration if one already exists.}';
3233

3334
/**
3435
* The console command description.
@@ -66,6 +67,11 @@ public function handle()
6667
$stub = $this->getStubContent('migration', $input->template);
6768
$fields = $this->getFields($input->fields, 'migration', $input->fieldsFile);
6869

70+
if(count($fields) == 0)
71+
{
72+
throw new Exception('You must provide at least one field to generate the migration');
73+
}
74+
6975
$properites = $this->getTableProperties($fields, $input);
7076

7177
$this->replaceSchemaUp($stub, $this->getSchemaUpCommand($input, $properites))
@@ -86,6 +92,7 @@ protected function getTableProperties(array $fields, $input)
8692

8793
$this->addEngineName($properties, $input->engine)
8894
->addPrimaryField($properties, $this->getPrimaryField($fields))
95+
->addTimestamps($properties, $input->withoutTimestamps)
8996
->addFieldProperties($properties, $fields)
9097
->addIndexes($properties, $input->indexes)
9198
->addForeignConstraints($properties, $input->keys);
@@ -342,7 +349,7 @@ protected function addFieldProperties(& $properties, array $fields)
342349

343350
foreach($fields as $field)
344351
{
345-
if($field instanceof Field && $field != $primaryField)
352+
if($field instanceof Field && $field != $primaryField && !is_null($primaryField))
346353
{
347354
$this->addFieldType($properties, $field)
348355
->addFieldComment($properties, $field)
@@ -711,7 +718,7 @@ protected function addEngineName(& $property, $engineName)
711718
* @param Field $field
712719
* @return $this
713720
*/
714-
protected function addPrimaryField(& $property, Field $field)
721+
protected function addPrimaryField(& $property, Field $field = null)
715722
{
716723
if(!is_null($field))
717724
{
@@ -723,6 +730,24 @@ protected function addPrimaryField(& $property, Field $field)
723730
return $this;
724731
}
725732

733+
/**
734+
* Adds 'updated_at' and 'created_at' columns to a giving $propery
735+
*
736+
* @param string $property
737+
* @param Field $field
738+
* @return $this
739+
*/
740+
protected function addTimestamps(& $property, $without)
741+
{
742+
if(!$without)
743+
{
744+
$property .= sprintf("%s()", $this->getPropertyBase('timestamps'));
745+
$this->addFieldPropertyClousure($property);
746+
}
747+
748+
return $this;
749+
}
750+
726751
/**
727752
* Gets the correct eloquent increment method from the giving type
728753
*
@@ -770,7 +795,10 @@ protected function getCommandInput()
770795
$force = $this->option('force');
771796
$keys = $this->getKeysCollections(trim($this->option('foreign-keys')));
772797
$template = $this->getTemplateName();
773-
return (object) compact('tableName','className','connection','engine','fields','fieldsFile','force','indexes','keys','template');
798+
$withoutTimestamps = $this->option('without-timestamps');
799+
800+
return (object) compact('tableName','className','connection','engine',
801+
'fields','fieldsFile','force','indexes','keys','template','withoutTimestamps');
774802
}
775803

776804

0 commit comments

Comments
 (0)