From eb6e4b7f75e28318739cff4f1c82ea5b35a0c29f Mon Sep 17 00:00:00 2001 From: Michal Lulco Date: Wed, 5 Jul 2023 12:42:26 +0200 Subject: [PATCH 1/3] Added support for latte/latte ^3.0 (Added Latte extension for ApiLink) --- CHANGELOG.md | 55 +++++++++++++++++++---------------- README.md | 20 +++++++++++-- composer.json | 2 +- src/Link/ApiLinkExtension.php | 20 +++++++++++++ src/Link/ApiLinkNode.php | 48 ++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 src/Link/ApiLinkExtension.php create mode 100644 src/Link/ApiLinkNode.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b54ff0..5db00ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,41 +4,46 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ## [Unreleased][unreleased] -#### Fixed +### Added -* Fixed request url separator if url already contain some param +* Support for latte/latte ^3.0 (Added Latte extension for ApiLink) -#### Added +## 2.10.0 - 2023-03-21 + +### 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 @@ -63,7 +68,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 @@ -72,29 +77,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 @@ -104,17 +109,17 @@ 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 @@ -122,7 +127,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip * OpenAPI handler * Information about RESTful urls -#### Fixed +### Fixed * Fixed sending empty string in multi params * UrlEncoding values sending through get param inputs @@ -131,13 +136,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) @@ -148,7 +153,7 @@ 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 @@ -156,26 +161,26 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip * 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 diff --git a/README.md b/README.md index 205857b..786f1bc 100644 --- a/README.md +++ b/README.md @@ -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: ``` diff --git a/composer.json b/composer.json index f9b9576..91e95a5 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "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", "squizlabs/php_codesniffer": "^3.2" diff --git a/src/Link/ApiLinkExtension.php b/src/Link/ApiLinkExtension.php new file mode 100644 index 0000000..af065d1 --- /dev/null +++ b/src/Link/ApiLinkExtension.php @@ -0,0 +1,20 @@ + [ApiLinkNode::class, 'create'], + ]; + } +} diff --git a/src/Link/ApiLinkNode.php b/src/Link/ApiLinkNode.php new file mode 100644 index 0000000..9a92985 --- /dev/null +++ b/src/Link/ApiLinkNode.php @@ -0,0 +1,48 @@ +expectArguments(); + $node = new static; + + $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 + { + + // TODO posledny parameter - params ma ist za EndpointIdentifier + 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; + } +} From bb87ddfaee8ea13ccf385b2dc7f488aa9267ebd6 Mon Sep 17 00:00:00 2001 From: Michal Lulco Date: Wed, 5 Jul 2023 12:48:01 +0200 Subject: [PATCH 2/3] Fixed php 7 syntax --- src/Link/ApiLinkExtension.php | 1 - src/Link/ApiLinkNode.php | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Link/ApiLinkExtension.php b/src/Link/ApiLinkExtension.php index af065d1..ef32f87 100644 --- a/src/Link/ApiLinkExtension.php +++ b/src/Link/ApiLinkExtension.php @@ -7,7 +7,6 @@ use Latte\Compiler\Tag; use Latte\Compiler\TemplateParser; use Latte\Extension; -use Tomaj\NetteApi\EndpointIdentifier; final class ApiLinkExtension extends Extension { diff --git a/src/Link/ApiLinkNode.php b/src/Link/ApiLinkNode.php index 9a92985..06285e5 100644 --- a/src/Link/ApiLinkNode.php +++ b/src/Link/ApiLinkNode.php @@ -13,9 +13,11 @@ final class ApiLinkNode extends StatementNode { - public ArrayNode $endpointArgs; + /** @var ArrayNode $endpointArgs */ + public $endpointArgs; - public ArrayNode $endpointParams; + /** @var ArrayNode $endpointParams */ + public $endpointParams; public static function create(Tag $tag): ?static { From fddd26c79b60589f3ee99ed2f06b5e6ad14118d2 Mon Sep 17 00:00:00 2001 From: Michal Lulco Date: Wed, 5 Jul 2023 12:57:20 +0200 Subject: [PATCH 3/3] PHP 7 syntax, symfony 6 support --- CHANGELOG.md | 1 + composer.json | 2 +- src/Link/ApiLinkExtension.php | 2 -- src/Link/ApiLinkNode.php | 8 ++------ 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5db00ef..8f420a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ### Added * Support for latte/latte ^3.0 (Added Latte extension for ApiLink) +* Support for symfony/yaml ^5.0 and ^6.0 ## 2.10.0 - 2023-03-21 diff --git a/composer.json b/composer.json index 91e95a5..9c52e1f 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "nette/di": "^3.0", "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": { diff --git a/src/Link/ApiLinkExtension.php b/src/Link/ApiLinkExtension.php index ef32f87..fed37fd 100644 --- a/src/Link/ApiLinkExtension.php +++ b/src/Link/ApiLinkExtension.php @@ -4,8 +4,6 @@ namespace Tomaj\NetteApi\Link; -use Latte\Compiler\Tag; -use Latte\Compiler\TemplateParser; use Latte\Extension; final class ApiLinkExtension extends Extension diff --git a/src/Link/ApiLinkNode.php b/src/Link/ApiLinkNode.php index 06285e5..1699971 100644 --- a/src/Link/ApiLinkNode.php +++ b/src/Link/ApiLinkNode.php @@ -5,8 +5,6 @@ namespace Tomaj\NetteApi\Link; use Latte\Compiler\Nodes\Php\Expression\ArrayNode; -use Latte\Compiler\Nodes\Php\ExpressionNode; -use Latte\Compiler\Nodes\Php\ModifierNode; use Latte\Compiler\Nodes\StatementNode; use Latte\Compiler\PrintContext; use Latte\Compiler\Tag; @@ -19,10 +17,10 @@ final class ApiLinkNode extends StatementNode /** @var ArrayNode $endpointParams */ public $endpointParams; - public static function create(Tag $tag): ?static + public static function create(Tag $tag): ?ApiLinkNode { $tag->expectArguments(); - $node = new static; + $node = new self(); $tag->parser->stream->tryConsume(','); $args = $tag->parser->parseArguments(); @@ -36,8 +34,6 @@ public static function create(Tag $tag): ?static public function print(PrintContext $context): string { - - // TODO posledny parameter - params ma ist za EndpointIdentifier return $context->format('echo ($this->filters->apiLink)(new Tomaj\NetteApi\EndpointIdentifier(%args), %args);', $this->endpointArgs, $this->endpointParams); }