Skip to content

Commit 78ab9c7

Browse files
committed
Fix a bug in the migration and another in the config file
1 parent 3ba82a0 commit 78ab9c7

7 files changed

+132
-50
lines changed

config/codegenerator.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
|--------------------------------------------------------------------------
99
|
1010
| Here you change the stub templates to use when generating code.
11-
| You can easily duplicate the "base_path('resources/codegenerator-templates/default')" folder, and call it "my-default"
12-
| now, you can change the stubs to have your own templates generated.
11+
| You can duplicate the "base_path('resources/codegenerator-templates/default')"
12+
| and call it what ever template your like "ex. skyblue".
13+
| Now, you can change the stubs to have your own templates generated.
14+
|
15+
|
16+
| IMPORTANT: It is not recomended to modify the default template, rather create a new template.
17+
| If you modify the default template and then executed "php artisan vendor:publish" command,
18+
| it will override the default template causing you to lose your modification.
1319
|
1420
*/
1521

@@ -34,6 +40,13 @@
3440

3541
'migrations_path' => base_path('database/migrations'),
3642

43+
/*
44+
|--------------------------------------------------------------------------
45+
| The default path of where the controllers will be generated from
46+
|--------------------------------------------------------------------------
47+
*/
48+
'form_requests_path' => app_path('Http/Requests'),
49+
3750
/*
3851
|--------------------------------------------------------------------------
3952
| The default path of where the controllers will be generated from
@@ -50,10 +63,11 @@
5063

5164
/*
5265
|--------------------------------------------------------------------------
53-
| The default path of where the controllers will be generated from
66+
| The default path of where the migrations will be generated from
5467
|--------------------------------------------------------------------------
5568
*/
56-
'form_requests_path' => app_path('Http/Requests'),
69+
70+
'languages_path' => base_path('resources/lang'),
5771

5872
/*
5973
|--------------------------------------------------------------------------

src/CodeGeneratorServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public function boot()
2727
$this->publishes([
2828
__DIR__ . '/templates' => base_path('resources\\codegenerator-templates\\default'),
2929
]);
30+
31+
$this->createDirectory(base_path('resources\\codegenerator-files'));
3032
}
3133

3234
/**
@@ -53,4 +55,20 @@ public function register()
5355
'CrestApps\CodeGenerator\Commands\CreateViewLayoutCommand'
5456
);
5557
}
58+
59+
/**
60+
* create a directory if one does not already exists
61+
*
62+
* @param string $path
63+
* @param string $mode
64+
*
65+
* @return void
66+
*/
67+
protected function createDirectory($path, $mode = '0777')
68+
{
69+
if(!file_exists($path))
70+
{
71+
mkdir($path, $mode);
72+
}
73+
}
5674
}

src/commands/CreateMigrationCommand.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ public function handle()
6666
$fields = $this->getFields($input->fields, 'migration', $input->fieldsFile);
6767

6868
$properites = $this->getTableProperties($fields, $input);
69-
69+
7070
$this->replaceSchemaUp($stub, $this->getSchemaUpCommand($input, $properites))
7171
->replaceSchemaDown($stub, $this->getSchemaDownCommand($input))
72+
->replaceClassName($stub, $input->className)
7273
->makeDirectory($this->getMigrationsPath())
73-
->makeFile($this->getMigrationsPath() . $input->className, $stub, $input->force);
74+
->makeFile($this->getMigrationsPath() . $this->makeFileName($input->tableName), $stub, $input->force);
7475
}
7576

