Skip to content

Commit

Permalink
2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lulco authored Jun 12, 2019
2 parents 885d88e + 8ca2725 commit 5302c6c
Show file tree
Hide file tree
Showing 76 changed files with 1,958 additions and 967 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ language: php
services:

php:
- 5.6
- 7.0
- 7.1
- hhvm
- 7.2
- 7.3

sudo: false

matrix:
allow_failures:
- php: hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction
Expand Down
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

#### Changed

* Updated nette libs to version 3.0.0 (BC break)
* Added typehints (BC break)
* Splitted InputParam to multiple subclasses (BC break)
* Removed type TYPE_POST_JSON_KEY (BC break)
* Wrong input now returns code 400 instead of 500 (BC break if somebody checks return code)
* Replaced handler information array triplet (endpoint, handler, authorization) with class Api (BC break for API console usage)
* Renamed some methods from ApiDecider (BC break)
* Pretty JSON output in API console - without escaping unicode and slashes

#### Added

* Added type JsonInputParam with scheme as replacement for type TYPE_POST_JSON_KEY
* Detailed error for wrong input if debugger is enabled
* Added summary (short description), description, tags and deprecated flag for API handlers
* Added description, default value and example for input params
* Added output validator

#### Removed

* Removed support for PHP 5.6, 7.0 and hhvm (BC Break)
* Removed deprecated class ApiResponse (BC Break)

## 1.15.0 - 2019-03-13

#### Added

* Added possibility to set own scope to Manager

* Added JSON validation - if JSON is invalid, throw "wrong input" error instead of setting params to null

## 1.14.0 - 2018-08-02
Expand Down
180 changes: 114 additions & 66 deletions README.md

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# UPGRADE

## Upgrade from 1.x to 2.0.0

### Splitted InputParam to multiple subclasses
InputParam is now abstract class and all types have their own classes. Also InputParam is more like Nette Form inputs with fluent API.

Examples of replacements:

Required GET input with available values:

Old:
```php
new InputParam(InputParam::TYPE_GET, 'status', InputParam::REQUIRED, ['ok', 'error'])
```

New:
```php
(new GetInputParam('status'))->setRequired()->setAvailableValues(['ok', 'error'])
```

Multiple optional FILE input:

Old:
```php
new InputParam(InputParam::TYPE_FILE, 'myfile', InputParam::OPTIONAL, null, true)
```

New:
```php
(new FileInputParam('myfile'))->setMulti()
```

For more info about types, see readme section Input Types.

### Removed support for old PHP versions
New version does not support PHP versions 5.6 and 7.0 and also hhvm. Please use it with newer versions of PHP (>7.1)

### Updated dependencies
Version 2.0.0 requires nette packages in version 3.0, so probably you will have to upgrade whole nette application.

### Typehints
There are some breaking changes because of added typehints:

#### ApiAuthorizationInterface
Add typehints to methods:
- `authorized(): bool`
- `getErrorMessage(): ?string`

#### ApiHandlerInterface
Add typehints to methods:
- `params(): array`
- `handle(array $params): Tomaj\NetteApi\Response\ResponseInterface`

#### ApiLoggerInterface
Add typehints to method:
- `log(int $responseCode, string $requestMethod, string $requestHeader, string $requestUri, string $requestIp, string $requestAgent, int $responseTime): bool`

#### BearerTokenRepositoryInterface
Add typehints to methods:
- `validToken(string $token): bool`
- `ipRestrictions(string $token): ?string`

#### IpDetectorInterface
Add typehints to method:
- `getRequestIp(): string`

#### ParamInterface
Add typehints to methods:
- `isValid(): bool`
- `getKey(): string`

#### EndpointInterface
Add typehints to methods:
- `getMethod(): string`
- `getVersion(): int`
- `getPackage(): string`
- `getApiAction(): ?string`
- `getUrl(): string`

### Changed behavior
API handler tripplet (array of endpoint, handler, authorization) has been changed to class `Api` which has methods `getEndpoint()`, `getHandler()` and `getAuthorization()`.

### Renamed methods
Few methods have been renamed, please use their new versions:
- `ApiDecider::addApiHandler()` -> `ApiDecider::addApi()`
- `ApiDecider::getApiHandler()` -> `ApiDecider::getApi()`
- `ApiDecider::getHandlers()` -> `ApiDecider::getApis()`

### Final methods
BaseHandler now have few final methods:
- `setEndpointIdentifier`
- `getEndpoint`
- `setupLinkGenerator`
- `createLink`

### Removed class
Class ApiResponse has been removed. Use JsonApiResponse instead.

### Removed params
Parameters $parent and $name have been removed from ApiListingControl. New usage is:
```php
new ApiListingControl($apiDecider)
```

### Changed params
Some parameters were strictly typed:
- second parameter in `JsonApiResponse::__construct` (`$payload` formerly known as `$data`) is now `array`
- fifth parameter in `JsonApiResponse::__construct` (`$expiration`) is now `DateTimeInteface` or `null`
- fourth parameter in `InputParam::__construct` (`$availableValues`, now parameter of method `setAvailableValues()`) is now `array`

### Changed events
Registration of event onClick in ApiListingControl has been changed.
Use:
```php
$apiListing->onClick[] = function ($method, $version, $package, $apiAction) {
...
};
```

