From 60150aaf2bcb973f6fb2e56848fe436eea118093 Mon Sep 17 00:00:00 2001 From: Nhoem Chenda Date: Wed, 28 Apr 2021 16:01:59 +0700 Subject: [PATCH 1/5] Update --- composer.json | 3 ++- src/Coders/Model/Model.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1d536f22..516e15f7 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "require-dev": { "fzaninotto/faker": "~1.4", "mockery/mockery": ">=1.4", - "phpunit/phpunit": "^9" + "phpunit/phpunit": "^9", + "laravel/laravel": "^8.5" }, "autoload": { "psr-4": { diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index cd3b788b..7ba56632 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Reliese\Coders\Model\Relations\BelongsTo; use Illuminate\Database\Eloquent\Model as Eloquent; +use Illuminate\Support\Facades\Config; use Reliese\Coders\Model\Relations\ReferenceFactory; class Model @@ -244,6 +245,23 @@ protected function fill() } } + /** + * convert cast for postgress + */ + protected function castConvertForPostgress($cast){ + $convertedCast = $cast; + if(Config::get('database.default') === 'pgsql'){ + $castMap = [ + 'character varying' => 'string', + 'timestamp without time zone' => 'date' + ]; + if(array_key_exists($cast,$castMap)){ + $convertedCast = $castMap[$convertedCast]; + } + } + return $convertedCast; + } + /** * @param \Illuminate\Support\Fluent $column */ @@ -252,6 +270,8 @@ protected function parseColumn(Fluent $column) // TODO: Check type cast is OK $cast = $column->type; + $cast = $this->castConvertForPostgress($cast); + $propertyName = $this->usesPropertyConstants() ? 'self::'.strtoupper($column->name) : $column->name; // Due to some casting problems when converting null to a Carbon instance, From 2dc008df9ffcd563121f831a16f99367b55dae29 Mon Sep 17 00:00:00 2001 From: Nhoem Chenda Date: Mon, 10 May 2021 09:08:07 +0700 Subject: [PATCH 2/5] update add placeModelInOwnDirectory --- config/models.php | 9 +++++++++ src/Coders/Model/Factory.php | 12 ++++++++++++ src/Coders/Model/Model.php | 17 +++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/config/models.php b/config/models.php index d63c9dfa..191f2da7 100644 --- a/config/models.php +++ b/config/models.php @@ -22,6 +22,7 @@ | We need a location to store your new generated files. All files will be | placed within this directory. When you turn on base files, they will | be placed within a Base directory inside this location. + | There is one variable caleed {model_name} it will repa | */ @@ -421,6 +422,14 @@ | */ 'fillable_in_base_files' => false, + /* + |-------------------------------------------------------------------------- + | Place Model in its owner directory + |-------------------------------------------------------------------------- + | + | + */ + 'place_model_in_own_directory' => false, ], /* diff --git a/src/Coders/Model/Factory.php b/src/Coders/Model/Factory.php index f354af40..68ddd951 100644 --- a/src/Coders/Model/Factory.php +++ b/src/Coders/Model/Factory.php @@ -488,12 +488,24 @@ protected function body(Model $model) */ protected function modelPath(Model $model, $custom = []) { + + $modelsDirectory = $this->path(array_merge([$this->config($model->getBlueprint(), 'path')], $custom)); + if(!in_array('Base',$custom)){ + //add + if($model->placeModelInOwnDirectory()){ + $modelsDirectory.= '\\'.$model->getClassName(); + } + } + if (! $this->files->isDirectory($modelsDirectory)) { $this->files->makeDirectory($modelsDirectory, 0755, true); } + + + return $this->path([$modelsDirectory, $model->getClassName().'.php']); } diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index 7ba56632..42f0e68f 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -503,9 +503,14 @@ public function getRelationNameStrategy() */ public function getBaseNamespace() { + $baseNameSpace = $this->getNamespace().'\\Base'; + $modelNameSpace = $this->getNamespace(); + if($this->placeModelInOwnDirectory()){ + $modelNameSpace = $modelNameSpace.'\\'.$this->getClassName(); + } return $this->usesBaseFiles() - ? $this->getNamespace().'\\Base' - : $this->getNamespace(); + ? $baseNameSpace + : $modelNameSpace; } /** @@ -1269,4 +1274,12 @@ public function fillableInBaseFiles(): bool { return $this->config('fillable_in_base_files', false); } + /** + * @return bool + */ + public function placeModelInOwnDirectory(): bool + { + return $this->config('place_model_in_own_directory', false); + } + } From 40332c767d93e1754c1bfab84cd32bbb2f1cb2ec Mon Sep 17 00:00:00 2001 From: Nhoem Chenda Date: Mon, 17 May 2021 14:57:09 +0700 Subject: [PATCH 3/5] Update --- src/Coders/Model/Factory.php | 10 ---------- src/Coders/Model/Model.php | 10 ++++++++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Coders/Model/Factory.php b/src/Coders/Model/Factory.php index 68ddd951..4b0aaff9 100644 --- a/src/Coders/Model/Factory.php +++ b/src/Coders/Model/Factory.php @@ -492,20 +492,10 @@ protected function modelPath(Model $model, $custom = []) $modelsDirectory = $this->path(array_merge([$this->config($model->getBlueprint(), 'path')], $custom)); - if(!in_array('Base',$custom)){ - //add - if($model->placeModelInOwnDirectory()){ - $modelsDirectory.= '\\'.$model->getClassName(); - } - } - if (! $this->files->isDirectory($modelsDirectory)) { $this->files->makeDirectory($modelsDirectory, 0755, true); } - - - return $this->path([$modelsDirectory, $model->getClassName().'.php']); } diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index 42f0e68f..2eef0d12 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -235,7 +235,7 @@ protected function fill() $belongsTo = new BelongsTo($relation, $this, $model); $this->relations[$belongsTo->name()] = $belongsTo; } - + foreach ($this->factory->referencing($this) as $related) { $factory = new ReferenceFactory($related, $this); $references = $factory->make(); @@ -477,6 +477,9 @@ public function withReferences($references) */ public function withNamespace($namespace) { + if($this->placeModelInOwnDirectory()){ + $namespace = $namespace.'\\'.$this->getClassName(); + } $this->namespace = $namespace; return $this; @@ -503,11 +506,14 @@ public function getRelationNameStrategy() */ public function getBaseNamespace() { + $className = $this->getClassName(); $baseNameSpace = $this->getNamespace().'\\Base'; $modelNameSpace = $this->getNamespace(); + if($this->placeModelInOwnDirectory()){ - $modelNameSpace = $modelNameSpace.'\\'.$this->getClassName(); + $baseNameSpace = Str::replaceFirst($className,'Base',$modelNameSpace); } + return $this->usesBaseFiles() ? $baseNameSpace : $modelNameSpace; From e207d98dc51444229e4295ea1ab8626648c2f9eb Mon Sep 17 00:00:00 2001 From: Nhoem Chenda Date: Fri, 11 Jun 2021 11:34:22 +0700 Subject: [PATCH 4/5] update fix error generate modle --- src/Coders/Model/Factory.php | 7 +++++++ src/Coders/Model/Model.php | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Coders/Model/Factory.php b/src/Coders/Model/Factory.php index 4b0aaff9..150150cb 100644 --- a/src/Coders/Model/Factory.php +++ b/src/Coders/Model/Factory.php @@ -492,6 +492,13 @@ protected function modelPath(Model $model, $custom = []) $modelsDirectory = $this->path(array_merge([$this->config($model->getBlueprint(), 'path')], $custom)); + if(!Str::endsWith($modelsDirectory,'Base')){ + if($model->placeModelInOwnDirectory()){ + $modelsDirectory = $this->path([$modelsDirectory,$model->getClassName()]); + } + + } + if (! $this->files->isDirectory($modelsDirectory)) { $this->files->makeDirectory($modelsDirectory, 0755, true); } diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index 2eef0d12..53dfa029 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -511,7 +511,7 @@ public function getBaseNamespace() $modelNameSpace = $this->getNamespace(); if($this->placeModelInOwnDirectory()){ - $baseNameSpace = Str::replaceFirst($className,'Base',$modelNameSpace); + $modelNameSpace = Str::replaceFirst($className,'Base',$modelNameSpace); } return $this->usesBaseFiles() From 9914dc69bf85e43b8f5e514b9b56dd923a471ab4 Mon Sep 17 00:00:00 2001 From: Nhoem Chenda Date: Fri, 11 Jun 2021 15:17:42 +0700 Subject: [PATCH 5/5] update fix error --- src/Coders/Model/Model.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Coders/Model/Model.php b/src/Coders/Model/Model.php index 53dfa029..0177deee 100644 --- a/src/Coders/Model/Model.php +++ b/src/Coders/Model/Model.php @@ -512,6 +512,7 @@ public function getBaseNamespace() if($this->placeModelInOwnDirectory()){ $modelNameSpace = Str::replaceFirst($className,'Base',$modelNameSpace); + $baseNameSpace = Str::replaceFirst('\\'.$className.'\\Base','\\Base',$baseNameSpace); } return $this->usesBaseFiles()