Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Apr 14, 2024
1 parent 89fc961 commit 57186b1
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 17 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
},
"scripts": {
"test": [
"phpcs",
"phpcs --colors",
"psalm --no-cache",
"phpstan analyse src --level=5 --memory-limit=-1",
"XDEBUG_MODE=coverage phpunit --coverage-text --colors=always"
Expand All @@ -83,5 +83,8 @@
"phpdoc -d src/ -t phpdoc/",
"XDEBUG_MODE=coverage phpunit --coverage-html coverage/"
]
},
"config": {
"sort-packages": true
}
}
2 changes: 1 addition & 1 deletion src/Annotation/Consumes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class Consumes
{
public function __construct(
public readonly MediaTypeInterface $value,
public readonly string|MediaTypeInterface $value,
) {
}
}
28 changes: 28 additions & 0 deletions src/Annotation/NamePrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Nekhay <[email protected]>
* @copyright Copyright (c) 2018, Anatoly Nekhay
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
* @link https://github.com/sunrise-php/http-router
*/

declare(strict_types=1);

namespace Sunrise\Http\Router\Annotation;

use Attribute;

/**
* @since 3.0.0
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
final class NamePrefix
{
public function __construct(
public readonly string $value,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @since 2.11.0
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
final class Prefix
final class PathPostfix
{
public function __construct(
public readonly string $value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @since 2.11.0
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
final class Postfix
final class PathPrefix
{
public function __construct(
public readonly string $value,
Expand Down
2 changes: 1 addition & 1 deletion src/Annotation/Produces.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class Produces
{
public function __construct(
public readonly MediaTypeInterface $value,
public readonly string|MediaTypeInterface $value,
) {
}
}
7 changes: 5 additions & 2 deletions src/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ final class Route
public mixed $holder = null;

/** @var array<array-key, string> */
public array $prefixes = [];
public array $namePrefixes = [];

/** @var array<array-key, string> */
public array $pathPrefixes = [];

/** @var non-empty-string|null */
public ?string $pattern = null;

public function __construct(
public readonly string $name,
public string $name,
public string $path,
/** @var array<string, string> */
public array $patterns = [],
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/MediaType/ServerMediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Sunrise\Http\Router\Entity\MediaType;

use function explode;

/**
* @since 3.0.0
*/
Expand All @@ -24,6 +26,16 @@ public function __construct(
) {
}

public static function fromString(string $string): self
{
$parts = explode(self::SEPARATOR, $string, 2);

$type = isset($parts[0][0]) ? $parts[0] : '*';
$subtype = isset($parts[1][0]) ? $parts[1] : '*';

return new self($type, $subtype);
}

public function getType(): string
{
return $this->type;
Expand Down
4 changes: 4 additions & 0 deletions src/Helper/HeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ final class HeaderParser
*/
public static function parseHeader(string $header): array
{
if ($header === '') {
return [];
}

$cursor = self::IN_IDENTIFIER;
$value = 0;
$param = -1;
Expand Down
37 changes: 27 additions & 10 deletions src/Loader/DescriptorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
use Sunrise\Http\Router\Annotation\Description;
use Sunrise\Http\Router\Annotation\Method;
use Sunrise\Http\Router\Annotation\Middleware;
use Sunrise\Http\Router\Annotation\NamePrefix;
use Sunrise\Http\Router\Annotation\PathPostfix;
use Sunrise\Http\Router\Annotation\PathPrefix;
use Sunrise\Http\Router\Annotation\Pattern;
use Sunrise\Http\Router\Annotation\Postfix;
use Sunrise\Http\Router\Annotation\Prefix;
use Sunrise\Http\Router\Annotation\Priority;
use Sunrise\Http\Router\Annotation\Produces;
use Sunrise\Http\Router\Annotation\Route as Descriptor;
use Sunrise\Http\Router\Annotation\Summary;
use Sunrise\Http\Router\Annotation\Tag;
use Sunrise\Http\Router\Entity\MediaType\ServerMediaType;
use Sunrise\Http\Router\Exception\InvalidRouteLoadingResourceException;
use Sunrise\Http\Router\Exception\InvalidRouteParsingSubjectException;
use Sunrise\Http\Router\Helper\ClassFinder;
Expand All @@ -44,6 +46,7 @@
use function class_exists;
use function is_dir;
use function is_file;
use function is_string;
use function join;
use function sprintf;
use function usort;
Expand Down Expand Up @@ -214,15 +217,22 @@ private function enrichDescriptorFromParentClasses(Descriptor $descriptor, Refle

private function enrichDescriptorFromClassOrMethod(Descriptor $descriptor, ReflectionClass|ReflectionMethod $classOrMethod): void
{
/** @var list<ReflectionAttribute<Prefix>> $annotations */
$annotations = $classOrMethod->getAttributes(Prefix::class);
/** @var list<ReflectionAttribute<NamePrefix>> $annotations */
$annotations = $classOrMethod->getAttributes(NamePrefix::class);
if (isset($annotations[0])) {
$annotation = $annotations[0]->newInstance();
$descriptor->prefixes[] = $annotation->value;
$descriptor->namePrefixes[] = $annotation->value;
}

/** @var list<ReflectionAttribute<Postfix>> $annotations */
$annotations = $classOrMethod->getAttributes(Postfix::class);
/** @var list<ReflectionAttribute<PathPrefix>> $annotations */
$annotations = $classOrMethod->getAttributes(PathPrefix::class);
if (isset($annotations[0])) {
$annotation = $annotations[0]->newInstance();
$descriptor->pathPrefixes[] = $annotation->value;
}

/** @var list<ReflectionAttribute<PathPostfix>> $annotations */
$annotations = $classOrMethod->getAttributes(PathPostfix::class);
if (isset($annotations[0])) {
$annotation = $annotations[0]->newInstance();
$descriptor->path .= $annotation->value;
Expand Down Expand Up @@ -267,14 +277,20 @@ private function enrichDescriptorFromClassOrMethod(Descriptor $descriptor, Refle
$annotations = $classOrMethod->getAttributes(Consumes::class);
foreach ($annotations as $annotation) {
$annotation = $annotation->newInstance();
$descriptor->consumes[] = $annotation->value;

$descriptor->consumes[] = is_string($annotation->value) ?
ServerMediaType::fromString($annotation->value) :
$annotation->value;
}

/** @var list<ReflectionAttribute<Produces>> $annotations */
$annotations = $classOrMethod->getAttributes(Produces::class);
foreach ($annotations as $annotation) {
$annotation = $annotation->newInstance();
$descriptor->produces[] = $annotation->value;

$descriptor->produces[] = is_string($annotation->value) ?
ServerMediaType::fromString($annotation->value) :
$annotation->value;
}

/** @var list<ReflectionAttribute<Tag>> $annotations */
Expand Down Expand Up @@ -317,7 +333,8 @@ private function enrichDescriptorFromClassOrMethod(Descriptor $descriptor, Refle
*/
private function completeDescriptor(Descriptor $descriptor): void
{
$descriptor->path = join($descriptor->prefixes) . $descriptor->path;
$descriptor->name = join($descriptor->namePrefixes) . $descriptor->name;
$descriptor->path = join($descriptor->pathPrefixes) . $descriptor->path;

$descriptor->pattern = RouteCompiler::compileRoute($descriptor->path, $descriptor->patterns);
}
Expand Down

0 comments on commit 57186b1

Please sign in to comment.