7677
/**
@@ -87,7 +88,7 @@ protected function getTableProperties(array $fields, $input)
8788
->addFieldProperties($properties, $fields)
8889
->addIndexes($properties, $input->indexes)
8990
->addForeignConstraints($properties, $input->keys);
90-
91+
9192
return $properties;
9293
}
9394

@@ -336,9 +337,11 @@ protected function getCleanColumns($columnsString)
336337
*/
337338
protected function addFieldProperties(& $properties, array $fields)
338339
{
340+
$primaryField = $this->getPrimaryField($fields);
341+
339342
foreach($fields as $field)
340343
{
341-
if($field instanceof Field)
344+
if($field instanceof Field && $field != $primaryField)
342345
{
343346
$this->addFieldType($properties, $field)
344347
->addFieldComment($properties, $field)
@@ -455,9 +458,7 @@ protected function getPropertyBaseSpace($multiplier = 12, $prependNewline = fals
455458
protected function getSchemaDownCommand($input)
456459
{
457460

458-
$stub = <<<EOD
459-
Schema::{{connectionName}}drop('{{tableName}}');
460-
EOD;
461+
$stub = $this->getStubContent('schema-down');
461462

462463
$this->replaceConnectionName($stub, $input->connection)
463464
->replaceTableName($stub, $input->tableName);
@@ -475,19 +476,32 @@ protected function getSchemaDownCommand($input)
475476
protected function getSchemaUpCommand($input, $blueprintBody)
476477
{
477478

478-
$stub = <<<EOD
479-
Schema::{{connectionName}}create('{{tableName}}', function(Blueprint \$table)
480-
{
481-
{{blueprintBody}}
482-
});
483-
EOD;
479+
$stub = $this->getStubContent('schema-up');
480+
484481
$this->replaceConnectionName($stub, $input->connection)
485482
->replaceTableName($stub, $input->tableName)
486483
->replaceBlueprintBodyName($stub, $blueprintBody);
487484

488485
return $stub;
489486
}
490487

488+
489+
490+
/**
491+
* Replace the className of the given stub.
492+
*
493+
* @param string $stub
494+
* @param string $className
495+
*
496+
* @return $this
497+
*/
498+
protected function replaceClassName(&$stub, $className)
499+
{
500+
$stub = str_replace('DummyClass', $className, $stub);
501+
502+
return $this;
503+
}
504+
491505
/**
492506
* Replace the blueprintBody for the given stub.
493507
*
@@ -666,8 +680,8 @@ protected function addPrimaryField(& $property, Field $field)
666680
{
667681
if(!is_null($field))
668682
{
669-
$primaryMethod = trim($this->getPropertyBase($this->getPrimaryMethodName($field->dataType)));
670-
$property .= sprintf("%s('%s')", $primaryMethod, $field->name);
683+
$eloquentMethodName = $this->getPrimaryMethodName($field->dataType);
684+
$property .= sprintf("%s('%s')", $this->getPropertyBase($eloquentMethodName), $field->name);
671685
$this->addFieldPropertyClousure($property);
672686
}
673687

@@ -711,19 +725,35 @@ protected function getPrimaryMethodName($type)
711725
protected function getCommandInput()
712726
{
713727
$tableName = trim($this->argument('table-name'));
714-
$className = trim($this->option('migration-class-name')) ?: sprintf('%s_create_%s_table.php', date('Y_m_d_His'), strtolower($tableName));
728+
729+
$className = trim($this->option('migration-class-name')) ?: sprintf('Create%sTable', ucfirst($tableName));
715730
$connection = trim($this->option('connection-name'));
716731
$engine = trim($this->option('engine-name'));
717732
$fields = trim($this->option('fields'));
718733
$fieldsFile = trim($this->option('fields-file'));
719734
$indexes = $this->getIndexColelction(trim($this->option('indexes')));
720735
$force = $this->option('force');
721736
$keys = $this->getKeysCollections(trim($this->option('foreign-keys')));
722-
$className = Helpers::postFixWith($className, '.php');
737+
723738

724739
return (object) compact('tableName','className','connection','engine','fields','fieldsFile','force','indexes','keys');
725740
}
726741

742+
743+
/**
744+
* Makes a file name for the migration
745+
*
746+
* @param string $path
747+
* @return $this
748+
*/
749+
protected function makeFileName($tableName)
750+
{
751+
$filename = sprintf('%s_create_%s_table.php', date('Y_m_d_His'), strtolower($tableName));
752+
753+
return Helpers::postFixWith($filename, '.php');
754+
}
755+
756+
727757
/**
728758
* Build the directory for the class if necessary.
729759
*

src/commands/CreateModelCommand.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\GeneratorCommand;
66
use CrestApps\CodeGenerator\Traits\CommonCommand;
77
use CrestApps\CodeGenerator\Support\Helpers;
8+
use CrestApps\CodeGenerator\Support\Field;
89

910
class CreateModelCommand extends GeneratorCommand
1011
{
@@ -42,19 +43,6 @@ class CreateModelCommand extends GeneratorCommand
4243
*/
4344
protected $type = 'Model';
4445

45-
/**
46-
* Gets the default namespace for the class.
47-
*
48-
* @param string $rootNamespace
49-
* @return string
50-
*/
51-
/*
52-
protected function getDefaultNamespace($rootNamespace)
53-
{
54-
return $rootNamespace;
55-
}
56-
*/
57-
5846
/**
5947
* Builds the model class with the given name.
6048
*
@@ -69,7 +57,7 @@ protected function buildClass($name)
6957
$input = $this->getCommandInput();
7058
$fields = $this->getFields($input->fields, 'model', $input->fieldsFile);
7159

72-
$primaryKey = $this->getNewPrimaryKey($this->getPrimaryKeyName($input->primaryKey, $fields));
60+
$primaryKey = $this->getNewPrimaryKey($this->getPrimaryKeyName($fields, $input->primaryKey));
7361

7462
return $this->replaceNamespace($stub, $name)
7563
->replaceTable($stub, $input->table)
@@ -86,7 +74,7 @@ protected function buildClass($name)
8674
*
8775
* @return string
8876
*/
89-
protected function getPrimaryKeyName($primaryKey, array $fields)
77+
protected function getPrimaryKeyName(array $fields, $primaryKey)
9078
{
9179
$primaryField = $this->getPrimaryField($fields);
9280

@@ -141,9 +129,12 @@ protected function getFillablefields(array $fields)
141129
{
142130
$fillables = [];
143131

132+
$primaryField = $this->getPrimaryField($fields);
133+
144134
foreach($fields as $field)
145135
{
146-
if($field->isOnFormView)
136+
//exclude the primary key and anythins
137+
if($this->shouldFill($field, $primaryField))
147138
{
148139
$fillables[] = sprintf("'%s'", $field->name);
149140
}
@@ -152,6 +143,19 @@ protected function getFillablefields(array $fields)
152143
return sprintf('[%s]', implode(',', $fillables));
153144
}
154145

146+
/**
147+
* Checks if the field should be fillable or not
148+
*
149+
* @param CrestApps\CodeGenerator\Support\Field $field
150+
* @param CrestApps\CodeGenerator\Support\Field $primaryField
151+
*
152+
* @return bool
153+
*/
154+
protected function shouldFill(Field $field, Field $primaryField)
155+
{
156+
return $primaryField != $field && $field->isOnFormView;
157+
}
158+
155159
/**
156160
* Gets a clean user inputs.
157161
*

src/commands/CreateResourceCommand.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace CrestApps\CodeGenerator\Commands;
44

55
use Illuminate\Console\Command;
6-
76
use CrestApps\CodeGenerator\Support\Helpers;
7+
use CrestApps\CodeGenerator\Traits\CommonCommand;
88

99
class CreateResourceCommand extends Command
1010
{
11-
11+
use CommonCommand;
12+
1213
/**
1314
* The name and signature of the console command.
1415
*
@@ -58,13 +59,23 @@ public function handle()
5859
{
5960
$input = $this->getCommandInput();
6061

61-
$this->createModel($input)
62-
->createController($input)
63-
->createRoutes($input)
64-
->createViews($input)
65-
->createLanguage($input)
66-
->createMigration($input)
67-
->info('All Done!');
62+
$fields = $this->getFields($input->fields, $input->languageFileName, $input->fieldsFile);
63+
64+
if(empty($fields) || !isset($fields[0]))
65+
{
66+
$this->error('You must provide at least one field to generate the views!');
67+
} else
68+
{
69+
70+
$this->createModel($input)
71+
->createController($input)
72+
->createRoutes($input)
73+
->createViews($input)
74+
->createLanguage($input)
75+
->createMigration($input)
76+
->info('All Done!');
77+
}
78+
6879
}
6980

7081
/**
@@ -104,7 +115,7 @@ protected function createLanguage($input)
104115
{
105116
$this->callSilent('create:language',
106117
[
107-
'language-file-name' => $input->langFile,
118+
'language-file-name' => $input->languageFileName,
108119
'--fields' => $input->fields,
109120
'--fields-file' => $input->fieldsFile
110121
]);
@@ -172,7 +183,7 @@ protected function createController($input)
172183
'--fields' => $input->fields,
173184
'--fields-file' => $input->fieldsFile,
174185
'--routes-prefix' => $input->prefix,
175-
'--lang-file-name' => $input->langFile,
186+
'--lang-file-name' => $input->languageFileName,
176187
'--form-request' => $input->formRequest,
177188
'--force' => $input->force
178189
]);
@@ -226,7 +237,7 @@ protected function getCommandInput()
226237
$perPage = intval($this->option('models-per-page'));
227238
$fields = trim($this->option('fields'));
228239
$fieldsFile = trim($this->option('fields-file'));
229-
$langFile = trim($this->option('lang-file-name')) ?: $modelNamePlural;
240+
$languageFileName = trim($this->option('lang-file-name')) ?: $modelNamePlural;
230241
$formRequest = $this->option('form-request');
231242
$controllerDirectory = trim($this->option('controller-directory'));
232243
$withoutMigration = $this->option('without-migration');
@@ -250,7 +261,7 @@ protected function getCommandInput()
250261
$layoutName = trim($this->option('layout-name')) ?: 'layouts.app';
251262

252263
return (object) compact('modelName','controllerName','viewsDirectory','prefix','perPage','fileSnippet','fields','force',
253-
'langFile','fieldsFile','formRequest','modelDirectory','table','fillable','primaryKey',
264+
'languageFileName','fieldsFile','formRequest','modelDirectory','table','fillable','primaryKey',
254265
'relationships','useSoftDelete','useTimeStamps','controllerDirectory','withoutMigration',
255266
'migrationClass','connectionName','indexes','foreignKeys','engineName','layoutName');
256267
}

src/templates/schema-down.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Schema::{{connectionName}}drop('{{tableName}}');

src/templates/schema-up.stub

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Schema::{{connectionName}}create('{{tableName}}', function(Blueprint $table)
2+
{
3+
{{blueprintBody}}
4+
});

0 commit comments

Comments
 (0)