Skip to content

Commit

Permalink
chore: OpenAPI schema paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Jan 10, 2024
1 parent 005ca8a commit 297fe5d
Show file tree
Hide file tree
Showing 45 changed files with 785 additions and 514 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use Attribute;
use Distantmagic\Resonance\Attribute as BaseAttribute;
use Distantmagic\Resonance\OpenAPICollectionSymbolInterface;
use Distantmagic\Resonance\OpenAPISchemaSymbolInterface;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
final readonly class BelongsToOpenAPICollection extends BaseAttribute
final readonly class BelongsToOpenAPISchema extends BaseAttribute
{
public function __construct(
public OpenAPICollectionSymbolInterface $collectionSymbol,
public OpenAPISchemaSymbolInterface $schemaSymbol,
) {}
}
3 changes: 0 additions & 3 deletions src/Attribute/RespondsToHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Attribute;
use Distantmagic\Resonance\Attribute as BaseAttribute;
use Distantmagic\Resonance\HttpOperationId\GeneratedFromClassName;
use Distantmagic\Resonance\HttpOperationIdInterface;
use Distantmagic\Resonance\HttpRouteSymbolInterface;
use Distantmagic\Resonance\RequestMethod;

Expand All @@ -23,7 +21,6 @@ public function __construct(
public bool $deprecated = false,
public ?string $description = null,
public ?HttpRouteSymbolInterface $routeSymbol = null,
public HttpOperationIdInterface $operationId = new GeneratedFromClassName(),
public int $priority = 0,
public array $requirements = [],
public ?string $summary = null,
Expand Down
11 changes: 4 additions & 7 deletions src/DependencyProviderIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Ds\Set;
use Generator;
use IteratorAggregate;
use ReflectionAttribute;

/**
* @template-implements IteratorAggregate<class-string,DependencyProvider>
Expand Down Expand Up @@ -44,18 +43,16 @@ public function getIterator(): Generator
*/
private function findRequiredCollections(PHPFileReflectionClassAttribute $reflectionAttribute): Set
{
$requiredCollectionsReflections = $reflectionAttribute
->reflectionClass
->getAttributes(RequiresSingletonCollection::class, ReflectionAttribute::IS_INSTANCEOF)
;
$reflectionAttributeManager = new ReflectionClassAttributeManager($reflectionAttribute->reflectionClass);
$requiredCollectionAttributes = $reflectionAttributeManager->findAttributes(RequiresSingletonCollection::class);

/**
* @var Set<SingletonCollectionInterface> $requiredCollections
*/
$requiredCollections = new Set();

foreach ($requiredCollectionsReflections as $requiredCollectionReflection) {
$requiredCollections->add($requiredCollectionReflection->newInstance()->collection);
foreach ($requiredCollectionAttributes as $requiredCollectionReflection) {
$requiredCollections->add($requiredCollectionReflection->collection);
}

return $requiredCollections;
Expand Down
1 change: 0 additions & 1 deletion src/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
enum Feature implements FeatureInterface
{
case OAuth2;
case OpenAPI;
case WebSocket;

public function getName(): string
Expand Down
22 changes: 6 additions & 16 deletions src/HttpInterceptor/JsonTemplateInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Distantmagic\Resonance\HttpInterceptor;

use Distantmagic\Resonance\ApplicationConfiguration;
use Distantmagic\Resonance\Attribute\Intercepts;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\ContentType;
use Distantmagic\Resonance\Environment;
use Distantmagic\Resonance\HttpInterceptor;
use Distantmagic\Resonance\HttpResponder\Json;
use Distantmagic\Resonance\HttpResponderInterface;
use Distantmagic\Resonance\JsonSerializer;
use Distantmagic\Resonance\JsonTemplate;
use Distantmagic\Resonance\SingletonCollection;
use Swoole\Http\Request;
Expand All @@ -22,23 +22,13 @@
#[Singleton(collection: SingletonCollection::HttpInterceptor)]
readonly class JsonTemplateInterceptor extends HttpInterceptor
{
public function __construct(
private ApplicationConfiguration $applicationConfiguration,
) {}
public function __construct(private JsonSerializer $jsonSerializer) {}

public function intercept(
Request $request,
Response $response,
object $intercepted,
): null {
$response->header('content-type', ContentType::ApplicationJson->value);
$response->end(json_encode(
value: $intercepted->data,
flags: Environment::Production === $this->applicationConfiguration->environment
? JSON_THROW_ON_ERROR
: JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES,
));

return null;
): HttpResponderInterface {
return new Json($this->jsonSerializer->serialize($intercepted->data));
}
}
7 changes: 0 additions & 7 deletions src/HttpOperationId.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/HttpOperationId/GeneratedFromClassName.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/HttpOperationId/StaticString.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/HttpOperationIdInterface.php

This file was deleted.

23 changes: 23 additions & 0 deletions src/HttpResponder/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance\HttpResponder;

use Distantmagic\Resonance\ContentType;
use Distantmagic\Resonance\HttpResponder;
use Swoole\Http\Request;
use Swoole\Http\Response;

readonly class Json extends HttpResponder
{
public function __construct(private string $json) {}

public function respond(Request $request, Response $response): null
{
$response->header('content-type', ContentType::ApplicationJson->value);
$response->end($this->json);

return null;
}
}
2 changes: 1 addition & 1 deletion src/HttpResponderAttributeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
readonly class HttpResponderAttributeCollection
{
/**
* @var Set<RespondsToHttp> $httpResponderAttributes
* @var Set<PHPFileReflectionClassAttribute<HttpResponderInterface,RespondsToHttp>> $httpResponderAttributes
*/
public Set $httpResponderAttributes;

Expand Down
13 changes: 12 additions & 1 deletion src/InternalLinkBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#[Singleton]
readonly class InternalLinkBuilder
{
public function __construct(private UrlGenerator $urlGenerator) {}
public function __construct(
private ApplicationConfiguration $applicationConfiguration,
private UrlGenerator $urlGenerator,
) {}

/**
* @param array<string,string> $params
Expand All @@ -19,4 +22,12 @@ public function build(HttpRouteSymbolInterface $routeSymbol, array $params = [])
{
return $this->urlGenerator->generate($routeSymbol->toConstant(), $params);
}

/**
* @param array<string,string> $params
*/
public function buildUrl(HttpRouteSymbolInterface $routeSymbol, array $params = []): string
{
return $this->applicationConfiguration->url.$this->build($routeSymbol, $params);
}
}
23 changes: 23 additions & 0 deletions src/JsonSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Distantmagic\Resonance;

use Distantmagic\Resonance\Attribute\Singleton;

#[Singleton]
readonly class JsonSerializer
{
public function __construct(private ApplicationConfiguration $applicationConfiguration) {}

public function serialize(mixed $data): string
{
return json_encode(
value: $data,
flags: Environment::Production === $this->applicationConfiguration->environment
? JSON_THROW_ON_ERROR
: JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES,
);
}
}
15 changes: 12 additions & 3 deletions src/OAuth2AuthorizationCodeFlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public function redirectToAuthenticatedPage(
Request $request,
Response $response,
): HttpInterceptableInterface {
$routeSymbol = $this->oAuth2EndpointResponderAggregate->endpointResponderRouteSymbol->get(OAuth2Endpoint::AuthenticatedPage);
$routeSymbol = $this
->oAuth2EndpointResponderAggregate
->getHttpRouteSymbolForEndpoint(OAuth2Endpoint::AuthenticatedPage)
;

return new InternalRedirect($routeSymbol);
}
Expand All @@ -105,7 +108,10 @@ public function redirectToClientScopeConsentPage(
Response $response,
AuthorizationRequest $authorizationRequest,
): HttpInterceptableInterface {
$routeSymbol = $this->oAuth2EndpointResponderAggregate->endpointResponderRouteSymbol->get(OAuth2Endpoint::ClientScopeConsentForm);
$routeSymbol = $this
->oAuth2EndpointResponderAggregate
->getHttpRouteSymbolForEndpoint(OAuth2Endpoint::ClientScopeConsentForm)
;

return new InternalRedirect($routeSymbol);
}
Expand All @@ -115,7 +121,10 @@ public function redirectToLoginPage(
Response $response,
AuthorizationRequest $authorizationRequest,
): HttpInterceptableInterface {
$routeSymbol = $this->oAuth2EndpointResponderAggregate->endpointResponderRouteSymbol->get(OAuth2Endpoint::LoginForm);
$routeSymbol = $this
->oAuth2EndpointResponderAggregate
->getHttpRouteSymbolForEndpoint(OAuth2Endpoint::LoginForm)
;

return new InternalRedirect($routeSymbol);
}
Expand Down
34 changes: 33 additions & 1 deletion src/OAuth2EndpointResponderAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,48 @@
namespace Distantmagic\Resonance;

use Ds\Map;
use Ds\Set;
use LogicException;

readonly class OAuth2EndpointResponderAggregate
{
/**
* @var Map<OAuth2Endpoint,HttpRouteSymbolInterface>
*/
public Map $endpointResponderRouteSymbol;
private Map $endpointResponderRouteSymbol;

public function __construct()
{
$this->endpointResponderRouteSymbol = new Map();
}

public function getHttpRouteSymbolForEndpoint(OAuth2Endpoint $oAuth2Endpoint): HttpRouteSymbolInterface
{
$httpRouteSymbol = $this
->endpointResponderRouteSymbol
->get($oAuth2Endpoint, null)
;

if (!$httpRouteSymbol) {
throw new LogicException(sprintf(
'There is no oauth2 endpoint responder registered for: "%s"',
$oAuth2Endpoint->name,
));
}

return $httpRouteSymbol;
}

/**
* @return Set<OAuth2Endpoint>
*/
public function getRegisteredEndpoints(): Set
{
return $this->endpointResponderRouteSymbol->keys();
}

public function registerEndpoint(OAuth2Endpoint $oAuth2Endpoint, HttpRouteSymbolInterface $httpRouteSymbol): void
{
$this->endpointResponderRouteSymbol->put($oAuth2Endpoint, $httpRouteSymbol);
}
}
4 changes: 2 additions & 2 deletions src/OAuth2GrantCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
/**
* @var Set<OAuth2Grant>
*/
public Set $oauth2Grants;
public Set $oAuth2Grants;

public function __construct()
{
$this->oauth2Grants = new Set();
$this->oAuth2Grants = new Set();
}
}
Loading

0 comments on commit 297fe5d

Please sign in to comment.