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/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..150150cb 100644 --- a/src/Coders/Model/Factory.php +++ b/src/Coders/Model/Factory.php @@ -488,8 +488,17 @@ protected function body(Model $model) */ 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 cd3b788b..0177deee 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 @@ -234,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(); @@ -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, @@ -457,6 +477,9 @@ public function withReferences($references) */ public function withNamespace($namespace) { + if($this->placeModelInOwnDirectory()){ + $namespace = $namespace.'\\'.$this->getClassName(); + } $this->namespace = $namespace; return $this; @@ -483,9 +506,18 @@ public function getRelationNameStrategy() */ public function getBaseNamespace() { + $className = $this->getClassName(); + $baseNameSpace = $this->getNamespace().'\\Base'; + $modelNameSpace = $this->getNamespace(); + + if($this->placeModelInOwnDirectory()){ + $modelNameSpace = Str::replaceFirst($className,'Base',$modelNameSpace); + $baseNameSpace = Str::replaceFirst('\\'.$className.'\\Base','\\Base',$baseNameSpace); + } + return $this->usesBaseFiles() - ? $this->getNamespace().'\\Base' - : $this->getNamespace(); + ? $baseNameSpace + : $modelNameSpace; } /** @@ -1249,4 +1281,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); + } + }