Skip to content

Commit

Permalink
Merge pull request #35 from sunrise-php/release/v2.1.0
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
fenric authored Oct 12, 2021
2 parents 5761522 + 609c932 commit bb63358
Show file tree
Hide file tree
Showing 19 changed files with 874 additions and 237 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@

---

## Important to understanding

* [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification/blob/6ba1577240b79c9f613c2ea8d745c6ef6c832e50/versions/3.0.2.md)

## Installation

```bash
composer require 'sunrise/http-router-openapi:^2.0'
composer require 'sunrise/http-router-openapi:^2.1'
```

## QuickStart
Expand All @@ -21,7 +25,7 @@ composer require 'sunrise/http-router-openapi:^2.0'
use Psr\SimpleCache\CacheInterface;
use Sunrise\Http\Router\OpenApi\Object\Info;
use Sunrise\Http\Router\OpenApi\OpenApi;
use Sunrise\Http\Router\Router;
use Sunrise\Http\Router\OpenApi\RouteInterface;

$openapi = new OpenApi(new Info('Acme', '1.0.0'));

Expand All @@ -30,7 +34,11 @@ $openapi = new OpenApi(new Info('Acme', '1.0.0'));
$openapi->setCache($cache);

// Passing all routes to the openapi object:
/** @var Router $router */
/** @var RouteInterface[] $routes */
$openapi->addRoute(...$routes);

// When using Sunrise Router:
/** @var \Sunrise\Http\Router\Router $router */
$openapi->addRoute(...$router->getRoutes());
```

Expand All @@ -41,7 +49,7 @@ $openapi->addRoute(...$router->getRoutes());
$openapi->toJson();
// Converting the openapi object to YAML document:
$openapi->toYaml();
// Converting the openapi object to an array
// Converting the openapi object to an array:
$openapi->toArray();
```

Expand All @@ -66,7 +74,6 @@ $openapi->getResponseBodyJsonSchema();
```php
use Sunrise\Http\Router\OpenApi\Middleware\RequestValidationMiddleware;
use Sunrise\Http\Router\OpenApi\OpenApi;
use Sunrise\Http\Router\Route;

/** @var OpenApi $openapi */
$middleware = new RequestValidationMiddleware($openapi);
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
],
"require": {
"php": "^7.1|^8.0",
"sunrise/http-router": "^2.10",
"doctrine/annotations": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "7.5.20|9.5.0",
"sunrise/coding-standard": "1.0.0",
"sunrise/http-factory": "1.1.0",
"sunrise/http-router": "^2.11",
"symfony/console": "^4.4",
"justinrainbow/json-schema": "5.2.10"
},
Expand All @@ -47,6 +47,10 @@
"test": [
"phpcs",
"XDEBUG_MODE=coverage phpunit --coverage-text"
],
"build": [
"phpdoc -d src/ -t phpdoc/",
"XDEBUG_MODE=coverage phpunit --coverage-html coverage/"
]
}
}
32 changes: 32 additions & 0 deletions src/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,36 @@ public function collectReferencedObjects(AnnotationReader $annotationReader) : a

return $this->referencedObjects;
}

/**
* Serializes the object
*
* @return array
*/
public function __serialize() : array
{
// reflector can't be serialized...
$this->holder = null;

$data = [];
foreach ($this as $key => $value) {
$data[$key] = $value;
}

return $data;
}

/**
* Unserializes the object
*
* @param array $data
*
* @return void
*/
public function __unserialize(array $data) : void
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
}
6 changes: 3 additions & 3 deletions src/Annotation/OpenApi/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameter-object
*
* @final Don't use the object outside of the package.
* @final
*/
class Parameter extends AbstractAnnotation implements ParameterInterface, ComponentInterface
{
Expand Down Expand Up @@ -147,15 +147,15 @@ class Parameter extends AbstractAnnotation implements ParameterInterface, Compon
/**
* {@inheritdoc}
*/
public function getComponentName() : string
final public function getComponentName() : string
{
return 'parameters';
}

/**
* {@inheritdoc}
*/
public function getReferenceName() : string
final public function getReferenceName() : string
{
return $this->refName ?? spl_object_hash($this);
}
Expand Down
127 changes: 127 additions & 0 deletions src/Bridge/Sunrise/SunriseRouteProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php declare(strict_types=1);

/**
* It's free open-source software released under the MIT License.
*
* @author Anatoly Fenric <anatoly@fenric.ru>
* @copyright Copyright (c) 2019, Anatoly Fenric
* @license https://github.com/sunrise-php/http-router-openapi/blob/master/LICENSE
* @link https://github.com/sunrise-php/http-router-openapi
*/

namespace Sunrise\Http\Router\OpenApi\Bridge\Sunrise;

/**
* Import classes
*/
use Sunrise\Http\Router\OpenApi\RouteInterface as OpenapiRouteInterface;
use Sunrise\Http\Router\RequestHandler\CallableRequestHandler as SunriseCallableRequestHandler;
use Sunrise\Http\Router\RouteInterface as SunriseRouteInterface;
use ReflectionClass;
use ReflectionMethod;
use Reflector;

/**
* Import functions
*/
use function is_array;
use function Sunrise\Http\Router\path_parse;
use function Sunrise\Http\Router\path_plain;

/**
* Sunrise Route Proxy
*/
final class SunriseRouteProxy implements OpenapiRouteInterface
{

/**
* Proxied Sunrise Route
*
* @var SunriseRouteInterface
*/
private $route;

/**
* Constructor of the class
*
* @param SunriseRouteInterface $route
*/
public function __construct(SunriseRouteInterface $route)
{
$this->route = $route;
}

/**
* {@inheritdoc}
*/
public function getName() : string
{
return $this->route->getName();
}

/**
* {@inheritdoc}
*/
public function getMethods() : array
{
return $this->route->getMethods();
}

/**
* {@inheritdoc}
*/
public function getPlainPath() : string
{
return path_plain($this->route->getPath());
}

/**
* {@inheritdoc}
*/
public function getPathAttributes() : array
{
return path_parse($this->route->getPath());
}

/**
* {@inheritdoc}
*/
public function getSummary() : string
{
return $this->route->getSummary();
}

/**
* {@inheritdoc}
*/
public function getDescription() : string
{
return $this->route->getDescription();
}

/**
* {@inheritdoc}
*/
public function getTags() : array
{
return $this->route->getTags();
}

/**
* {@inheritdoc}
*/
public function getHolder() : ?Reflector
{
$handler = $this->route->getRequestHandler();
if (!($handler instanceof SunriseCallableRequestHandler)) {
return new ReflectionClass($handler);
}

$callback = $handler->getCallback();
if (is_array($callback)) {
return new ReflectionMethod(...$callback);
}

return null;
}
}
Loading

0 comments on commit bb63358

Please sign in to comment.