Skip to content

Commit a53867f

Browse files
committed
Updated readme and fixed scrutinizer
1 parent a86f30a commit a53867f

File tree

2 files changed

+62
-47
lines changed

2 files changed

+62
-47
lines changed

README.md

+60-45
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
## Why Nette-Api
1414

15-
This library provides out-of-the box API solution for Nette framework. You can register API endpoints and connect it to specified handlers. You need only implement you custom business logic. Library provide authorisation, validation and formatting services for you api.
15+
This library provides out-of-the box API solution for Nette framework. You can register API endpoints and connect it to specified handlers. You need only implement you custom business logic. Library provide authorization, validation and formatting services for you API.
1616

1717
## Installation
1818

19-
This library requires PHP 5.4 or later. It works also on PHP 7.0.
19+
This library requires PHP 7.1 or later.
2020

2121
Recommended installation method is via Composer:
2222

23-
``` bash
23+
```bash
2424
$ composer require tomaj/nette-api
2525
```
2626

@@ -35,51 +35,52 @@ Library is compliant with [PSR-1][], [PSR-2][], [PSR-3][] and [PSR-4][].
3535

3636
First you have register library presenter for routing. In *config.neon* just add this line:
3737

38-
``` yaml
38+
```neon
3939
application:
4040
mapping:
4141
Api: Tomaj\NetteApi\Presenters\*Presenter
4242
```
4343

4444
And add route to you RouterFactory:
4545

46-
``` php
46+
```php
4747
$router[] = new Route('/api/v<version>/<package>[/<apiAction>][/<params>]', 'Api:Api:default');
4848
```
4949

5050
After that you need only register your api handlers to *apiDecider* [ApiDecider](src/ApiDecider.php) and register [ApiLink](src/Link/ApiLink.php) and [Tomaj\NetteApi\Misc\IpDetector](src/Misc/IpDetector.php). This can be done also with *config.neon*:
5151

52-
``` yaml
52+
```neon
5353
services:
5454
- Tomaj\NetteApi\Link\ApiLink
5555
- Tomaj\NetteApi\Misc\IpDetector
5656
apiDecider:
57-
class: Tomaj\NetteApi\ApiDecider
57+
factory: Tomaj\NetteApi\ApiDecider
5858
setup:
59-
- addApiHandler(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), \Tomaj\NetteApi\Authorization\NoAuthorization())
60-
- addApiHandler(\Tomaj\NetteApi\EndpointIdentifier('POST', 1, 'users', 'send-email'), \App\MyApi\v1\Handlers\SendEmailHandler(), \Tomaj\NetteApi\Authorization\BearerTokenAuthorization())
59+
- addApi(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), \Tomaj\NetteApi\Authorization\NoAuthorization())
60+
- addApi(\Tomaj\NetteApi\EndpointIdentifier('POST', 1, 'users', 'send-email'), \App\MyApi\v1\Handlers\SendEmailHandler(), \Tomaj\NetteApi\Authorization\BearerTokenAuthorization())
6161
```
6262

63-
As you can see in example, you can register as many endpoints as you want with different configurations. Nette-Api support api versioning from the beginning.
64-
This example will prepare this api calls:
63+
As you can see in example, you can register as many endpoints as you want with different configurations. Nette-Api supports API versioning from the beginning.
64+
This example will prepare these API calls:
6565

6666
1. `http://yourapp/api/v1/users` - available via GET
6767
2. `http://yourapp/api/v1/users/send-email` - available via POST
6868

6969

70-
Core of the Nette-Api is handlers. For this example you need implement 2 classes:
70+
Core of the Nette-Api are handlers. For this example you need implement 2 classes:
7171

7272
1. App\MyApi\v1\Handlers\UsersListingHandler
7373
2. App\MyApi\v1\Handlers\SendEmailHandler
7474

75-
This handlers implements interface *[ApiHandlerInterface](src/Handlers/ApiHandlerInterface.php)* but for easier usage you can extends your handler from [BaseHandler](src/Handlers/BaseHandler.php).
76-
When someone reach your api this handlers will be triggered and *handle()* method will be called.
75+
These handlers implement interface *[ApiHandlerInterface](src/Handlers/ApiHandlerInterface.php)* but for easier usage you can extends your handlers from [BaseHandler](src/Handlers/BaseHandler.php).
76+
When someone reach your API this handlers will be triggered and *handle()* method will be called.
7777

78-
``` php
78+
```php
7979
namespace App\MyApi\v1\Handlers;
8080

8181
use Tomaj\NetteApi\Handlers\BaseHandler;
8282
use Tomaj\NetteApi\Response\JsonApiResponse;
83+
use Tomaj\NetteApi\Response\ResponseInterface;
8384

8485
class UsersListingHandler extends Basehandler
8586
{
@@ -91,7 +92,7 @@ class UsersListingHandler extends Basehandler
9192
$this->userRepository = $userRepository;
9293
}
9394

