-
-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for specifying example model sources
- Loading branch information
Showing
11 changed files
with
149 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Extracting; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c; | ||
use Knuckles\Scribe\Tools\ErrorHandlingUtils as e; | ||
use Knuckles\Scribe\Tools\Utils; | ||
use Throwable; | ||
|
||
trait InstantiatesExampleModels | ||
{ | ||
/** | ||
* @param string $type | ||
* | ||
* @param array $relations | ||
* @param array $factoryStates | ||
* | ||
* @return Model|object | ||
*/ | ||
protected function instantiateExampleModel(string $type, array $factoryStates = [], array $relations = []) | ||
{ | ||
$configuredStrategies = $this->config->get('examples.models_source', ['factoryCreate', 'factoryMake', 'database']); | ||
|
||
$strategies = [ | ||
'factoryCreate' => fn() => $this->getExampleModelFromFactoryCreate($type, $factoryStates, $relations), | ||
'factoryMake' => fn() => $this->getExampleModelFromFactoryMake($type, $factoryStates, $relations), | ||
'database' => fn() => $this->getExampleModelFromDatabase($type, $relations), | ||
]; | ||
|
||
foreach ($configuredStrategies as $strategyName) { | ||
try { | ||
$model = $strategies[$strategyName](); | ||
if ($model) return $model; | ||
} catch (Throwable $e) { | ||
c::warn("Couldn't get example model for {$type} via $strategyName."); | ||
e::dumpExceptionIfVerbose($e, true); | ||
} | ||
} | ||
|
||
return new $type; | ||
} | ||
|
||
protected function getExampleModelFromFactoryCreate(string $type, array $factoryStates = [], array $relations = []) | ||
{ | ||
$factory = Utils::getModelFactory($type, $factoryStates, $relations); | ||
return $factory->create()->load($relations); | ||
} | ||
|
||
protected function getExampleModelFromFactoryMake(string $type, array $factoryStates = [], array $relations = []) | ||
{ | ||
$factory = Utils::getModelFactory($type, $factoryStates, $relations); | ||
return $factory->make(); | ||
} | ||
|
||
protected function getExampleModelFromDatabase(string $type, array $relations = []) | ||
{ | ||
return $type::with($relations)->first(); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.