-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
603d86a
commit 143a50f
Showing
10 changed files
with
238 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Apiato\Foundation\Configuration; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory as AbstractFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @template TModel of Model | ||
* @template TFactory of AbstractFactory | ||
*/ | ||
final class Factory | ||
{ | ||
protected static \Closure $nameResolver; | ||
|
||
public function __construct() | ||
{ | ||
$this->resolveFactoryNameUsing( | ||
static function (string $modelName): string|null { | ||
$factoryName = Str::of($modelName)->beforeLast('Models\\') | ||
->append('Data\\Factories\\' . class_basename($modelName) . 'Factory') | ||
->value(); | ||
|
||
if (class_exists($factoryName)) { | ||
return $factoryName; | ||
} | ||
|
||
return null; | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* @param \Closure(class-string<TModel>): (class-string<TFactory>|null) $callback | ||
*/ | ||
public function resolveFactoryNameUsing(\Closure $callback): self | ||
{ | ||
self::$nameResolver = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param class-string<TModel> $modelName | ||
* | ||
* @return class-string<TFactory>|null | ||
*/ | ||
public function resolveFactoryName(string $modelName): string|null | ||
{ | ||
return app()->call(self::$nameResolver, ['modelName' => $modelName]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Apiato\Foundation\Configuration; | ||
|
||
use Apiato\Abstract\Repositories\Repository as AbstractRepository; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @template TModel of Model | ||
* @template TRepository of AbstractRepository | ||
*/ | ||
final class Repository | ||
{ | ||
protected static \Closure $nameResolver; | ||
|
||
public function __construct() | ||
{ | ||
$this->resolveModelNameUsing( | ||
static function (string $repositoryName): string { | ||
$modelName = Str::of($repositoryName)->beforeLast('Data\\Repositories\\') | ||
->append('Models\\') | ||
->append( | ||
Str::of(class_basename($repositoryName)) | ||
->beforeLast('Repository') | ||
->title()->value(), | ||
)->value(); | ||
|
||
if (class_exists($modelName)) { | ||
return $modelName; | ||
} | ||
|
||
throw new \RuntimeException("Model not found for repository: {$repositoryName}"); | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* @param \Closure(class-string<TRepository>): (class-string<TModel>) $callback | ||
*/ | ||
public function resolveModelNameUsing(\Closure $callback): self | ||
{ | ||
self::$nameResolver = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param class-string<TRepository> $repositoryName | ||
* | ||
* @return class-string<TModel> | ||
*/ | ||
public function resolveModelName(string $repositoryName): string | ||
{ | ||
return app()->call(self::$nameResolver, ['repositoryName' => $repositoryName]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.