94-
public function handle($params)
95+
public function handle(array $params): ResponseInterface
9596
{
9697
$users = [];
9798
foreach ($this->userRepository->all() as $user) {
@@ -115,7 +116,7 @@ Example with fractal:
115116

116117
1. You will need Formater
117118

118-
``` php
119+
```php
119120
namespace App\MyApi\v1\Transformers;
120121

121122
use League\Fractal\TransformerAbstract;
@@ -135,11 +136,12 @@ class UserTransformer extends TransformerAbstract
135136

136137
2. And this will be your handler:
137138

138-
``` php
139+
```php
139140
namespace App\MyApi\v1\Handlers;
140141

141142
use Tomaj\NetteApi\Handlers\BaseHandler;
142143
use Tomaj\NetteApi\Response\JsonApiResponse;
144+
use Tomaj\NetteApi\Response\ResponseInterface;
143145

144146
class UsersListingHandler extends Basehandler
145147
{
@@ -151,7 +153,7 @@ class UsersListingHandler extends Basehandler
151153
$this->userTransformer = $userTransformer;
152154
}
153155

154-
public function handle($params)
156+
public function handle(array $params): ResponseInterface
155157
{
156158
$users = $this->useRepository->all();
157159

@@ -173,12 +175,13 @@ Each handler can describe which input is required. It could be GET or POST param
173175

174176
Example with user detail:
175177

176-
``` php
178+
```php
177179
namespace App\MyApi\v1\Handlers;
178180

179181
use Tomaj\NetteApi\Handlers\BaseHandler;
180182
use Tomaj\NetteApi\Response\JsonApiResponse;
181183
use Tomaj\NetteApi\Params\InputParam;
184+
use Tomaj\NetteApi\Response\ResponseInterface;
182185

183186
class UsersDetailHandler extends Basehandler
184187
{
@@ -190,14 +193,14 @@ class UsersDetailHandler extends Basehandler
190193
$this->userRepository = $userRepository;
191194
}
192195

193-
public function params()
196+
public function params(): array
194197
{
195198
return [
196199
new InputParam(InputParam::TYPE_GET, 'id', InputParam::REQUIRED),
197200
];
198201
}
199202

200-
public function handle($params)
203+
public function handle(array $params): ResponseInterface
201204
{
202205
$user = $this->userRepository->find($params['id']);
203206
if (!$user) {
@@ -231,18 +234,18 @@ This is table with support input types:
231234

232235
## Security
233236

234-
Protecting your api is easy with Nette-Api. You have to implement your [Authorization](src/Authorization/ApiAuthorizationInterface.php) (Tomaj\NetteApi\Authorization\ApiAuthorizationInterface) and add it as third argument to *addApiHandler()* method in *config.neon*.
237+
Protecting your api is easy with Nette-Api. You have to implement your [Authorization](src/Authorization/ApiAuthorizationInterface.php) (Tomaj\NetteApi\Authorization\ApiAuthorizationInterface) and add it as third argument to *addApi()* method in *config.neon*.
235238

236239
For simple use, if you want to use Bearer token authorisation with few tokens, you can use [StaticBearerTokenRepository](src/Misc/StaticBearerTokenRepository.php) (Tomaj\NetteApi\Misc\StaticBearerTokenRepository).
237240

238-
``` yaml
241+
```neon
239242
services:
240243
staticBearer: Tomaj\NetteApi\Misc\StaticBearerTokenRepository(['dasfoihwet90hidsg': '*', 'asfoihweiohgwegi': '127.0.0.1'])
241244
242245
apiDecider:
243-
class: Tomaj\NetteApi\ApiDecider
246+
factory: Tomaj\NetteApi\ApiDecider
244247
setup:
245-
- addApiHandler(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), \Tomaj\NetteApi\Authorization\BearerTokenAuthorization(@staticBearer))
248+
- addApi(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), \Tomaj\NetteApi\Authorization\BearerTokenAuthorization(@staticBearer))
246249
247250
```
248251

@@ -255,7 +258,7 @@ In Nette-Api if you would like to specify IP restrictions for tokens you can use
255258
|`127.0.0.1` | accessible from single IP
256259
|`127.0.0.1,127.0.02` | accessible from multiple IP, separator could be new line or space
257260
|`127.0.0.1/32` | accessible from ip range
258-
|*false* | token is disabled, cannot access
261+
|*null* | token is disabled, cannot access
259262

260263

261264
But it is very easy to implement your own Authorisation for API.
@@ -267,24 +270,24 @@ Nette-api is ready for this situation and you can choose if you want to enable p
267270

268271
Globally enabled - every api endpoint will be available for preflight OPTIONS call:
269272

270-
``` neon
273+
```neon
271274
services:
272275
apiDecider:
273-
class: Tomaj\NetteApi\ApiDecider
276+
factory: Tomaj\NetteApi\ApiDecider
274277
setup:
275278
- enableGlobalPreflight()
276-
- addApiHandler(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), Tomaj\NetteApi\Authorization\NoAuthorization())
279+
- addApi(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), Tomaj\NetteApi\Authorization\NoAuthorization())
277280
```
278281

279282
Or you can register custom OPTIONS endpoints:
280283

281-
``` neon
284+
```neon
282285
services:
283286
apiDecider:
284-
class: Tomaj\NetteApi\ApiDecider
287+
factory: Tomaj\NetteApi\ApiDecider
285288
setup:
286-
- addApiHandler(\Tomaj\NetteApi\EndpointIdentifier('OPTIONS', 1, 'users'), \Tomaj\NetteApi\Handlers\CorsPreflightHandler(), Tomaj\NetteApi\Authorization\NoAuthorization())
287-
- addApiHandler(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), Tomaj\NetteApi\Authorization\NoAuthorization())
289+
- addApi(\Tomaj\NetteApi\EndpointIdentifier('OPTIONS', 1, 'users'), \Tomaj\NetteApi\Handlers\CorsPreflightHandler(), Tomaj\NetteApi\Authorization\NoAuthorization())
290+
- addApi(\Tomaj\NetteApi\EndpointIdentifier('GET', 1, 'users'), \App\MyApi\v1\Handlers\UsersListingHandler(), Tomaj\NetteApi\Authorization\NoAuthorization())
288291
289292
```
290293

@@ -303,10 +306,10 @@ If you need to iteract with your API with Javascript you will need to send corre
303306

304307
You can set this property in config.neon if you register [ApiPresenter](src/Presenters/ApiPresenter.php):
305308

306-
``` neon
309+
```neon
307310
services:
308311
-
309-
class: Tomaj\NetteApi\Presenters\ApiPresenter
312+
factory: Tomaj\NetteApi\Presenters\ApiPresenter
310313
setup:
311314
- setCorsHeader('auto')
312315
```
@@ -323,7 +326,7 @@ All components generate bootstrap html and can be styled with bootstrap css:
323326

324327
You have to create components in your controller:
325328

326-
``` php
329+
```php
327330
use Nette\Application\UI\Presenter;
328331
use Tomaj\NetteApi\ApiDecider;
329332
use Tomaj\NetteApi\Component\ApiConsoleControl;
@@ -333,29 +336,41 @@ class MyPresenter extends Presenter
333336
{
334337
private $apiDecider;
335338

339+
private $method;
340+
341+
private $version;
342+
343+
private $package;
344+
345+
private $apiAction;
346+
336347
public function __construct(ApiDecider $apiDecider)
337348
{
338349
parent::__construct();
339350
$this->apiDecider = $apiDecider;
340351
}
341352

342-
public function renderShow($method, $version, $package, $apiAction)
353+
public function renderShow(string $method, int $version, string $package, ?string $apiAction = null): void
343354
{
355+
$this->method = $method;
356+
$this->version = $version;
357+
$this->package = $package;
358+
$this->apiAction = $apiAction;
344359
}
345360

346-
protected function createComponentApiListing()
361+
protected function createComponentApiListing(): ApiListingControl
347362
{
348-
$apiListing = new ApiListingControl($this, 'apiListingControl', $this->apiDecider);
349-
$apiListing->onClick(function ($method, $version, $package, $apiAction) {
363+
$apiListing = new ApiListingControl($this->apiDecider);
364+
$apiListing->onClick[] = function ($method, $version, $package, $apiAction) {
350365
$this->redirect('show', $method, $version, $package, $apiAction);
351-
});
366+
};
352367
return $apiListing;
353368
}
354369

355370
protected function createComponentApiConsole()
356371
{
357-
$api = $this->apiDecider->getApiHandler($this->params['method'], $this->params['version'], $this->params['package'], isset($this->params['apiAction']) ? $this->params['apiAction'] : null);
358-
$apiConsole = new ApiConsoleControl($this->getHttpRequest(), $api['endpoint'], $api['handler'], $api['authorization']);
372+
$api = $this->apiDecider->getApiHandler($this->method, $this->version, $this->package, $this->apiAction);
373+
$apiConsole = new ApiConsoleControl($this->getHttpRequest(), $api->getEndpoint(), $api->getHandler(), $api->getAuthorization());
359374
return $apiConsole;
360375
}
361376
}
@@ -377,7 +392,7 @@ Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recen
377392

378393
## Testing
379394

380-
``` bash
395+
```bash
381396
$ composer test
382397
```
383398

src/Authorization/BearerTokenAuthorization.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(BearerTokenRepositoryInterface $tokenRepository, IpD
4040
public function authorized(): bool
4141
{
4242
$token = $this->readAuthorizationToken();
43-
if (!$token) {
43+
if ($token === null) {
4444
return false;
4545
}
4646

@@ -73,7 +73,7 @@ public function getErrorMessage(): ?string
7373
* '127.0.0.1' - accessible from single IP
7474
* '127.0.0.1,127.0.02' - accessible from multiple IP, separator could be new line or space
7575
* '127.0.0.1/32' - accessible from ip range
76-
* false - disabled access
76+
* null - disabled access
7777
*
7878
* @return boolean
7979
*/

0 commit comments

Comments
 (0)