Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AnnotationAttributeDualReader that can read both annotations and attributes at the same time #165

Merged
merged 8 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-json": "*",
"contributte/psr7-http-message": "~0.7.0",
"doctrine/annotations": "^1.13",
"koriym/attributes": "^1.0",
"koriym/attributes": "^1.0.2",
"nette/utils": "^3.0.3"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
<exclude-pattern>/tests/tmp</exclude-pattern>

<!-- fails on PHP 7.4 because of attributes syntax collision (remove when on PHP 8) -->
<exclude-pattern>/tests/fixtures/Controllers/AttributeFoobarController.php</exclude-pattern>
<exclude-pattern>/tests/fixtures/Controllers/*.php</exclude-pattern>
</ruleset>
28 changes: 18 additions & 10 deletions src/Annotation/Controller/RequestParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ public function __construct($parameters)
$parameters = [$parameters];
}

$this->validateUniqueNames($parameters);

$this->parameters = $parameters;
}

/**
* @return RequestParameter[]
*/
public function getParameters(): array
{
return $this->parameters;
}

/**
* @param RequestParameter[] $parameters
*/
private function validateUniqueNames(array $parameters): void
{
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
$takenNames = [];

foreach ($parameters as $parameter) {
Expand All @@ -44,16 +62,6 @@ public function __construct($parameters)
));
}
}

$this->parameters = $parameters;
}

/**
* @return RequestParameter[]
*/
public function getParameters(): array
{
return $this->parameters;
}

}
4 changes: 2 additions & 2 deletions src/DI/Loader/DoctrineAnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ protected function parseControllerClassAnnotations(Controller $controller, Refle
}
}

protected function parseControllerMethodsAnnotations(Controller $controller, ReflectionClass $class): void
protected function parseControllerMethodsAnnotations(Controller $controller, ReflectionClass $reflectionClass): void
{
// Iterate over all methods in class
foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
// Read method annotations
$annotations = $this->getReader()->getMethodAnnotations($method);

Expand Down
1 change: 0 additions & 1 deletion src/Schema/Validation/RequestParameterValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ protected function validateMaskParametersAreInPath(SchemaBuilder $builder): void
{
foreach ($builder->getControllers() as $controller) {
foreach ($controller->getMethods() as $method) {

// Check if parameters in mask are in path
/** @var EndpointParameter[] $pathParameters */
$pathParameters = array_filter($method->getParameters(), function (EndpointParameter $parameter): bool {
Expand Down
41 changes: 41 additions & 0 deletions tests/cases/DI/Loader/MixedAttributeAnnotationLoader.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

/**
* Test: DI\Loader\DoctrineAnnotationLoader for combination of attribute and annotations
*
* @phpVersion 8.0
*/

require_once __DIR__ . '/../../../bootstrap.php';

use Apitte\Core\DI\Loader\DoctrineAnnotationLoader;
use Apitte\Core\Schema\SchemaBuilder;
use Apitte\Core\Schema\Validation\RequestParameterValidation;
use Nette\DI\ContainerBuilder;
use Tester\Assert;
use Tests\Fixtures\Controllers\Mixed\AnnotationAttributeController;
use Tests\Fixtures\Controllers\Mixed\AttributesOnlyController;
use Tests\Fixtures\Controllers\Mixed\PathAndRequestParamsController;

// Parse annotations
test(function (): void {
$builder = new ContainerBuilder();
$builder->addDefinition('first_controller')
->setType(AnnotationAttributeController::class);

$builder->addDefinition('second_controller')
->setType(PathAndRequestParamsController::class);

$builder->addDefinition('third_controller')
->setType(AttributesOnlyController::class);

$loader = new DoctrineAnnotationLoader($builder);
$schemaBuilder = $loader->load(new SchemaBuilder());
Assert::type(SchemaBuilder::class, $schemaBuilder);

$controllers = $schemaBuilder->getControllers();
Assert::count(3, $controllers);

$requestParameterValidation = new RequestParameterValidation();
$requestParameterValidation->validate($schemaBuilder);
});
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions tests/fixtures/Controllers/Mixed/AnnotationAttributeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types = 1);

namespace Tests\Fixtures\Controllers\Mixed;

use Apitte\Core\Annotation\Controller\Path;
use Apitte\Core\Annotation\Controller\RequestParameter;
use Apitte\Core\Annotation\Controller\RequestParameters;
use Tests\Fixtures\Controllers\ApiV1Controller;

final class AnnotationAttributeController extends ApiV1Controller
{

/**
* @RequestParameters({
* @RequestParameter(in="path", type="int", name="userId", description="User ID"),
* })
*/
#[Path(path: '/user/{userId}/collections')]
public function run()
{
}

}
21 changes: 21 additions & 0 deletions tests/fixtures/Controllers/Mixed/AttributesOnlyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers\Mixed;

use Apitte\Core\Annotation\Controller\Path;
use Apitte\Core\Annotation\Controller\RequestParameter;
use Tests\Fixtures\Controllers\ApiV1Controller;

final class AttributesOnlyController extends ApiV1Controller
{

#[RequestParameter(name: "userId", type: "int", in: "path")]
#[RequestParameter(name: "photoId", type: "int", in: "path")]
#[Path(path: '/user/{userId}/verification-photo/{photoId}')]
public function run()
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Controllers\Mixed;

use Apitte\Core\Annotation\Controller\Path;
use Apitte\Core\Annotation\Controller\RequestParameters;
use Apitte\Core\Annotation\Controller\RequestParameter;
use Tests\Fixtures\Controllers\ApiV1Controller;

final class PathAndRequestParamsController extends ApiV1Controller
{

/**
* @RequestParameters({
* @RequestParameter(name="userId", type="int", description="User ID", in="path"),
* @RequestParameter(name="photoId", type="int", description="Verification photo ID", in="path")
* })
*/
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
#[Path(path: '/user/{userId}/verification-photo/{photoId}')]
public function run()
{
}

}