Skip to content

Commit 2dc0577

Browse files
committed
Create v2.2.3 along with more documentation
1 parent a78b079 commit 2dc0577

File tree

8 files changed

+72
-82
lines changed

8 files changed

+72
-82
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Upgrade
2-
- If you are upgrading from v2.0 or v2.1 to v2.3 make sure you publish the vendor resource. There are some changes to the templates that required to be updated.
2+
- If you are upgrading from v2.0, v2.1 or v2.2 to v2.3 make sure you publish the vendor resource. There are some updates to the config file.
3+
- If you are upgrading from v2.0 or v2.1 to v2.2 make sure you publish the vendor resource. There are some changes to the templates that required to be updated.
34
-- If you are upgrading any version prior v2.2 follow the upgrate instruction on https://crestapps.com/laravel-code-generator/docs/2.2#upgrade-guide
45

56
### Options Changes

README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# An awesome code generator for laravel framework - with client-side validation
2-
3-
For full documentation and live demo please visit <a href="https://crestapps.com/laravel-code-generator/docs/2.2" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
4-
51
## Introduction
62

7-
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.
3+
An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! 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.
4+
5+
For full documentation and live demo please visit <a href="https://crestapps.com/laravel-code-generator/docs/2.2" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
86

97
## Features
108

@@ -40,31 +38,32 @@ A clean code generator for Laravel framework that will save you time! This aweso
4038

4139
1. To download this package into your laravel project, use the command-line to execute the following command
4240

43-
```
44-
composer require crestapps/laravel-code-generator --dev
45-
```
41+
```
42+
composer require crestapps/laravel-code-generator --dev
43+
```
4644

4745
2. **(You may skip this step when using Laravel >= 5.5)** To bootstrap the packages into your project while using command-line only, open the app/Providers/AppServiceProvider.php file in your project. Then, add the following code to the register() method.
4846

49-
Add the following line to bootstrap laravel-code-generator to the framework.
47+
Add the following line to bootstrap laravel-code-generator to the framework.
5048

51-
```
52-
if ($this->app->runningInConsole()) {
53-
$this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider');
54-
}
55-
```
49+
```
50+
if ($this->app->runningInConsole()) {
51+
$this->app->register('CrestApps\CodeGenerator\CodeGeneratorServiceProvider');
52+
}
53+
```
5654

5755
3. Execute the following command from the command-line to publish the package's config and the default template to start generating awesome code.
58-
```
59-
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default
60-
```
56+
57+
```
58+
php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServiceProvider" --tag=default
59+
```
6160

6261
> 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.
6362
6463
## Lessons
65-
Checkout our YouTube channel
66-
> https://youtu.be/l21qNcsMAWg
67-
> https://youtu.be/infoecfXOCw
64+
Checkout our YouTube channel on <a href="https://www.youtube.com/channel/UCkEd0nOoRf3o0ahspAu7Y9w/videos" target="_blank" title="CrestApps YouTube Channel">YouTube.com</a>
65+
* https://youtu.be/l21qNcsMAWg
66+
* https://youtu.be/infoecfXOCw
6867

6968

7069
## Available Commands

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": "v2.2.2",
5+
"version": "v2.2.3",
66
"keywords": [
77
"laravel","crud","crud generator",
88
"laravel crud generator","laravel crud builder",

config/codegenerator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,17 @@
208208
| Key phrases that are will be used to determine if a field name should be
209209
| used for header.
210210
|--------------------------------------------------------------------------
211+
|
212+
| You may use * as a while card in the name. For example, "head*" will
213+
| match any field name that starts with the word "head"
211214
|
212215
*/
213216
'common_header_patterns' => [
214217
'title',
215218
'name',
216219
'label',
217-
'header',
218220
'subject',
219-
'head',
221+
'head*',
220222
],
221223

222224
/*
@@ -284,6 +286,7 @@
284286
[
285287
'match' => '*',
286288
'set' => [
289+
// You may use any of the field templates to create the label
287290
'labels' => '[% field_name_title %]',
288291
],
289292
],
@@ -302,7 +305,7 @@
302305
],
303306
],
304307
[
305-
'match' => ['title', 'name', 'label', 'header'],
308+
'match' => ['title', 'name', 'label', 'subject', 'head*'],
306309
'set' => [
307310
'is-nullable' => false,
308311
'data-type' => 'string',

src/Models/Field.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,6 @@ class Field implements JsonWriter
141141
*/
142142
private $labels = [];
143143

144-
/**
145-
* The languages for the labels
146-
*
147-
* @var array
148-
*/
149-
private $languages = [];
150-
151144
/**
152145
* The html-type of the field
153146
*
@@ -369,15 +362,13 @@ class Field implements JsonWriter
369362
*
370363
* @param string $name
371364
* @param string $localeGroup
372-
* @param array $languages
373365
*
374366
* @return void
375367
*/
376-
public function __construct($name, $localeGroup, $languages)
368+
public function __construct($name, $localeGroup)
377369
{
378370
$this->name = $name;
379371
$this->localeGroup = $localeGroup;
380-
$this->languages = $languages;
381372
$this->defaultLang = App::getLocale();
382373
}
383374

@@ -1133,16 +1124,6 @@ protected function getLabelsFromProperties(array $properties)
11331124
];
11341125
}
11351126

