Skip to content

Commit

Permalink
fixed context usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Apr 10, 2024
1 parent ad0cd6a commit c9bb9de
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
25 changes: 13 additions & 12 deletions src/System/OpenApiAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ class OpenApiAnalyser extends Analysis
private array $routes = [];

public function __construct(
private readonly ZweistConfiguration $zweistConfiguration
private readonly ZweistConfiguration $zweistConfiguration,
Context $context,
)
{
parent::__construct([], new Context());
parent::__construct([], $context);
}

public function addAnnotation(object $annotation, Context $context): void
{
/** @var class-string $class */
$class = sprintf(
'%s\%s',
$context->namespace,
$context->class
);

$method = (string) $context->method;

if ($annotation instanceof Operation) {
$middleware = [];

/** @var class-string $class */
$class = sprintf(
'%s\%s',
$context->namespace,
$context->class
);

$method = (string) $context->method;

/** @var Middleware $middlewareAttribute */
foreach (
AttributeReflection::getMethodAttributes(Middleware::class, $class, $method)
Expand Down Expand Up @@ -70,7 +71,7 @@ public function addAnnotation(object $annotation, Context $context): void

foreach ($this->zweistConfiguration->inspectors as $inspector) {
if ($annotation instanceof AbstractAnnotation) {
$inspector->inspect($class, $method, $annotation);
$inspector->inspect($annotation, $context);
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/System/OpenApiInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
namespace Zrnik\Zweist\System;

use OpenApi\Annotations\AbstractAnnotation;
use OpenApi\Context;

interface OpenApiInspector
{
/**
* @param class-string $className
* @param string $methodName
* @param AbstractAnnotation $annotation
* @return void
*/
public function inspect(string $className, string $methodName, AbstractAnnotation $annotation): void;
public function inspect(AbstractAnnotation $annotation, Context $context): void;
}
17 changes: 16 additions & 1 deletion src/ZweistOpenApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use JsonException;
use OpenApi\Annotations\OpenApi;
use OpenApi\Context;
use OpenApi\Generator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
Expand All @@ -16,11 +17,18 @@

class ZweistOpenApiGenerator
{
private Context $context;

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __construct(
private readonly ZweistConfiguration $zweistConfiguration,
private readonly ContainerInterface $container,
)
{
$this->context = $this->container->get(Context::class);
}

/**
Expand All @@ -31,11 +39,18 @@ public function __construct(
*/
public function generate(): void
{
$openApiAnalyser = new OpenApiAnalyser($this->zweistConfiguration);
$this->context->version = OpenApi::VERSION_3_1_0;

$openApiAnalyser = new OpenApiAnalyser(
$this->zweistConfiguration,
$this->context
);

Generator::$context = $this->context;
$openApi = Generator::scan(
$this->zweistConfiguration->openApiDefinitionPaths,
[
'version' => OpenApi::VERSION_3_1_0,
'analysis' => $openApiAnalyser,
'logger' => $this->container->get(LoggerInterface::class),
],
Expand Down
3 changes: 2 additions & 1 deletion tests/ExampleApplication/ExampleInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

use Exception;
use OpenApi\Annotations\AbstractAnnotation;
use OpenApi\Context;
use Zrnik\Zweist\System\OpenApiInspector;

class ExampleInspector implements OpenApiInspector
{
/**
* @throws Exception
*/
public function inspect(string $className, string $methodName, AbstractAnnotation $annotation): void
public function inspect(AbstractAnnotation $annotation, Context $context): void
{
/**
* @noinspection ThrowRawExceptionInspection
Expand Down
4 changes: 4 additions & 0 deletions tests/ZweistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ public function testFileNotFoundException(): void
$zweistRouteService->applyRoutes(new App(new Psr17Factory()));
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function testInvalidControllerThrowsException(): void
{
$zweistConfiguration = new ZweistConfiguration(
Expand Down

0 comments on commit c9bb9de

Please sign in to comment.