Skip to content

Commit

Permalink
allow users to use inspectors to further analyse the annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Apr 10, 2024
1 parent c3d01fa commit 10f6267
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/System/OpenApiAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use OpenApi\Annotations\Operation;
use OpenApi\Attributes\Middleware;
use OpenApi\Context;
use Override;
use Zrnik\AttributeReflection\AttributeReflection;
use Zrnik\Zweist\ZweistConfiguration;
use Zrnik\Zweist\ZweistRouteService;

/**
Expand All @@ -20,16 +22,17 @@ class OpenApiAnalyser extends Analysis
/** @phpstan-var SlimRouteDataArrayShape[] */
private array $routes = [];

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

#[\Override]
#[Override]
public function addAnnotation(object $annotation, Context $context): void
{
if ($annotation instanceof Operation) {

/** @var class-string $controllerClass */
$controllerClass = sprintf(
'%s\%s',
Expand Down Expand Up @@ -64,6 +67,10 @@ public function addAnnotation(object $annotation, Context $context): void
'controller_method' => $method,
'middleware' => $middleware,
];

foreach ($this->zweistConfiguration->inspectors as $inspector) {
$inspector->inspect($controllerClass, $method, $annotation);
}
}

parent::addAnnotation($annotation, $context);
Expand Down
18 changes: 18 additions & 0 deletions src/System/OpenApiInspector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Zrnik\Zweist\System;

use OpenApi\Annotations\AbstractAnnotation;

interface OpenApiInspector
{
/**
* @param class-string $className
* @param string $methodName
* @param AbstractAnnotation $annotation
* @return void
*/
public function inspect(string $className, string $methodName, AbstractAnnotation $annotation): void;
}
3 changes: 3 additions & 0 deletions src/ZweistConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Zrnik\Zweist;

use Zrnik\Zweist\Exception\MisconfiguredOpenApiGeneratorException;
use Zrnik\Zweist\System\OpenApiInspector;

class ZweistConfiguration
{
Expand All @@ -15,11 +16,13 @@ class ZweistConfiguration

/**
* @param string[] $openApiDefinitionPaths
* @param OpenApiInspector[] $inspectors
*/
public function __construct(
array $openApiDefinitionPaths,
public readonly string $openApiJsonPath,
public readonly string $routerJsonPath,
public readonly array $inspectors = [],
)
{
if (count($openApiDefinitionPaths) === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/ZweistOpenApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
*/
public function generate(): void
{
$openApiAnalyser = new OpenApiAnalyser();
$openApiAnalyser = new OpenApiAnalyser($this->zweistConfiguration);

$openApi = Generator::scan(
$this->zweistConfiguration->openApiDefinitionPaths,
Expand Down
23 changes: 23 additions & 0 deletions tests/ExampleApplication/ExampleInspector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Zrnik\Zweist\Tests\ExampleApplication;

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

class ExampleInspector implements OpenApiInspector
{
/**
* @throws Exception
*/
public function inspect(string $className, string $methodName, AbstractAnnotation $annotation): void
{
/**
* @noinspection ThrowRawExceptionInspection
*/
throw new Exception(sprintf('%s called', self::class));
}
}
57 changes: 57 additions & 0 deletions tests/InspectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Zrnik\Zweist\Tests;

use DI\Container;
use Exception;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Zrnik\PHPUnit\Exceptions;
use Zrnik\Zweist\Tests\ExampleApplication\ExampleInspector;
use Zrnik\Zweist\ZweistConfiguration;
use Zrnik\Zweist\ZweistOpenApiGenerator;

class InspectorTest extends TestCase
{
use Exceptions;

private Container $container;

private ZweistConfiguration $zweistConfiguration;

/**
* @throws Exception
*/
protected function setUp(): void
{
$this->container = new Container();

$this->container->set(LoggerInterface::class, $this->createMock(LoggerInterface::class));

$this->zweistConfiguration = new ZweistConfiguration(
[
__DIR__ . '/ExampleApplication',
],
__DIR__ . '/../temp/OpenApi.json',
__DIR__ . '/../temp/router.json',
[
new ExampleInspector(),
],
);
}

public function testInspectorCalled(): void
{
$zweistOpenApiGenerator = new ZweistOpenApiGenerator($this->zweistConfiguration, $this->container);

/** @var Exception $exception */
$exception = $this->assertExceptionThrown(
Exception::class,
fn() => $zweistOpenApiGenerator->generate(),
);

$this->assertSame(sprintf('%s called', ExampleInspector::class), $exception->getMessage());
}
}

0 comments on commit 10f6267

Please sign in to comment.