1136-
/**
1137-
* Checks if the field has languages
1138-
*
1139-
* @return bool
1140-
*/
1141-
protected function isMultiLingual()
1142-
{
1143-
return !empty($this->langauges);
1144-
}
1145-
11461127
/**
11471128
* It set the validationRules property for a giving field
11481129
*
@@ -1785,19 +1766,18 @@ public static function getNameFromArray(array $properties)
17851766
*
17861767
* @param array $properties
17871768
* @param string $localeGroup
1788-
* @param array $languages
17891769
*
17901770
* @return $this
17911771
*/
1792-
public static function fromArray(array $properties, $localeGroup, $languages = [])
1772+
public static function fromArray(array $properties, $localeGroup)
17931773
{
17941774
$fieldName = self::getNameFromArray($properties);
17951775

17961776
if (!Field::isValidHtmlType($properties)) {
17971777
unset($properties['html-type']);
17981778
}
17991779

1800-
$field = new self($fieldName, $localeGroup, $languages);
1780+
$field = new self($fieldName, $localeGroup);
18011781

18021782
$field->setPredefindProperties($properties)
18031783
->setLabelsProperty($properties)

src/Models/ForeignRelationship.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,24 @@ public function getName()
169169
*/
170170
protected function guessForeignField()
171171
{
172+
// First we try to find a column that match the header pattern
172173
$columns = $this->getModelColumns();
173174
$names = Config::getHeadersPatterns();
174-
175175
foreach ($columns as $column) {
176-
$matchedPattern = '';
177-
if (Helpers::strIs($names, $column, $matchedPattern)) {
176+
if (Helpers::strIs($names, $column)) {
177+
// At this point a column that match the header patter was found
178178
return $column;
179179
}
180180
}
181181

182+
// At this point we know no column that match the header patters found.
183+
// Second we try to find a non-primary/non-foreign key column
182184
$primary = $this->getPrimaryKeyForForeignModel();
183185
$idPatterns = Config::getKeyPatterns();
184-
$columns = array_filter($columns, function ($column) use ($primary, $idPatterns) {
185-
186-
return $column != $primary && !Helpers::strIs($idPatterns, $column);
187-
});
188-
189-
if (count($columns) > 0) {
190-
return current($columns);
186+
foreach ($columns as $column) {
187+
if ($column != $primary && !Helpers::strIs($idPatterns, $column)) {
188+
return $column;
189+
}
191190
}
192191

193192
return $primary;
@@ -293,11 +292,17 @@ public function getModelColumns()
293292
$model = $this->getForeignModelInstance();
294293
$columns = [];
295294
if ($this->isModel($model)) {
295+
// At this point we know a model class exists
296+
// Try to get the database column listing from the database directly
296297
$tableName = $model->getTable();
297298
$columns = DB::getSchemaBuilder()->getColumnListing($tableName);
298299
}
299300

300301
if (count($columns) == 0) {
302+
// At this poing we know the column have not yet been identified
303+
// which also mean that the model does not exists or the table
304+
// does not existing in the database.
305+
// Try to find the columns from the resource-file if one found.
301306
$columns = $this->getFieldNamesFromResource();
302307
}
303308

@@ -344,10 +349,10 @@ protected function getFieldNamesFromResource()
344349
protected function getForeignResource()
345350
{
346351
$modelName = $this->getForeignModelName();
352+
// Find the resource file from the resource-map or make a standard name.
347353
$resourceFile = ResourceMapper::pluckFirst($modelName) ?: Helpers::makeJsonFileName($modelName);
348-
$resourceFileFullName = Config::getResourceFilePath($resourceFile);
349354

350-
if (File::exists($resourceFileFullName)) {
355+
if (File::exists(Config::getResourceFilePath($resourceFile))) {
351356
return Resource::fromFile($resourceFile, 'crestapps');
352357
}
353358

src/Support/FieldTransformer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,6 @@ protected function getOptions(array $options)
355355

356356
// At this point we are just formatting the labels
357357
foreach ($options as $key => $option) {
358-
if (is_array($option)) {
359-
dd($option, $this->hasLanguages(), $this->languages);
360-
}
361-
362358
$labels[$key] = Helpers::convertNameToLabel($option);
363359
}
364360
}

src/Support/Helpers.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class Helpers
1818
*/
1919
public static function makeControllerName($modelName)
2020
{
21-
$case = ucfirst(camel_case(self::getProperCaseFor($modelName, 'controller-name')));
21+
$name = self::getProperCaseFor($modelName, 'controller-name');
22+
$case = ucfirst(camel_case($name));
2223

2324
if (!empty($postFix = Config::getControllerNamePostFix())) {
2425
return str_finish($case, $postFix);
@@ -81,7 +82,8 @@ public static function convertNameToLabel($name)
8182
*/
8283
public static function makeFormRequestName($modelName)
8384
{
84-
$case = ucfirst(camel_case(self::getProperCaseFor($modelName, 'request-form-name')));
85+
$name = self::getProperCaseFor($modelName, 'request-form-name');
86+
$case = ucfirst(camel_case($name));
8587

8688
if (!empty($postFix = Config::getFormRequestNamePostFix())) {
8789
return str_finish($case, $postFix);
@@ -150,7 +152,9 @@ public static function guessModelFullName($name, $modelsPath)
150152
*/
151153
public static function extractModelName($name)
152154
{
153-
return ucfirst(studly_case(Str::singular(str_replace('_id', '', $name))));
155+
$name = self::removePostFixWith($name, '_id');
156+
157+
return ucfirst(studly_case(Str::singular($name)));
154158
}
155159

156160
/**
@@ -266,9 +270,9 @@ public static function isNewerThanOrEqualTo($version = '5.3')
266270
*/
267271
public static function strReplaceOnce($pattern, $replacment, $subject)
268272
{
269-
if (strpos($subject, $pattern) !== false) {
270-
$occurrence = strpos($subject, $pattern);
271-
return substr_replace($subject, $replacment, strpos($subject, $pattern), strlen($pattern));
273+
$index = strpos($subject, $pattern);
274+
if ($index !== false) {
275+
return substr_replace($subject, $replacment, $index, strlen($pattern));
272276
}
273277

274278
return $subject;
@@ -392,14 +396,16 @@ public static function stringToBool($str)
392396
* Removes a string from the end of another giving string if it already ends with it.
393397
*
394398
* @param string $name
395-
* @param string $postFix
399+
* @param string $fix
396400
*
397401
* @return string
398402
*/
399-
public static function removePostFixWith($name, $postFix = '/')
403+
public static function removePostFixWith($name, $fix = '/')
400404
{
401-
if (ends_with($name, $postFix)) {
402-
return strstr($name, $postFix, true);
405+
$position = strripos($name, $fix);
406+
407+
if ($position !== false) {
408+
return substr($name, 0, $position);
403409
}
404410

405411
return $name;
@@ -409,14 +415,14 @@ public static function removePostFixWith($name, $postFix = '/')
409415
* Adds a postFix string at the end of another giving string if it does not already ends with it.
410416
*
411417
* @param string $name
412-
* @param string $postFix
418+
* @param string $fix
413419
*
414420
* @return string
415421
*/
416-
public static function postFixWith($name, $postFix = '/')
422+
public static function postFixWith($name, $fix = '/')
417423
{
418-
if (!ends_with($name, $postFix)) {
419-
return $name . $postFix;
424+
if (!ends_with($name, $fix)) {
425+
return $name . $fix;
420426
}
421427

422428
return $name;
@@ -426,14 +432,14 @@ public static function postFixWith($name, $postFix = '/')
426432
* Adds a preFix string at the begining of another giving string if it does not already ends with it.
427433
*
428434
* @param string $name
429-
* @param string $preFix
435+
* @param string $fix
430436
*
431437
* @return string
432438
*/
433-
public static function preFixWith($name, $preFix = '/')
439+
public static function preFixWith($name, $fix = '/')
434440
{
435-
if (!starts_with($name, $preFix)) {
436-
return $preFix . $name;
441+
if (!starts_with($name, $fix)) {
442+
return $fix . $name;
437443
}
438444

439445
return $name;

0 commit comments

Comments
 (0)