generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23e15a7
commit 85858b9
Showing
10 changed files
with
344 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Configuration; | ||
|
||
use Dedoc\Scramble\Support\OperationExtensions\RequestBodyExtension; | ||
use Dedoc\Scramble\Support\OperationExtensions\RequestEssentialsExtension; | ||
use Dedoc\Scramble\Support\OperationExtensions\ResponseExtension; | ||
use Illuminate\Support\Arr; | ||
|
||
class OperationTransformers | ||
{ | ||
protected array $transformers = []; | ||
|
||
protected array $appends = []; | ||
|
||
protected array $prepends = []; | ||
|
||
public function append(array|callable|string $transformers) | ||
{ | ||
$this->appends = array_merge( | ||
$this->appends, | ||
Arr::wrap($transformers) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function prepend(array|callable|string $transformers) | ||
{ | ||
$this->prepends = array_merge( | ||
$this->prepends, | ||
Arr::wrap($transformers) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function use(array $transformers) | ||
{ | ||
$this->transformers = $transformers; | ||
|
||
return $this; | ||
} | ||
|
||
public function all(): array | ||
{ | ||
$base = $this->transformers ?: [ | ||
RequestEssentialsExtension::class, | ||
RequestBodyExtension::class, | ||
ResponseExtension::class, | ||
]; | ||
|
||
return array_values(array_unique([ | ||
...$this->prepends, | ||
...$base, | ||
...$this->appends, | ||
])); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Contexts; | ||
|
||
use Dedoc\Scramble\GeneratorConfig; | ||
use Dedoc\Scramble\Reflections\ReflectionRoute; | ||
use Dedoc\Scramble\Support\Generator\OpenApi; | ||
use Illuminate\Routing\Route; | ||
|
||
class OperationTransformerContext | ||
{ | ||
public function __construct( | ||
public readonly Route $route, | ||
public readonly ReflectionRoute $reflectionRoute, | ||
public readonly OpenApi $document, | ||
public readonly GeneratorConfig $config, | ||
) | ||
{ | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Contracts; | ||
|
||
use Dedoc\Scramble\Contexts\OperationTransformerContext; | ||
use Dedoc\Scramble\Support\Generator\Operation; | ||
|
||
interface OperationTransformer | ||
{ | ||
public function handle(Operation $operation, OperationTransformerContext $context); | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\OperationTransformers; | ||
|
||
use Dedoc\Scramble\Contexts\OperationTransformerContext; | ||
use Dedoc\Scramble\Contracts\OperationTransformer; | ||
use Dedoc\Scramble\Infer; | ||
use Dedoc\Scramble\Support\Generator\Operation; | ||
use Dedoc\Scramble\Support\Generator\TypeTransformer; | ||
use Dedoc\Scramble\Support\RouteInfo; | ||
|
||
class ExtensionWrapperTransformer | ||
{ | ||
public function __construct(private string $extension) | ||
{ | ||
} | ||
|
||
public function __invoke(Operation $operation, OperationTransformerContext $context) | ||
{ | ||
$routeInfo = new RouteInfo($context->route, $context->reflectionRoute); | ||
|
||
$extensionInstance = new $this->extension( | ||
|
||
) | ||
} | ||
|
||
|
||
public function handle(Operation $operation, OperationTransformerContext $context) | ||
{ | ||
$routeInfo = new RouteInfo($context->route, $this->infer, $this->typeTransformer); | ||
} | ||
} |
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,122 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Reflections; | ||
|
||
use Dedoc\Scramble\Infer; | ||
use Dedoc\Scramble\Infer\Reflector\MethodReflector; | ||
use Dedoc\Scramble\Support\IndexBuilders\Bag; | ||
use Dedoc\Scramble\Support\IndexBuilders\RequestParametersBuilder; | ||
use Dedoc\Scramble\Support\RouteResponseTypeRetriever; | ||
use Dedoc\Scramble\Support\Type\FunctionType; | ||
use Illuminate\Routing\Route; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; | ||
use ReflectionClass; | ||
use ReflectionMethod; | ||
|
||
class ReflectionRoute | ||
{ | ||
public ?FunctionType $methodType = null; | ||
|
||
private ?PhpDocNode $phpDoc = null; | ||
|
||
private ?ClassMethod $methodNode = null; | ||
|
||
public readonly Bag $requestParametersFromCalls; | ||
|
||
public readonly Infer\Extensions\IndexBuildingBroker $indexBuildingBroker; | ||
|
||
public function __construct( | ||
public readonly Route $route, | ||
) { | ||
$this->requestParametersFromCalls = new Bag; | ||
$this->indexBuildingBroker = app(Infer\Extensions\IndexBuildingBroker::class); | ||
} | ||
|
||
public function isControllerAction(): bool | ||
{ | ||
return is_string($this->route->getAction('uses')); | ||
} | ||
|
||
public function getControllerClass(): ?string | ||
{ | ||
return $this->isControllerAction() | ||
? explode('@', $this->route->getAction('uses'))[0] | ||
: null; | ||
} | ||
|
||
public function getControllerMethod(): ?string | ||
{ | ||
return $this->isControllerAction() | ||
? explode('@', $this->route->getAction('uses'))[1] | ||
: null; | ||
} | ||
|
||
public function parsePhpDoc(): PhpDocNode | ||
{ | ||
if ($this->phpDoc) { | ||
return $this->phpDoc; | ||
} | ||
|
||
if (! $this->getControllerMethodAstNode()) { | ||
return new PhpDocNode([]); | ||
} | ||
|
||
$this->phpDoc = $this->getControllerMethodAstNode()->getAttribute('parsedPhpDoc') ?: new PhpDocNode([]); | ||
|
||
return $this->phpDoc; | ||
} | ||
|
||
public function getControllerMethodAstNode(): ?ClassMethod | ||
{ | ||
if ($this->methodNode || ! $this->isControllerAction() || ! $this->getReflectionMethod()) { | ||
return $this->methodNode; | ||
} | ||
|
||
return $this->methodNode = MethodReflector::make(...explode('@', $this->route->getAction('uses'))) | ||
->getAstNode(); | ||
} | ||
|
||
public function getReflectionMethod(): ?ReflectionMethod | ||
{ | ||
if (! $this->isControllerAction()) { | ||
return null; | ||
} | ||
|
||
if (! method_exists($this->getControllerClass(), $this->getControllerMethod())) { | ||
return null; | ||
} | ||
|
||
return (new ReflectionClass($this->getControllerClass())) | ||
->getMethod($this->getControllerMethod()); | ||
} | ||
|
||
public function getReturnType() | ||
{ | ||
return (new RouteResponseTypeRetriever($this))->getResponseType(); | ||
} | ||
|
||
/** | ||
* @todo Maybe better name is needed as this method performs method analysis, indexes building, etc. | ||
*/ | ||
public function getMethodType(): ?FunctionType | ||
{ | ||
if (! $this->isControllerAction() || ! $this->getReflectionMethod()) { | ||
return null; | ||
} | ||
|
||
if (! $this->methodType) { | ||
$def = $this->infer->analyzeClass($this->getReflectionMethod()->getDeclaringClass()->getName()); | ||
|
||
/* | ||
* Here the final resolution of the method types may happen. | ||
*/ | ||
$this->methodType = $def->getMethodDefinition($this->getControllerMethod(), indexBuilders: [ | ||
new RequestParametersBuilder($this->requestParametersFromCalls, $this->typeTransformer), | ||
...$this->indexBuildingBroker->indexBuilders, | ||
])->type; | ||
} | ||
|
||
return $this->methodType; | ||
} | ||
} |
Oops, something went wrong.