Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature Place Model In Own Directory #246

Open
wants to merge 5 commits into
base: v1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 9 additions & 0 deletions config/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
*/

Expand Down Expand Up @@ -421,6 +422,14 @@
|
*/
'fillable_in_base_files' => false,
/*
|--------------------------------------------------------------------------
| Place Model in its owner directory
|--------------------------------------------------------------------------
|
|
*/
'place_model_in_own_directory' => false,
],

/*
Expand Down
9 changes: 9 additions & 0 deletions src/Coders/Model/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
46 changes: 43 additions & 3 deletions src/Coders/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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
*/
Expand All @@ -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,
Expand Down Expand Up @@ -457,6 +477,9 @@ public function withReferences($references)
*/
public function withNamespace($namespace)
{
if($this->placeModelInOwnDirectory()){
$namespace = $namespace.'\\'.$this->getClassName();
}
$this->namespace = $namespace;

return $this;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

}