Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #183 from Qoraiche/fix/issue-168
Browse files Browse the repository at this point in the history
Fix/issue 168
  • Loading branch information
ReeceM authored Sep 28, 2021
2 parents d28f054 + 5d751c9 commit 306769b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
39 changes: 39 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 7 additions & 3 deletions config/maileclipse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],

/*
|--------------------------------------------------------------------------
Expand Down
44 changes: 24 additions & 20 deletions src/MailEclipse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 306769b

Please sign in to comment.