From 4d069bedc461a73e80fc76c60a92055efc00f81e Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:03:41 +0700 Subject: [PATCH 1/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=D0=BE=20=D1=81=D1=82=D1=80=D0=BE=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- composer.lock | 498 +++++++++++++++++++++++++------------------------- 2 files changed, 251 insertions(+), 250 deletions(-) diff --git a/composer.json b/composer.json index 0060158..9ada841 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "ext-json": "*", "jms/serializer": "^3.13", "symfony/validator": "^5.3", - "symfony/cache": "^5.3" + "symfony/cache": "^5.3", + "symfony/string": "^5.3" }, "require-dev": { "codeception/codeception": "^4.1", diff --git a/composer.lock b/composer.lock index 9d83692..ef82352 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ebb07a3c4ca215e5d28ff500c581294b", + "content-hash": "72e1ad26da8a0e774d935c357e8229cc", "packages": [ { "name": "doctrine/annotations", @@ -901,6 +901,171 @@ ], "time": "2021-02-19T12:13:01+00:00" }, + { + "name": "symfony/polyfill-intl-grapheme", + "version": "v1.23.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-grapheme.git", + "reference": "24b72c6baa32c746a4d0840147c9715e42bb68ab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/24b72c6baa32c746a4d0840147c9715e42bb68ab", + "reference": "24b72c6baa32c746a4d0840147c9715e42bb68ab", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.23-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Intl\\Grapheme\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's grapheme_* functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "grapheme", + "intl", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.23.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-05-27T09:17:38+00:00" + }, + { + "name": "symfony/polyfill-intl-normalizer", + "version": "v1.23.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-normalizer.git", + "reference": "8590a5f561694770bdcd3f9b5c69dde6945028e8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/8590a5f561694770bdcd3f9b5c69dde6945028e8", + "reference": "8590a5f561694770bdcd3f9b5c69dde6945028e8", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.23-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Intl\\Normalizer\\": "" + }, + "files": [ + "bootstrap.php" + ], + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's Normalizer class and related functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "intl", + "normalizer", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.23.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-02-19T12:13:01+00:00" + }, { "name": "symfony/polyfill-mbstring", "version": "v1.23.0", @@ -1222,6 +1387,89 @@ ], "time": "2021-04-01T10:43:52+00:00" }, + { + "name": "symfony/string", + "version": "v5.3.3", + "source": { + "type": "git", + "url": "https://github.com/symfony/string.git", + "reference": "bd53358e3eccec6a670b5f33ab680d8dbe1d4ae1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/string/zipball/bd53358e3eccec6a670b5f33ab680d8dbe1d4ae1", + "reference": "bd53358e3eccec6a670b5f33ab680d8dbe1d4ae1", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-intl-grapheme": "~1.0", + "symfony/polyfill-intl-normalizer": "~1.0", + "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php80": "~1.15" + }, + "require-dev": { + "symfony/error-handler": "^4.4|^5.0", + "symfony/http-client": "^4.4|^5.0", + "symfony/translation-contracts": "^1.1|^2", + "symfony/var-exporter": "^4.4|^5.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\String\\": "" + }, + "files": [ + "Resources/functions.php" + ], + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way", + "homepage": "https://symfony.com", + "keywords": [ + "grapheme", + "i18n", + "string", + "unicode", + "utf-8", + "utf8" + ], + "support": { + "source": "https://github.com/symfony/string/tree/v5.3.3" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-06-27T11:44:38+00:00" + }, { "name": "symfony/translation-contracts", "version": "v2.4.0", @@ -5092,171 +5340,6 @@ ], "time": "2021-06-30T08:27:49+00:00" }, - { - "name": "symfony/polyfill-intl-grapheme", - "version": "v1.23.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-intl-grapheme.git", - "reference": "24b72c6baa32c746a4d0840147c9715e42bb68ab" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/24b72c6baa32c746a4d0840147c9715e42bb68ab", - "reference": "24b72c6baa32c746a4d0840147c9715e42bb68ab", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "suggest": { - "ext-intl": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Intl\\Grapheme\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for intl's grapheme_* functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "grapheme", - "intl", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.23.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2021-05-27T09:17:38+00:00" - }, - { - "name": "symfony/polyfill-intl-normalizer", - "version": "v1.23.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-intl-normalizer.git", - "reference": "8590a5f561694770bdcd3f9b5c69dde6945028e8" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/8590a5f561694770bdcd3f9b5c69dde6945028e8", - "reference": "8590a5f561694770bdcd3f9b5c69dde6945028e8", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "suggest": { - "ext-intl": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Intl\\Normalizer\\": "" - }, - "files": [ - "bootstrap.php" - ], - "classmap": [ - "Resources/stubs" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for intl's Normalizer class and related functions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "intl", - "normalizer", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.23.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2021-02-19T12:13:01+00:00" - }, { "name": "symfony/routing", "version": "v5.3.0", @@ -5347,89 +5430,6 @@ ], "time": "2021-05-26T17:43:10+00:00" }, - { - "name": "symfony/string", - "version": "v5.3.3", - "source": { - "type": "git", - "url": "https://github.com/symfony/string.git", - "reference": "bd53358e3eccec6a670b5f33ab680d8dbe1d4ae1" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/bd53358e3eccec6a670b5f33ab680d8dbe1d4ae1", - "reference": "bd53358e3eccec6a670b5f33ab680d8dbe1d4ae1", - "shasum": "" - }, - "require": { - "php": ">=7.2.5", - "symfony/polyfill-ctype": "~1.8", - "symfony/polyfill-intl-grapheme": "~1.0", - "symfony/polyfill-intl-normalizer": "~1.0", - "symfony/polyfill-mbstring": "~1.0", - "symfony/polyfill-php80": "~1.15" - }, - "require-dev": { - "symfony/error-handler": "^4.4|^5.0", - "symfony/http-client": "^4.4|^5.0", - "symfony/translation-contracts": "^1.1|^2", - "symfony/var-exporter": "^4.4|^5.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "Symfony\\Component\\String\\": "" - }, - "files": [ - "Resources/functions.php" - ], - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way", - "homepage": "https://symfony.com", - "keywords": [ - "grapheme", - "i18n", - "string", - "unicode", - "utf-8", - "utf8" - ], - "support": { - "source": "https://github.com/symfony/string/tree/v5.3.3" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2021-06-27T11:44:38+00:00" - }, { "name": "symfony/var-dumper", "version": "v5.3.3", From 8d5640cf4e4636c2639eacf7d4084cc7da2c361c Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:04:58 +0700 Subject: [PATCH 2/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D1=81=20=D0=B0=D1=82=D1=80=D0=B8=D0=B1=D1=83=D1=82=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 ++- composer.lock | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 9ada841..013f927 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "jms/serializer": "^3.13", "symfony/validator": "^5.3", "symfony/cache": "^5.3", - "symfony/string": "^5.3" + "symfony/string": "^5.3", + "rinsvent/attribute-extractor": "^0.0.2" }, "require-dev": { "codeception/codeception": "^4.1", diff --git a/composer.lock b/composer.lock index ef82352..0da6926 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "72e1ad26da8a0e774d935c357e8229cc", + "content-hash": "6f29c92495f5e5045ad725f6a287b947", "packages": [ { "name": "doctrine/annotations", @@ -580,6 +580,45 @@ }, "time": "2021-05-03T11:20:27+00:00" }, + { + "name": "rinsvent/attribute-extractor", + "version": "v0.0.2", + "source": { + "type": "git", + "url": "https://github.com/Rinsvent/attribute-extractor.git", + "reference": "081b9f18c2fa4305e8acbe63fff873ae59928c9d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Rinsvent/attribute-extractor/zipball/081b9f18c2fa4305e8acbe63fff873ae59928c9d", + "reference": "081b9f18c2fa4305e8acbe63fff873ae59928c9d", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "codeception/codeception": "^4.1", + "codeception/module-asserts": "^1.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "tests\\": "tests/", + "Rinsvent\\AttributeExtractor\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHP 8 attribute extractor", + "support": { + "issues": "https://github.com/Rinsvent/attribute-extractor/issues", + "source": "https://github.com/Rinsvent/attribute-extractor/tree/v0.0.2" + }, + "time": "2021-07-28T13:37:17+00:00" + }, { "name": "symfony/cache", "version": "v5.3.3", From 14746a88bccc515870c6a235a43cfcefa4356f16 Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:07:43 +0700 Subject: [PATCH 3/8] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= =?UTF-8?q?=20=D1=81=20=D0=B0=D1=82=D1=80=D0=B8=D0=B1=D1=83=D1=82=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EventListener/RequestListener.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index da15a33..5ef6a32 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -3,12 +3,12 @@ namespace Rinsvent\RequestBundle\EventListener; use JMS\Serializer\SerializerBuilder; -use ReflectionMethod; use ReflectionObject; use Rinsvent\RequestBundle\Annotation\RequestDTO; use Rinsvent\RequestBundle\Annotation\HeaderKey; use Rinsvent\RequestBundle\DTO\Error; use Rinsvent\RequestBundle\DTO\ErrorCollection; +use Rinsvent\AttributeExtractor\MethodExtractor; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; @@ -23,20 +23,16 @@ class RequestListener $controller = $request->get('_controller'); if (is_string($controller)) { $controller = explode('::', $controller); - $method = new ReflectionMethod($controller[0], $controller[1]); } if (is_callable($controller)){ - $method = new ReflectionMethod($controller[0], $controller[1]); - } - if (!isset($method)) { - return; + if (is_object($controller[0])) { + $controller[0] = get_class($controller[0]); + } + $methodExtractor = new MethodExtractor($controller[0], $controller[1]); } - $attributes = $method->getAttributes(RequestDTO::class); - $attribute = $attributes[0] ?? null; - if ($attribute) { - /** @var RequestDTO $requestDTO */ - $requestDTO = $attribute->newInstance(); + /** @var RequestDTO $requestDTO */ + if ($requestDTO = $methodExtractor->fetch(RequestDTO::class)) { $requestDTOInstance = $this->grabRequestDTO($requestDTO->className, $request->getContent(), $request->query->all(), $request->request->all(), $request->headers->all()); $errorCollection = $this->validate($requestDTOInstance); From 76aa1dda483794a4a59f4c56b9cf6e26714a1482 Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:08:17 +0700 Subject: [PATCH 4/8] =?UTF-8?q?=D0=9F=D0=BE=D1=87=D0=B8=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/Listener/FillTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/Listener/FillTest.php b/tests/unit/Listener/FillTest.php index 02ef8e6..f646670 100644 --- a/tests/unit/Listener/FillTest.php +++ b/tests/unit/Listener/FillTest.php @@ -3,6 +3,7 @@ namespace Rinsvent\RequestBundle\Tests\Listener; use Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest\Controller; +use Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest\HelloRequest; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -41,7 +42,7 @@ class GenerateTest extends \Codeception\Test\Unit ]); $response = $this->send($request); - $this->assertEquals(3, $request->get('form')); + $this->assertEquals('Surname', $request->get(HelloRequest::class)->surname); } private function send(Request $request): Response From 778c8bbee0b9336ac9d755052316298247ca635f Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:16:31 +0700 Subject: [PATCH 5/8] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= =?UTF-8?q?=20=D1=81=20=D0=B0=D1=82=D1=80=D0=B8=D0=B1=D1=83=D1=82=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EventListener/RequestListener.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index 22d4b18..5e500e4 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -4,6 +4,7 @@ namespace Rinsvent\RequestBundle\EventListener; use JMS\Serializer\SerializerBuilder; use ReflectionObject; +use Rinsvent\AttributeExtractor\PropertyExtractor; use Rinsvent\RequestBundle\Annotation\RequestDTO; use Rinsvent\RequestBundle\Annotation\HeaderKey; use Rinsvent\RequestBundle\DTO\Error; @@ -88,11 +89,9 @@ class RequestListener $reflectionObject = new ReflectionObject($object); $properties = $reflectionObject->getProperties(); foreach ($properties as $property) { - $attributes = $property->getAttributes(HeaderKey::class); - $attribute = $attributes[0] ?? null; - if ($attribute) { - /** @var HeaderKey $headerKey */ - $headerKey = $attribute->newInstance(); + $propertyExtractor = new PropertyExtractor($object::class, $property->getName()); + /** @var HeaderKey $headerKey */ + if ($headerKey = $propertyExtractor->fetch(HeaderKey::class)) { $value = $data[strtolower($headerKey->key)][0] ?? null; } else { $value = $data[$property->getName()] ?? null; From 4151075d8947efca0c14420c9fb0346993bea29a Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:16:52 +0700 Subject: [PATCH 6/8] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= =?UTF-8?q?=20=D1=81=20=D0=B0=D1=82=D1=80=D0=B8=D0=B1=D1=83=D1=82=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EventListener/RequestListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index 5e500e4..cd602fc 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -27,7 +27,7 @@ class RequestListener if (is_string($controller)) { $controller = explode('::', $controller); } - if (is_callable($controller)){ + if (is_callable($controller)) { if (is_object($controller[0])) { $controller[0] = get_class($controller[0]); } From 8f266d18ebfc1031d79cb0f3a4ad82f95fbb6a8d Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:27:16 +0700 Subject: [PATCH 7/8] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{HeaderKey.php => PropertyPath.php} | 4 +- src/EventListener/RequestListener.php | 8 ++-- tests/_support/Helper/Unit.php | 46 ++++++++++++++----- tests/unit/Listener/FillTest.php | 25 +--------- 4 files changed, 41 insertions(+), 42 deletions(-) rename src/Annotation/{HeaderKey.php => PropertyPath.php} (69%) diff --git a/src/Annotation/HeaderKey.php b/src/Annotation/PropertyPath.php similarity index 69% rename from src/Annotation/HeaderKey.php rename to src/Annotation/PropertyPath.php index 45eda63..288cbb3 100644 --- a/src/Annotation/HeaderKey.php +++ b/src/Annotation/PropertyPath.php @@ -3,9 +3,9 @@ namespace Rinsvent\RequestBundle\Annotation; #[\Attribute] -class HeaderKey +class PropertyPath { public function __construct( - public string $key + public string $path ) {} } \ No newline at end of file diff --git a/src/EventListener/RequestListener.php b/src/EventListener/RequestListener.php index cd602fc..3b86b85 100644 --- a/src/EventListener/RequestListener.php +++ b/src/EventListener/RequestListener.php @@ -5,8 +5,8 @@ namespace Rinsvent\RequestBundle\EventListener; use JMS\Serializer\SerializerBuilder; use ReflectionObject; use Rinsvent\AttributeExtractor\PropertyExtractor; +use Rinsvent\RequestBundle\Annotation\PropertyPath; use Rinsvent\RequestBundle\Annotation\RequestDTO; -use Rinsvent\RequestBundle\Annotation\HeaderKey; use Rinsvent\RequestBundle\DTO\Error; use Rinsvent\RequestBundle\DTO\ErrorCollection; use Rinsvent\AttributeExtractor\MethodExtractor; @@ -90,9 +90,9 @@ class RequestListener $properties = $reflectionObject->getProperties(); foreach ($properties as $property) { $propertyExtractor = new PropertyExtractor($object::class, $property->getName()); - /** @var HeaderKey $headerKey */ - if ($headerKey = $propertyExtractor->fetch(HeaderKey::class)) { - $value = $data[strtolower($headerKey->key)][0] ?? null; + /** @var PropertyPath $propertyPath */ + if ($propertyPath = $propertyExtractor->fetch(PropertyPath::class)) { + $value = $data[strtolower($propertyPath->path)][0] ?? null; } else { $value = $data[$property->getName()] ?? null; } diff --git a/tests/_support/Helper/Unit.php b/tests/_support/Helper/Unit.php index 9b77d42..74dce91 100644 --- a/tests/_support/Helper/Unit.php +++ b/tests/_support/Helper/Unit.php @@ -4,21 +4,43 @@ namespace Rinsvent\RequestBundle\Tests\Helper; // here you can define custom actions // all public methods declared in helper class will be available in $I -use Rinsvent\ApiSDKGenerator\DTO\Writer\Config; -use Rinsvent\ApiSDKGenerator\Service\Writer; +use Rinsvent\RequestBundle\EventListener\RequestListener; +use Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest\Controller; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Controller\ArgumentResolver; +use Symfony\Component\HttpKernel\Controller\ControllerResolver; +use Symfony\Component\HttpKernel\EventListener\RouterListener; +use Symfony\Component\HttpKernel\HttpKernel; +use Symfony\Component\Routing\Matcher\UrlMatcher; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\RouteCollection; class Unit extends \Codeception\Module { - public function getWriter(string $lang = 'php'): Writer + public function send(Request $request): Response { - return new Writer( - new Config( - dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates', - dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'var/tests/cache', - $lang, - dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'var/tests/result', - 'Rinsvent\\AuthSDK' - ) - ); + $routes = new RouteCollection(); + $controller = new Controller(); + $routes->add('hello', new Route('/hello/{name}', [ + '_controller' => [$controller, 'hello'] + ] + )); + + $matcher = new UrlMatcher($routes, new RequestContext()); + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber(new RouterListener($matcher, new RequestStack())); + $listener = new RequestListener(); + $dispatcher->addListener('kernel.request', [$listener, 'onKernelRequest']); + + $controllerResolver = new ControllerResolver(); + $argumentResolver = new ArgumentResolver(); + $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver); + $response = $kernel->handle($request); + $response->send(); + return $response; } } diff --git a/tests/unit/Listener/FillTest.php b/tests/unit/Listener/FillTest.php index 085c04d..1651e54 100644 --- a/tests/unit/Listener/FillTest.php +++ b/tests/unit/Listener/FillTest.php @@ -40,31 +40,8 @@ class FillTest extends \Codeception\Test\Unit $request = Request::create('/hello/igor', 'GET', [ 'surname' => 'Surname' ]); - $response = $this->send($request); + $response = $this->tester->send($request); $this->assertEquals('Surname', $request->get(RequestListener::REQUEST_DATA)->surname); } - - private function send(Request $request): Response - { - $routes = new RouteCollection(); - $controller = new Controller(); - $routes->add('hello', new Route('/hello/{name}', [ - '_controller' => [$controller, 'hello'] - ] - )); - - $matcher = new UrlMatcher($routes, new RequestContext()); - $dispatcher = new EventDispatcher(); - $dispatcher->addSubscriber(new RouterListener($matcher, new RequestStack())); - $listener = new RequestListener(); - $dispatcher->addListener('kernel.request', [$listener, 'onKernelRequest']); - - $controllerResolver = new ControllerResolver(); - $argumentResolver = new ArgumentResolver(); - $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver); - $response = $kernel->handle($request); - $response->send(); - return $response; - } } From ab6b26abddb445237954bdf6acf1dd3eb329c07f Mon Sep 17 00:00:00 2001 From: Rinsvent Date: Wed, 28 Jul 2021 23:37:58 +0700 Subject: [PATCH 8/8] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=BE=D0=BF=20=D0=BF=D0=BE=D0=BB=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B5=D1=81=D0=BF=D0=BE=D0=BD=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unit/Listener/FillTest.php | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tests/unit/Listener/FillTest.php b/tests/unit/Listener/FillTest.php index 1651e54..6d8fa63 100644 --- a/tests/unit/Listener/FillTest.php +++ b/tests/unit/Listener/FillTest.php @@ -2,20 +2,7 @@ namespace Rinsvent\RequestBundle\Tests\Listener; -use Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest\Controller; -use Rinsvent\RequestBundle\Tests\unit\Listener\fixtures\FillTest\HelloRequest; -use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Controller\ArgumentResolver; -use Symfony\Component\HttpKernel\Controller\ControllerResolver; -use Symfony\Component\HttpKernel\EventListener\RouterListener; -use Symfony\Component\HttpKernel\HttpKernel; -use Symfony\Component\Routing\Matcher\UrlMatcher; -use Symfony\Component\Routing\RequestContext; -use Symfony\Component\Routing\Route; -use Symfony\Component\Routing\RouteCollection; use Rinsvent\RequestBundle\EventListener\RequestListener; @@ -42,6 +29,20 @@ class FillTest extends \Codeception\Test\Unit ]); $response = $this->tester->send($request); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('Surname', $request->get(RequestListener::REQUEST_DATA)->surname); + $this->assertEquals('Hello igor', $response->getContent()); + + } + + public function testFailRequestData() + { + $request = Request::create('/hello/igor', 'GET', [ + 'surname' => '' + ]); + $response = $this->tester->send($request); + + $this->assertEquals(400, $response->getStatusCode()); + $this->assertEquals('{"errors":[{"message":"This value should not be blank.","path":"surname"}]}', $response->getContent()); } }