generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed return type inference from models when in JSON API resource con…
…text (#516) * removed old model analysis approach * moved stuff from old extension to simpler new one * resolving unknown classes before firing the events * fix enums being not correctly indexed and transformed * Fix styling * remove json infer extension * Fix styling --------- Co-authored-by: romalytvynenko <[email protected]>
- Loading branch information
1 parent
224be1b
commit 8eb2672
Showing
12 changed files
with
220 additions
and
344 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
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,70 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\Helpers; | ||
|
||
use Dedoc\Scramble\Infer\Definition\ClassDefinition; | ||
use Dedoc\Scramble\Infer\Scope\Scope; | ||
use Dedoc\Scramble\Infer\Services\FileNameResolver; | ||
use Dedoc\Scramble\Support\Type\ObjectType; | ||
use Dedoc\Scramble\Support\Type\Type; | ||
use Dedoc\Scramble\Support\Type\UnknownType; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
|
||
class JsonResourceHelper | ||
{ | ||
public static $jsonResourcesModelTypesCache = []; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function modelType(ClassDefinition $jsonClass, Scope $scope): ?Type | ||
{ | ||
if ($cachedModelType = static::$jsonResourcesModelTypesCache[$jsonClass->name] ?? null) { | ||
return $cachedModelType; | ||
} | ||
|
||
$modelClass = static::getModelName( | ||
$jsonClass->name, | ||
new \ReflectionClass($jsonClass->name), | ||
$scope->nameResolver, | ||
); | ||
|
||
$modelType = new UnknownType("Cannot resolve [$modelClass] model type."); | ||
if ($modelClass && is_a($modelClass, Model::class, true)) { | ||
$modelType = new ObjectType($modelClass); | ||
} | ||
|
||
static::$jsonResourcesModelTypesCache[$jsonClass->name] = $modelType; | ||
|
||
return $modelType; | ||
} | ||
|
||
private static function getModelName(string $jsonResourceClassName, \ReflectionClass $reflectionClass, FileNameResolver $getFqName) | ||
{ | ||
$phpDoc = $reflectionClass->getDocComment() ?: ''; | ||
|
||
$mixinOrPropertyLine = Str::of($phpDoc) | ||
->explode("\n") | ||
->first(fn ($str) => Str::is(['*@property*$resource', '*@mixin*'], $str)); | ||
|
||
if ($mixinOrPropertyLine) { | ||
$modelName = Str::replace(['@property', '$resource', '@mixin', ' ', '*'], '', $mixinOrPropertyLine); | ||
|
||
$modelClass = $getFqName($modelName); | ||
|
||
if (class_exists($modelClass)) { | ||
return $modelClass; | ||
} | ||
} | ||
|
||
$modelName = (string) Str::of(Str::of($jsonResourceClassName)->explode('\\')->last())->replace('Resource', '')->singular(); | ||
|
||
$modelClass = 'App\\Models\\'.$modelName; | ||
if (! class_exists($modelClass)) { | ||
return null; | ||
} | ||
|
||
return $modelClass; | ||
} | ||
} |
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.