Skip to content

Commit

Permalink
Streamline creating related model factories
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Jan 1, 2020
1 parent 843eceb commit 4fdb0e6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
'username' => $faker->userName,
'email' => $faker->safeEmail,
'password' => bcrypt($faker->password),
'company_id' => function () {
return factory(App\Company::class)->create()->id;
},
'company_id' => factory(App\Company::class),
'remember_token' => Str::random(10),
];
});
Expand Down
21 changes: 6 additions & 15 deletions src/Console/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ protected function getPropertiesFromMethods($model)
$methods = get_class_methods($model);

foreach ($methods as $method) {
if (!method_exists('Illuminate\Database\Eloquent\Model', $method) && !Str::startsWith($method, 'get')) {
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
if (!Str::startsWith($method, 'get') && !method_exists('Illuminate\Database\Eloquent\Model', $method)) {
// Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
$reflection = new \ReflectionMethod($model, $method);
$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);
Expand All @@ -272,22 +272,13 @@ protected function getPropertiesFromMethods($model)
$code = trim(preg_replace('/\s\s+/', '', $code));
$begin = strpos($code, 'function(');
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
foreach (array(
'belongsTo',
) as $relation) {
foreach (['belongsTo'] as $relation) {
$search = '$this->' . $relation . '(';
if ($pos = stripos($code, $search)) {
//Resolve the relation's model to a Relation object.
$relationObj = $model->$method();
if ($relationObj instanceof Relation) {
$relatedModel = '\\' . get_class($relationObj->getRelated());
$relatedObj = new $relatedModel;
$property = method_exists($relationObj, 'getForeignKeyName')
? $relationObj->getForeignKeyName()
: $relationObj->getForeignKey();
$this->setProperty($property, 'function () {
return factory(' . get_class($relationObj->getRelated()) . '::class)->create()->' . $relatedObj->getKeyName() . ';
}');
echo 'found method: ', $method, PHP_EOL;
$this->setProperty($relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)');
}
}
}
Expand All @@ -301,7 +292,7 @@ protected function getPropertiesFromMethods($model)
*/
protected function setProperty($name, $type = null, $table = null)
{
if ($type !== null && Str::startsWith($type, 'function (')) {
if ($type !== null && Str::startsWith($type, 'factory(')) {
$this->properties[$name] = $type;

return;
Expand Down

0 comments on commit 4fdb0e6

Please sign in to comment.