Skip to content

Commit

Permalink
Merge pull request #133 from tomaj/api-link-extension
Browse files Browse the repository at this point in the history
Added support for latte/latte ^3.0 (Added Latte extension for ApiLink)
  • Loading branch information
tomaj authored Sep 5, 2023
2 parents b07129c + ceb7ed5 commit acc9031
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 35 deletions.
61 changes: 31 additions & 30 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,54 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## [Unreleased][unreleased]

#### Added
### Added

* Support for symfony/yaml ^5.0 and ^6.0
* Support for JsonSerializable payload in JsonApiResponse
* Support for latte/latte ^3.0 (Added Latte extension for ApiLink)

## 2.10.1

#### Fixed
### Fixed

* Fix *null* `$body` for php 8.0

## 2.10.0

#### Fixed

* Fixed request url separator if url already contain some param
## 2.10.0 - 2023-03-21

#### Added
### Added

* Allow to set expiration to false in JsonApiResponse and XmlApiResponse
* Render body of response if there is a debug message: Tracy Debug Bar

### Fixed

* Fixed request url separator if url already contain some param

## 2.9.0 - 2022-11-10

#### Added
### Added

* Support for generating examples for RawInputParam by OpenApiHandler
* PHP 8.1 support (`league/fractal` 0.20.1)
* PHP 8.2 support
* Support for `league/fractal` ~0.17 (Possible BC if you use Fractal classes, you have to update typehints)

#### Changed
### Changed

* If output doesn't match any output schema, error is just logged and output is returned in production mode

#### Fixed
### Fixed

* Missing expiration params in XmlApiResponse
* Missing body request for PUT methost

## 2.8.0 - 2022-06-15

#### Changed
### Changed

* ApiLinkMacro now uses latte filter to avoid use presenter context (BC break in nette/application 3.1)

#### Fixed
### Fixed

* BaseUrl in OpenApiHandler

Expand All @@ -75,7 +76,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## 2.5.0 - 2021-09-17

#### Fixed
### Fixed

* Empty console input fixed by adding checkbox for each parameter
* Handling wrong input for Get and Post InputParam
Expand All @@ -84,29 +85,29 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## 2.4.0 - 2021-04-24

#### Added
### Added

* User can set own form renderer for API console and own template file(s) for API console and for API listing

## 2.3.1 - 2021-04-13

#### Fixed
### Fixed

* Added RateLimit to ApiDecider::addApi

## 2.3.0 - 2021-01-20

#### Changed
### Changed

* Form in API console is rendered with BootstrapVerticalRenderer instead of BootstrapRenderer (labels are over fields instead of left side)

#### Added
### Added

* Added API key authentication (query, header, cookie) - see https://swagger.io/docs/specification/authentication/api-keys/

* Added missing strict_types declarations

#### Fixed
### Fixed

* Open API handler warnings about unused security schemes

Expand All @@ -116,25 +117,25 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## 2.2.0 - 2020-08-27

#### Added
### Added

* Added Basic authentication

## 2.1.0 - 2020-05-12

#### Changed
### Changed

* Rewritten ApiPresenter

#### Added
### Added

* Added API rate limit
* Added custom headers to API console
* Added field for timeout to API console
* OpenAPI handler
* Information about RESTful urls

#### Fixed
### Fixed

* Fixed sending empty string in multi params
* UrlEncoding values sending through get param inputs
Expand All @@ -143,13 +144,13 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## 2.0.1 - 2020-03-24

#### Fixed
### Fixed

* Fixed return types for ConsoleRequest::processParam() and ConsoleResponse::getResponseHeaders()

## 2.0.0 - 2019-06-12

#### Changed
### Changed

* Updated nette libs to version 3.0.0 (BC break)
* Added typehints (BC break)
Expand All @@ -160,34 +161,34 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
* Renamed some methods from ApiDecider (BC break)
* Pretty JSON output in API console - without escaping unicode and slashes

#### Added
### 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

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

## 1.17.0 - 2020-08-27

#### Changed
### Changed

* Detailed error message enabled only for non production mode

## 1.16.0 - 2019-06-19

#### Added
### Added

* Added ApiLink Macro

## 1.15.0 - 2019-03-13

#### Added
### 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
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,35 @@ We recommend to take a look at [Fractal][] library. There are much more informat
[Fractal]: http://fractal.thephpleague.com/


## ApiLink Macro
## ApiLink in latte

First you have to register filter helper and than macro. In *config.neon* just add these lines:
First, you have to register filter in *config.neon*:

``` yaml
```neon
services:
apiLink: Tomaj\NetteApi\Link\ApiLink()
latte.latteFactory:
setup:
- addFilter(apiLink, [@apiLink, link])
```
**Note**: Name of filter has to be `apiLink`, because it is used in macro / extension.

For latte < 3.0 register latte macro:
```neon
latte:
macros:
- Tomaj\NetteApi\Link\ApiLinkMacro
```

For latte >= 3.0 register latte extension:
```neon
latte:
extensions:
- Tomaj\NetteApi\Link\ApiLinkExtension
```


Usage in latte files:

```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
},
"require-dev": {
"nette/di": "^3.0",
"latte/latte": "^2.4",
"latte/latte": "^2.4 | ^3.0",
"phpunit/phpunit": ">7.0 <10.0",
"symfony/yaml": "^4.4|5.0",
"symfony/yaml": "^4.4|^5.0|^6.0",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
Expand Down
17 changes: 17 additions & 0 deletions src/Link/ApiLinkExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Link;

use Latte\Extension;

final class ApiLinkExtension extends Extension
{
public function getTags(): array
{
return [
'apiLink' => [ApiLinkNode::class, 'create'],
];
}
}
46 changes: 46 additions & 0 deletions src/Link/ApiLinkNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Link;

use Latte\Compiler\Nodes\Php\Expression\ArrayNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;

final class ApiLinkNode extends StatementNode
{
/** @var ArrayNode $endpointArgs */
public $endpointArgs;

/** @var ArrayNode $endpointParams */
public $endpointParams;

public static function create(Tag $tag): ?ApiLinkNode
{
$tag->expectArguments();
$node = new self();

$tag->parser->stream->tryConsume(',');
$args = $tag->parser->parseArguments();

$allParameters = $args->items;
$node->endpointArgs = new ArrayNode(array_slice($allParameters, 0, 4));
$node->endpointParams = new ArrayNode(array_slice($allParameters, 4));

return $node;
}

public function print(PrintContext $context): string
{
return $context->format('echo ($this->filters->apiLink)(new Tomaj\NetteApi\EndpointIdentifier(%args), %args);', $this->endpointArgs, $this->endpointParams);
}


public function &getIterator(): \Generator
{
yield $this->endpointArgs;
yield $this->endpointParams;
}
}

0 comments on commit acc9031

Please sign in to comment.