### Features
With new version of Nette API you can:
- add description to your API handlers, also you can mark some handlers as deprecated and add tags for them.
- add description, default value and example for all your input params.
- add list of possible outputs which are validate before response is sent to user. If output is not valid, error is sent.
18 changes: 10 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@
}
],
"require": {
"php": ">= 5.6.0",
"php": ">= 7.1.0",
"ext-curl": "*",
"ext-json": "*",
"ext-session": "*",
"ext-filter": "*",
"nette/application": "^2.3.12",
"nette/http": "^2.0",
"tracy/tracy": "^2.0",
"nette/application": "^3.0",
"nette/http": "^3.0",
"tracy/tracy": "^2.6",
"league/fractal": "^0.17.0",
"tomaj/nette-bootstrap-form": "^1.1"
"tomaj/nette-bootstrap-form": "^2.0",
"justinrainbow/json-schema": "^5.2"
},
"require-dev": {
"nette/di": "^2.0",
"phpunit/phpunit": "^5.3",
"squizlabs/php_codesniffer": "^3.0"
"nette/di": "^3.0",
"latte/latte": "^2.4",
"phpunit/phpunit": ">7.0",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
39 changes: 39 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi;

use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
use Tomaj\NetteApi\Handlers\ApiHandlerInterface;

class Api
{
private $endpoint;

private $handler;

private $authorization;

public function __construct(EndpointInterface $endpoint, ApiHandlerInterface $handler, ApiAuthorizationInterface $authorization)
{
$this->endpoint = $endpoint;
$this->handler = $handler;
$this->authorization = $authorization;
}

public function getEndpoint(): EndpointInterface
{
return $this->endpoint;
}

public function getHandler(): ApiHandlerInterface
{
return $this->handler;
}

public function getAuthorization(): ApiAuthorizationInterface
{
return $this->authorization;
}
}
66 changes: 27 additions & 39 deletions src/ApiDecider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi;

use Nette\Http\Response;
Expand All @@ -11,14 +13,10 @@

class ApiDecider
{
/**
* @var ApiHandlerInterface[]
*/
private $handlers = [];
/** @var Api[] */
private $apis = [];

/**
* @var ApiHandlerInterface
*/
/** @var ApiHandlerInterface|null */
private $globalPreflightHandler = null;

/**
Expand All @@ -30,30 +28,25 @@ class ApiDecider
* @param string $package
* @param string $apiAction
*
* @return array
* @return Api
*/
public function getApiHandler($method, $version, $package, $apiAction = '')
public function getApi(string $method, int $version, string $package, ?string $apiAction = null)
{
foreach ($this->handlers as $handler) {
$identifier = $handler['endpoint'];
if ($method == $identifier->getMethod() && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
$method = strtoupper($method);
$apiAction = $apiAction === '' ? null : $apiAction;

foreach ($this->apis as $api) {
$identifier = $api->getEndpoint();
if ($method === $identifier->getMethod() && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) {
$endpointIdentifier = new EndpointIdentifier($method, $version, $package, $apiAction);
$handler['handler']->setEndpointIdentifier($endpointIdentifier);
return $handler;
$api->getHandler()->setEndpointIdentifier($endpointIdentifier);
return $api;
}
if ($method == 'OPTIONS' && $this->globalPreflightHandler && $identifier->getVersion() == $version && $identifier->getPackage() == $package && $identifier->getApiAction() == $apiAction) {
return [
'endpoint' => new EndpointIdentifier('OPTION', $version, $package, $apiAction),
'authorization' => new NoAuthorization(),
'handler' => $this->globalPreflightHandler,
];
if ($method === 'OPTIONS' && $this->globalPreflightHandler && $identifier->getVersion() === $version && $identifier->getPackage() === $package && $identifier->getApiAction() === $apiAction) {
return new Api(new EndpointIdentifier('OPTIONS', $version, $package, $apiAction), $this->globalPreflightHandler, new NoAuthorization());
}
}
return [
'endpoint' => new EndpointIdentifier($method, $version, $package, $apiAction),
'authorization' => new NoAuthorization(),
'handler' => new DefaultHandler()
];
return new Api(new EndpointIdentifier($method, $version, $package, $apiAction), new DefaultHandler(), new NoAuthorization());
}

public function enableGlobalPreflight(ApiHandlerInterface $corsHandler = null)
Expand All @@ -67,29 +60,24 @@ public function enableGlobalPreflight(ApiHandlerInterface $corsHandler = null)
/**
* Register new api handler
*
* @param EndpointInterface $endpointIdentifier
* @param ApiHandlerInterface $handler
* @param EndpointInterface $endpointIdentifier
* @param ApiHandlerInterface $handler
* @param ApiAuthorizationInterface $apiAuthorization
*
* @return $this
* @return self
*/
public function addApiHandler(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization)
public function addApi(EndpointInterface $endpointIdentifier, ApiHandlerInterface $handler, ApiAuthorizationInterface $apiAuthorization): self
{
$this->handlers[] = [
'endpoint' => $endpointIdentifier,
'handler' => $handler,
'authorization' => $apiAuthorization,
];
$this->apis[] = new Api($endpointIdentifier, $handler, $apiAuthorization);
return $this;
}

/**
* Get all registered handlers
* Get all registered apis
*
* @return Handlers\ApiHandlerInterface[]
* @return Api[]
*/
public function getHandlers()
public function getApis(): array
{
return $this->handlers;
return $this->apis;
}
}
12 changes: 0 additions & 12 deletions src/ApiResponse.php

This file was deleted.

Loading

0 comments on commit 5302c6c

Please sign in to comment.