diff --git a/changelog.md b/changelog.md index 16c98c1..b436749 100644 --- a/changelog.md +++ b/changelog.md @@ -221,3 +221,42 @@ This release will resolve the issue that has been brought up in issue #63 where ## Version 3.4.1 - Add development env to the allowed environments in the config by default as many using it instead of the local +## Version 3.4.2 + +### Fixes + +- Fixes issue where package would try to search for `string` type of a parameter and fail. [#178](https://github.com/Qoraiche/laravel-mail-editor/issues/178) + +## Version 3.5.0 + +### Changes +- The structure of the config file for the relations section. It will fallback to loading one by default. + +New Structure: + +```diff + ++ /* ++ |-------------------------------------------------------------------------- ++ | Relationship loading. ++ |-------------------------------------------------------------------------- ++ | ++ | This configures how deep the package will search an load relations. ++ | If you set this to 0, relations will not be loaded. ++ | ++ | relation_depth: off = 0, min = 1, max = 5 ++ | model: this is the model to use to exclude methods when searching. ++ | ++ | N.B. This does not configure how many many relationship types are loaded. ++ */ +- 'relation_depth' => env('MAILECLIPSE_RELATION_DEPTH', 2), ++ 'relations' => [ ++ 'relation_depth' => env('MAILECLIPSE_RELATION_DEPTH', 2), ++ ++ 'model' => \Illuminate\Foundation\Auth\User::class, ++ ], + +``` +### Fixes + +- Fixes [#168](https://github.com/Qoraiche/laravel-mail-editor/issues/168) diff --git a/config/maileclipse.php b/config/maileclipse.php index 524c0a4..74d8cc9 100644 --- a/config/maileclipse.php +++ b/config/maileclipse.php @@ -34,18 +34,22 @@ /* |-------------------------------------------------------------------------- - | Relationship loading depth + | Relationship loading. |-------------------------------------------------------------------------- | | This configures how deep the package will search an load relations. | If you set this to 0, relations will not be loaded. | - | off = 0, min = 1, max = 5 + | relation_depth: off = 0, min = 1, max = 5 + | model: this is the model to use to exclude methods when searching. | | N.B. This does not configure how many many relationship types are loaded. */ + 'relations' => [ + 'relation_depth' => env('MAILECLIPSE_RELATION_DEPTH', 2), - 'relation_depth' => env('MAILECLIPSE_RELATION_DEPTH', 2), + 'model' => \Illuminate\Foundation\Auth\User::class, + ], /* |-------------------------------------------------------------------------- diff --git a/src/MailEclipse.php b/src/MailEclipse.php index 9123bda..0244bc4 100644 --- a/src/MailEclipse.php +++ b/src/MailEclipse.php @@ -4,7 +4,6 @@ use ErrorException; use Illuminate\Database\Eloquent\Factory as EloquentFactory; -use Illuminate\Database\Eloquent\Model; use Illuminate\Http\JsonResponse; use Illuminate\Mail\Markdown; use Illuminate\Support\Collection; @@ -30,7 +29,7 @@ class MailEclipse { public const VIEW_NAMESPACE = 'maileclipse'; - public const VERSION = '3.4.0'; + public const VERSION = '3.5.0'; /** * Default type examples for being passed to reflected classes. @@ -942,37 +941,42 @@ private static function resolveFactory($eloquentFactory, $model): ?object */ private static function hydrateRelations($eloquentFactory, $factoryModel): ?object { - if (config('maileclipse.relation_depth') === 0) { + if (config('maileclipse.relations.relation_depth', 1) === 0) { return $factoryModel; } if (self::$traversed >= 5) { - Log::warning('[MailEclipse]: more than 5 calls to relation loader', ['last_model' => get_class($factoryModel) ?? null]); + Log::warning('[MailEclipse]: more than 5 calls to relation loader', ['last_model' => get_class($factoryModel) ?? 'model unknown']); self::$traversed = 6; return $factoryModel; } - $model = new ReflectionClass(Model::class); + $model = new ReflectionClass(config('maileclipse.relations.model', \Illuminate\Foundation\Auth\User::class)); self::$traversed += 1; collect((new ReflectionClass($factoryModel))->getMethods()) - ->pluck('name') - ->diff(collect($model->getMethods())->pluck('name')) - ->filter(function ($method) use ($factoryModel) { - return rescue( - function () use ($factoryModel, $method) { - $parents = class_parents($factoryModel->$method()); - - return isset($parents["Illuminate\Database\Eloquent\Relations\Relation"]); - }, - false, - false - ); + ->filter(function (\ReflectionMethod $method) use ($model) { + return ! $model->hasMethod($method->getName()); + }) + ->filter(function (\ReflectionMethod $method) use ($factoryModel) { + if ($method->getNumberOfParameters() >= 1) { + return false; + } + + $parents = rescue(function () use ($method, $factoryModel) { + $methodName = $method->getName(); + + return $method->hasReturnType() + ? class_parents($method->getReturnType()->getName()) + : class_parents($factoryModel->$methodName()); + }, [], false); + + return isset($parents["Illuminate\Database\Eloquent\Relations\Relation"]); }) - ->each(function ($relationName) use (&$factoryModel, $eloquentFactory) { - $factoryModel = self::loadRelations($relationName, $factoryModel, $eloquentFactory); + ->each(function (\ReflectionMethod $relationName) use (&$factoryModel, $eloquentFactory) { + $factoryModel = self::loadRelations($relationName->getName(), $factoryModel, $eloquentFactory); }); return $factoryModel; @@ -999,7 +1003,7 @@ public static function loadRelations($relationName, $factoryModel, $eloquentFact $related = $factoryModel->$relationName()->getRelated(); $relatedFactory = self::resolveFactory($eloquentFactory, get_class($related)); - if (self::$traversed <= config('maileclipse.relation_depth')) { + if (self::$traversed <= config('maileclipse.relations.relation_depth')) { if (! $loadIfIterable) { $relatedFactory = self::hydrateRelations($eloquentFactory, $relatedFactory); } else {