Skip to content

Commit

Permalink
Merge pull request #56 from sunrise-php/release/v2.4.0
Browse files Browse the repository at this point in the history
v2.4.0
  • Loading branch information
fenric committed Apr 20, 2020
2 parents f3ac5e9 + de81569 commit 2b5089a
Show file tree
Hide file tree
Showing 19 changed files with 568 additions and 30 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
## Installation

```bash
composer require 'sunrise/http-router:^2.2'
composer require 'sunrise/http-router:^2.4'
```

## QuickStart
Expand Down Expand Up @@ -70,6 +70,14 @@ $loader->attach('routes/public.php');
// [!] available from version 2.2
$loader->attach('routes');

// or attach an array...
// [!] available from version 2.4
$loader->attachArray([
'routes/api.php',
'routes/admin.php',
'routes/public.php',
]);

$router = new Router();
$router->load($loader);

Expand All @@ -92,6 +100,13 @@ AnnotationRegistry::registerLoader('class_exists');
$loader = new AnnotationDirectoryLoader();
$loader->attach('src/Controller');

// or attach an array
// [!] available from version 2.4
$loader->attachArray([
'src/Controller',
'src/Bundle/BundleName/Controller',
]);

$router = new Router();
$router->load($loader);

Expand Down Expand Up @@ -122,6 +137,29 @@ $response = $router->handle($request);
$response = $router->process($request, $handler);
```

#### Route Annotation Example

```php
/**
* @Route(
* name="apiEntryUpdate",
* path="/api/v1/entry/{id<@uuid>}(/{optionalAttribute})",
* methods={"PATCH"},
* middlewares={
* "App\Middleware\CorsMiddleware",
* "App\Middleware\ApiAuthMiddleware",
* },
* attributes={
* "optionalAttribute": "defaultValue",
* },
* summary="Updates an entry by UUID",
* description="Here you can describe the method in more detail...",
* tags={"api", "entry"},
* priority=0,
* )
*/
```

---

## Useful to know
Expand Down
86 changes: 86 additions & 0 deletions src/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ final class Route
*/
public $attributes;

/**
* @var string
*
* @since 2.4.0
*/
public $summary;

/**
* @var string
*
* @since 2.4.0
*/
public $description;

/**
* @var array
*
* @since 2.4.0
*/
public $tags;

/**
* @var int
*/
Expand All @@ -73,6 +94,9 @@ public function __construct(array $params)
$params += [
'middlewares' => [],
'attributes' => [],
'summary' => '',
'description' => '',
'tags' => [],
'priority' => 0,
];

Expand All @@ -81,13 +105,19 @@ public function __construct(array $params)
$this->assertParamsContainValidMethods($params);
$this->assertParamsContainValidMiddlewares($params);
$this->assertParamsContainValidAttributes($params);
$this->assertParamsContainValidSummary($params);
$this->assertParamsContainValidDescription($params);
$this->assertParamsContainValidTags($params);
$this->assertParamsContainValidPriority($params);

$this->name = $params['name'];
$this->path = $params['path'];
$this->methods = $params['methods'];
$this->middlewares = $params['middlewares'];
$this->attributes = $params['attributes'];
$this->summary = $params['summary'];
$this->description = $params['description'];
$this->tags = $params['tags'];
$this->priority = $params['priority'];
}

Expand Down Expand Up @@ -209,6 +239,62 @@ private function assertParamsContainValidAttributes(array $params) : void
}
}

/**
* @param array $params
*
* @return void
*
* @throws InvalidAnnotationParameterException
*/
private function assertParamsContainValidSummary(array $params) : void
{
if (!is_string($params['summary'])) {
throw new InvalidAnnotationParameterException(
'@Route.summary must be a string.'
);
}
}

/**
* @param array $params
*
* @return void
*
* @throws InvalidAnnotationParameterException
*/
private function assertParamsContainValidDescription(array $params) : void
{
if (!is_string($params['description'])) {
throw new InvalidAnnotationParameterException(
'@Route.description must be a string.'
);
}
}

/**
* @param array $params
*
* @return void
*
* @throws InvalidAnnotationParameterException
*/
private function assertParamsContainValidTags(array $params) : void
{
if (!is_array($params['tags'])) {
throw new InvalidAnnotationParameterException(
'@Route.tags must be an array.'
);
}

foreach ($params['tags'] as $middleware) {
if (!is_string($middleware)) {
throw new InvalidAnnotationParameterException(
'@Route.tags must contain only strings.'
);
}
}
}

/**
* @param array $params
*
Expand Down
15 changes: 15 additions & 0 deletions src/Exception/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Sunrise\Http\Router\Exception;

/**
* Import functions
*/
use function implode;

/**
* MethodNotAllowedException
*/
Expand All @@ -26,4 +31,14 @@ public function getAllowedMethods() : array
{
return $this->fromContext('allowed', []);
}

/**
* Gets joined allowed methods
*
* @return string
*/
public function getJoinedAllowedMethods() : string
{
return implode(',', $this->getAllowedMethods());
}
}
15 changes: 15 additions & 0 deletions src/Exception/UnsupportedMediaTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Sunrise\Http\Router\Exception;

/**
* Import functions
*/
use function implode;

/**
* UnsupportedMediaTypeException
*/
Expand All @@ -36,4 +41,14 @@ public function getSupportedTypes() : array
{
return $this->fromContext('supported', []);
}

/**
* Gets joined supported types
*
* @return string
*/
public function getJoinedSupportedTypes() : string
{
return implode(',', $this->getSupportedTypes());
}
}
15 changes: 14 additions & 1 deletion src/Loader/AnnotationDirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ public function attach($resource) : void
$this->resources[] = $resource;
}

/**
* {@inheritDoc}
*/
public function attachArray(array $resources) : void
{
foreach ($resources as $resource) {
$this->attach($resource);
}
}

/**
* {@inheritDoc}
*/
Expand All @@ -171,7 +181,10 @@ public function load() : RouteCollectionInterface
$this->initClass($class),
$this->initClasses(...$annotation->middlewares),
$annotation->attributes
);
)
->setSummary($annotation->summary)
->setDescription($annotation->description)
->setTags(...$annotation->tags);
}

return $this->collectionFactory->createCollection(...$routes);
Expand Down
10 changes: 10 additions & 0 deletions src/Loader/CollectableFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public function attach($resource) : void
$this->resources[] = $resource;
}

/**
* {@inheritDoc}
*/
public function attachArray(array $resources) : void
{
foreach ($resources as $resource) {
$this->attach($resource);
}
}

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ interface LoaderInterface
*/
public function attach($resource) : void;

/**
* Attaches the given array with resources to the loader
*
* @param array $resources
*
* @return void
*
* @throws InvalidLoaderResourceException
*/
public function attachArray(array $resources) : void;

/**
* Loads routes from previously attached resources
*
Expand Down

0 comments on commit 2b5089a

Please sign in to comment.