From 77d2de4e3df51f346a75514301faab65cd4794b3 Mon Sep 17 00:00:00 2001 From: Arthur Mogliev Date: Wed, 30 Jan 2019 11:36:00 +0400 Subject: [PATCH] [PHP][php-ze-ph] Support for PHP 7.1+, Zend Expressive 3.2 and PathHander 0.4 (#1902) * - support for PHP 7.1, Zend Expressive 3.2 and PathHander 0.4 for php-ze-ph generator * - fixed mess with petstore samples (added new files, removed obsolete files) * php-ze-ph: - overwriting "*/*" media type for producers with "n/a" (PathHandler does not support that cause it makes no sense to return response with "Content-Type: */*") - "array" return type declaration for handler methods with ambiguous "container" return type - better way to generate attribute annotation stub for request body data with ambiguous "container" type - fixed missing dependency in composer.json - minor optimization for container.php - samples for OAS3 petstore spec * php-ze-ph: - note about ext-yaml in stub README - updated .gitignore * php-ze-ph: - logging '*/*' replacement as warning --- ...server.sh => php-ze-ph-petstore-server.sh} | 8 +- bin/php-ze-ph-petstore-server.sh | 6 +- ...endExpressivePathHandlerServerCodegen.java | 40 +++--- .../src/main/resources/ze-ph/.gitignore | 23 +--- .../main/resources/ze-ph/Date.php.mustache | 4 +- .../resources/ze-ph/DateTime.php.mustache | 7 +- .../ze-ph/ErrorMiddleware.php.mustache | 34 ----- .../main/resources/ze-ph/Factory.php.mustache | 58 ++++++++ .../ze-ph/InternalServerError.php.mustache | 54 ++++++++ .../ze-ph/QueryParameter.php.mustache | 3 +- .../ze-ph/QueryParameterArray.php.mustache | 13 +- .../QueryParameterArrayType.php.mustache | 30 +++-- .../ze-ph/QueryParameterType.php.mustache | 14 +- .../main/resources/ze-ph/README.md.mustache | 12 +- .../main/resources/ze-ph/Type.php.mustache | 24 ++-- .../src/main/resources/ze-ph/api.mustache | 17 ++- .../src/main/resources/ze-ph/app.yml.mustache | 29 ++-- .../resources/ze-ph/composer.json.mustache | 20 +-- .../src/main/resources/ze-ph/config.yml | 40 +++--- .../src/main/resources/ze-ph/container.php | 14 +- .../ze-ph/data_transfer.yml.mustache | 3 +- .../src/main/resources/ze-ph/index.php | 1 + .../src/main/resources/ze-ph/model.mustache | 1 + .../resources/ze-ph/path_handler.yml.mustache | 33 ++--- .../openapi3/petstore/php-ze-ph/.gitignore | 9 ++ .../php-ze-ph/.openapi-generator-ignore | 23 ++++ .../php-ze-ph/.openapi-generator/VERSION | 1 + .../openapi3/petstore/php-ze-ph/README.md | 34 +++++ .../petstore/php-ze-ph/application/config.yml | 39 ++++++ .../php-ze-ph/application/config/app.yml | 23 ++++ .../application/config/data_transfer.yml | 33 +++++ .../application/config/path_handler.yml | 71 ++++++++++ .../php-ze-ph/application/container.php | 46 +++++++ .../openapi3/petstore/php-ze-ph/composer.json | 25 ++++ .../petstore/php-ze-ph/public/index.php | 11 ++ .../src/App/DTO/AdditionalPropertiesClass.php | 31 +++++ .../petstore/php-ze-ph/src/App/DTO/Animal.php | 24 ++++ .../php-ze-ph/src/App/DTO/ApiResponse.php | 30 +++++ .../src/App/DTO/ArrayOfArrayOfNumberOnly.php | 22 +++ .../src/App/DTO/ArrayOfNumberOnly.php | 21 +++ .../php-ze-ph/src/App/DTO/ArrayTest.php | 41 ++++++ .../php-ze-ph/src/App/DTO/Capitalization.php | 49 +++++++ .../petstore/php-ze-ph/src/App/DTO/Cat.php | 30 +++++ .../php-ze-ph/src/App/DTO/Category.php | 24 ++++ .../php-ze-ph/src/App/DTO/ClassModel.php | 19 +++ .../petstore/php-ze-ph/src/App/DTO/Client.php | 18 +++ .../petstore/php-ze-ph/src/App/DTO/Dog.php | 30 +++++ .../php-ze-ph/src/App/DTO/EnumArrays.php | 27 ++++ .../php-ze-ph/src/App/DTO/EnumClass.php} | 3 +- .../php-ze-ph/src/App/DTO/EnumTest.php | 43 ++++++ .../src/App/DTO/FileSchemaTestClass.php | 29 ++++ .../src/App/DTO/FindPetsByStatusQueryData.php | 1 + .../src/App/DTO/FindPetsByTagsQueryData.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Foo.php | 18 +++ .../php-ze-ph/src/App/DTO/FormatTest.php | 121 +++++++++++++++++ .../php-ze-ph/src/App/DTO/HasOnlyReadOnly.php | 24 ++++ .../php-ze-ph/src/App/DTO/InlineObject.php | 26 ++++ .../php-ze-ph/src/App/DTO/InlineObject1.php | 27 ++++ .../php-ze-ph/src/App/DTO/InlineObject2.php | 29 ++++ .../php-ze-ph/src/App/DTO/InlineObject3.php | 125 ++++++++++++++++++ .../php-ze-ph/src/App/DTO/InlineObject4.php | 26 ++++ .../php-ze-ph/src/App/DTO/InlineObject5.php | 27 ++++ .../src/App/DTO/InlineResponseDefault.php | 19 +++ .../src/App/DTO/LoginUserQueryData.php | 1 + .../php-ze-ph/src/App/DTO/MapTest.php | 49 +++++++ ...PropertiesAndAdditionalPropertiesClass.php | 35 +++++ .../src/App/DTO/Model200Response.php | 25 ++++ .../php-ze-ph/src/App/DTO/ModelReturn.php | 19 +++ .../petstore/php-ze-ph/src/App/DTO/Name.php | 37 ++++++ .../php-ze-ph/src/App/DTO/NumberOnly.php | 18 +++ .../petstore/php-ze-ph/src/App/DTO/Order.php | 50 +++++++ .../php-ze-ph/src/App/DTO/OuterComposite.php | 30 +++++ .../php-ze-ph/src/App/DTO/OuterEnum.php} | 3 +- .../petstore/php-ze-ph/src/App/DTO/Pet.php | 57 ++++++++ .../php-ze-ph/src/App/DTO/ReadOnlyFirst.php | 24 ++++ .../src/App/DTO/SpecialModelName.php | 18 +++ .../petstore/php-ze-ph/src/App/DTO/Tag.php | 24 ++++ .../DTO/TestBodyWithQueryParamsQueryData.php | 20 +++ .../App/DTO/TestEnumParametersQueryData.php | 13 +- .../App/DTO/TestGroupParametersQueryData.php | 45 +++++++ .../petstore/php-ze-ph/src/App/DTO/User.php | 61 +++++++++ .../petstore/php-ze-ph/src/App/Factory.php | 58 ++++++++ .../src/App/Handler/AnotherFakeDummy.php | 39 ++++++ .../php-ze-ph/src/App/Handler/Fake.php | 89 +++++++++++++ .../App/Handler/FakeBodyWithFileSchema.php | 34 +++++ .../App/Handler/FakeBodyWithQueryParams.php | 41 ++++++ .../src/App/Handler/FakeClassnameTest.php | 39 ++++++ .../FakeInlineAdditionalProperties.php | 36 +++++ .../src/App/Handler/FakeJsonFormData.php | 30 +++++ .../src/App/Handler/FakeOuterBoolean.php | 35 +++++ .../src/App/Handler/FakeOuterComposite.php | 38 ++++++ .../src/App/Handler/FakeOuterNumber.php | 35 +++++ .../src/App/Handler/FakeOuterString.php | 35 +++++ .../FakePetIdUploadImageWithRequiredFile.php | 34 +++++ .../php-ze-ph/src/App/Handler/Foo.php | 33 +++++ .../php-ze-ph/src/App/Handler/Pet.php | 56 ++++++++ .../src/App/Handler/PetFindByStatus.php | 44 ++++++ .../src/App/Handler/PetFindByTags.php | 44 ++++++ .../php-ze-ph/src/App/Handler/PetPetId.php | 60 +++++++++ .../src/App/Handler/PetPetIdUploadImage.php} | 13 +- .../src/App/Handler/StoreInventory.php | 35 +++++ .../php-ze-ph/src/App/Handler/StoreOrder.php | 41 ++++++ .../src/App/Handler/StoreOrderOrderId.php | 48 +++++++ .../php-ze-ph/src/App/Handler/User.php | 35 +++++ .../src/App/Handler/UserCreateWithArray.php | 36 +++++ .../src/App/Handler/UserCreateWithList.php | 36 +++++ .../php-ze-ph/src/App/Handler/UserLogin.php | 43 ++++++ .../php-ze-ph/src/App/Handler/UserLogout.php | 30 +++++ .../src/App/Handler/UserUsername.php | 65 +++++++++ .../App/Middleware/InternalServerError.php | 54 ++++++++ .../php-ze-ph/src/App/Strategy/Date.php | 21 +++ .../php-ze-ph/src/App/Strategy/DateTime.php | 47 +++++++ .../src/App/Strategy/QueryParameter.php | 77 +++++++++++ .../src/App/Strategy/QueryParameterArray.php | 74 +++++++++++ .../App/Validator/QueryParameterArrayType.php | 74 +++++++++++ .../src/App/Validator/QueryParameterType.php | 29 ++++ .../php-ze-ph/src/App/Validator/Type.php | 73 ++++++++++ samples/server/petstore/php-ze-ph/.gitignore | 23 +--- samples/server/petstore/php-ze-ph/README.md | 12 +- .../petstore/php-ze-ph/application/config.yml | 40 +++--- .../php-ze-ph/application/config/app.yml | 29 ++-- .../application/config/data_transfer.yml | 3 +- .../application/config/path_handler.yml | 41 +++--- .../php-ze-ph/application/container.php | 14 +- .../server/petstore/php-ze-ph/composer.json | 20 +-- .../petstore/php-ze-ph/public/index.php | 1 + .../src/App/DTO/AdditionalPropertiesClass.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Animal.php | 1 + .../php-ze-ph/src/App/DTO/AnimalFarm.php | 11 -- .../php-ze-ph/src/App/DTO/ApiResponse.php | 1 + .../src/App/DTO/ArrayOfArrayOfNumberOnly.php | 1 + .../src/App/DTO/ArrayOfNumberOnly.php | 1 + .../php-ze-ph/src/App/DTO/ArrayTest.php | 1 + .../php-ze-ph/src/App/DTO/Capitalization.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Cat.php | 1 + .../php-ze-ph/src/App/DTO/Category.php | 1 + .../php-ze-ph/src/App/DTO/ClassModel.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Client.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Dog.php | 1 + .../php-ze-ph/src/App/DTO/EnumArrays.php | 1 + .../php-ze-ph/src/App/DTO/EnumClass.php | 1 + .../php-ze-ph/src/App/DTO/EnumTest.php | 1 + .../src/App/DTO/FileSchemaTestClass.php | 1 + .../php-ze-ph/src/App/DTO/FormatTest.php | 1 + .../php-ze-ph/src/App/DTO/HasOnlyReadOnly.php | 1 + .../php-ze-ph/src/App/DTO/MapTest.php | 1 + ...PropertiesAndAdditionalPropertiesClass.php | 1 + .../src/App/DTO/Model200Response.php | 1 + .../php-ze-ph/src/App/DTO/ModelReturn.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Name.php | 1 + .../php-ze-ph/src/App/DTO/NumberOnly.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Order.php | 1 + .../php-ze-ph/src/App/DTO/OuterBoolean.php | 11 -- .../php-ze-ph/src/App/DTO/OuterComposite.php | 1 + .../php-ze-ph/src/App/DTO/OuterEnum.php | 1 + .../petstore/php-ze-ph/src/App/DTO/Pet.php | 1 + .../php-ze-ph/src/App/DTO/ReadOnlyFirst.php | 1 + .../src/App/DTO/SpecialModelName.php | 1 + .../src/App/DTO/StringBooleanMap.php | 11 -- .../petstore/php-ze-ph/src/App/DTO/Tag.php | 1 + .../src/App/DTO/TypeHolderDefault.php | 1 + .../src/App/DTO/TypeHolderExample.php | 1 + .../petstore/php-ze-ph/src/App/DTO/User.php | 1 + .../php-ze-ph/src/App/ErrorMiddleware.php | 34 ----- .../petstore/php-ze-ph/src/App/Factory.php | 58 ++++++++ .../src/App/Handler/AnotherFakeDummy.php | 11 +- .../php-ze-ph/src/App/Handler/Fake.php | 32 +++-- .../App/Handler/FakeBodyWithFileSchema.php | 11 +- .../App/Handler/FakeBodyWithQueryParams.php | 11 +- .../src/App/Handler/FakeClassnameTest.php | 11 +- .../FakeInlineAdditionalProperties.php | 13 +- .../src/App/Handler/FakeJsonFormData.php | 11 +- .../src/App/Handler/FakeOuterBoolean.php | 13 +- .../src/App/Handler/FakeOuterComposite.php | 13 +- .../src/App/Handler/FakeOuterNumber.php | 13 +- .../src/App/Handler/FakeOuterString.php | 13 +- .../FakePetIdUploadImageWithRequiredFile.php | 11 +- .../php-ze-ph/src/App/Handler/Pet.php | 18 +-- .../src/App/Handler/PetFindByStatus.php | 12 +- .../src/App/Handler/PetFindByTags.php | 12 +- .../php-ze-ph/src/App/Handler/PetPetId.php | 25 ++-- .../src/App/Handler/PetPetIdUploadImage.php | 11 +- .../src/App/Handler/StoreInventory.php | 12 +- .../php-ze-ph/src/App/Handler/StoreOrder.php | 11 +- .../src/App/Handler/StoreOrderOrderId.php | 18 +-- .../php-ze-ph/src/App/Handler/User.php | 11 +- .../src/App/Handler/UserCreateWithArray.php | 13 +- .../src/App/Handler/UserCreateWithList.php | 13 +- .../php-ze-ph/src/App/Handler/UserLogin.php | 11 +- .../php-ze-ph/src/App/Handler/UserLogout.php | 11 +- .../src/App/Handler/UserUsername.php | 25 ++-- .../App/Middleware/InternalServerError.php | 54 ++++++++ .../php-ze-ph/src/App/Strategy/Date.php | 4 +- .../php-ze-ph/src/App/Strategy/DateTime.php | 7 +- .../src/App/Strategy/QueryParameter.php | 3 +- .../src/App/Strategy/QueryParameterArray.php | 13 +- .../App/Validator/QueryParameterArrayType.php | 30 +++-- .../src/App/Validator/QueryParameterType.php | 14 +- .../php-ze-ph/src/App/Validator/Type.php | 24 ++-- 199 files changed, 4098 insertions(+), 587 deletions(-) rename bin/openapi3/{ze-ph-petstore-server.sh => php-ze-ph-petstore-server.sh} (68%) mode change 100755 => 100644 delete mode 100644 modules/openapi-generator/src/main/resources/ze-ph/ErrorMiddleware.php.mustache create mode 100644 modules/openapi-generator/src/main/resources/ze-ph/Factory.php.mustache create mode 100644 modules/openapi-generator/src/main/resources/ze-ph/InternalServerError.php.mustache create mode 100644 samples/server/openapi3/petstore/php-ze-ph/.gitignore create mode 100644 samples/server/openapi3/petstore/php-ze-ph/.openapi-generator-ignore create mode 100644 samples/server/openapi3/petstore/php-ze-ph/.openapi-generator/VERSION create mode 100644 samples/server/openapi3/petstore/php-ze-ph/README.md create mode 100644 samples/server/openapi3/petstore/php-ze-ph/application/config.yml create mode 100644 samples/server/openapi3/petstore/php-ze-ph/application/config/app.yml create mode 100644 samples/server/openapi3/petstore/php-ze-ph/application/config/data_transfer.yml create mode 100644 samples/server/openapi3/petstore/php-ze-ph/application/config/path_handler.yml create mode 100644 samples/server/openapi3/petstore/php-ze-ph/application/container.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/composer.json create mode 100644 samples/server/openapi3/petstore/php-ze-ph/public/index.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/AdditionalPropertiesClass.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Animal.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ApiResponse.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ArrayOfArrayOfNumberOnly.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ArrayOfNumberOnly.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ArrayTest.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Capitalization.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Cat.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Category.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ClassModel.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Client.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Dog.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/EnumArrays.php rename samples/server/{petstore/php-ze-ph/src/App/DTO/OuterNumber.php => openapi3/petstore/php-ze-ph/src/App/DTO/EnumClass.php} (67%) create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/EnumTest.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/FileSchemaTestClass.php rename samples/server/{ => openapi3}/petstore/php-ze-ph/src/App/DTO/FindPetsByStatusQueryData.php (95%) rename samples/server/{ => openapi3}/petstore/php-ze-ph/src/App/DTO/FindPetsByTagsQueryData.php (95%) create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Foo.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/FormatTest.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/HasOnlyReadOnly.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject1.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject2.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject3.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject4.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject5.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineResponseDefault.php rename samples/server/{ => openapi3}/petstore/php-ze-ph/src/App/DTO/LoginUserQueryData.php (96%) create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/MapTest.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/MixedPropertiesAndAdditionalPropertiesClass.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Model200Response.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ModelReturn.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Name.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/NumberOnly.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Order.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/OuterComposite.php rename samples/server/{petstore/php-ze-ph/src/App/DTO/OuterString.php => openapi3/petstore/php-ze-ph/src/App/DTO/OuterEnum.php} (67%) create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Pet.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/ReadOnlyFirst.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/SpecialModelName.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/Tag.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/TestBodyWithQueryParamsQueryData.php rename samples/server/{ => openapi3}/petstore/php-ze-ph/src/App/DTO/TestEnumParametersQueryData.php (74%) create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/TestGroupParametersQueryData.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/User.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Factory.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/Fake.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterBoolean.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterComposite.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterString.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakePetIdUploadImageWithRequiredFile.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/Foo.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/Pet.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByTags.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetPetId.php rename samples/server/{petstore/php-ze-ph/src/App/Handler/PetPetIdUploadImageWithRequiredFile.php => openapi3/petstore/php-ze-ph/src/App/Handler/PetPetIdUploadImage.php} (65%) create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/StoreInventory.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/StoreOrder.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/User.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogin.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogout.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserUsername.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/Date.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/DateTime.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameter.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php create mode 100644 samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/Type.php delete mode 100644 samples/server/petstore/php-ze-ph/src/App/DTO/AnimalFarm.php delete mode 100644 samples/server/petstore/php-ze-ph/src/App/DTO/OuterBoolean.php delete mode 100644 samples/server/petstore/php-ze-ph/src/App/DTO/StringBooleanMap.php delete mode 100644 samples/server/petstore/php-ze-ph/src/App/ErrorMiddleware.php create mode 100644 samples/server/petstore/php-ze-ph/src/App/Factory.php create mode 100644 samples/server/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php diff --git a/bin/openapi3/ze-ph-petstore-server.sh b/bin/openapi3/php-ze-ph-petstore-server.sh old mode 100755 new mode 100644 similarity index 68% rename from bin/openapi3/ze-ph-petstore-server.sh rename to bin/openapi3/php-ze-ph-petstore-server.sh index 6aef4530f07..fbb7f10dfec --- a/bin/openapi3/ze-ph-petstore-server.sh +++ b/bin/openapi3/php-ze-ph-petstore-server.sh @@ -22,11 +22,15 @@ executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar" if [ ! -f "$executable" ] then - mvn clean package + mvn -B clean package fi +input=modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml +out_folder=samples/server/openapi3/petstore/php-ze-ph +resources=modules/openapi-generator/src/main/resources/ze-ph + # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/ze-ph -i modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml -g php-ze-ph -o samples/server/petstore/ze-ph $@" +ags="generate -t $resources -i $input -g php-ze-ph -o $out_folder $@" java $JAVA_OPTS -jar $executable $ags diff --git a/bin/php-ze-ph-petstore-server.sh b/bin/php-ze-ph-petstore-server.sh index 577e94b0fa1..3ee4621ed7e 100755 --- a/bin/php-ze-ph-petstore-server.sh +++ b/bin/php-ze-ph-petstore-server.sh @@ -25,8 +25,12 @@ then mvn -B clean package fi +input=modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +out_folder=samples/server/petstore/php-ze-ph +resources=modules/openapi-generator/src/main/resources/ze-ph + # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/ze-ph -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g php-ze-ph -o samples/server/petstore/php-ze-ph $@" +ags="generate -t $resources -i $input -g php-ze-ph -o $out_folder $@" java $JAVA_OPTS -jar $executable $ags diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpZendExpressivePathHandlerServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpZendExpressivePathHandlerServerCodegen.java index f8b5a5c4bce..d68c59755df 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpZendExpressivePathHandlerServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpZendExpressivePathHandlerServerCodegen.java @@ -26,11 +26,14 @@ import io.swagger.v3.oas.models.parameters.Parameter; import io.swagger.v3.oas.models.parameters.QueryParameter; import org.openapitools.codegen.*; import org.openapitools.codegen.utils.ModelUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.util.*; public class PhpZendExpressivePathHandlerServerCodegen extends AbstractPhpCodegen { + private static final Logger LOGGER = LoggerFactory.getLogger(PhpZendExpressivePathHandlerServerCodegen.class); public static final String VEN_FROM_QUERY = "internal.ze-ph.fromQuery"; public static final String VEN_COLLECTION_FORMAT = "internal.ze-ph.collectionFormat"; @@ -78,7 +81,8 @@ public class PhpZendExpressivePathHandlerServerCodegen extends AbstractPhpCodege supportingFiles.add(new SupportingFile("app.yml.mustache", "application" + File.separator + "config", "app.yml")); supportingFiles.add(new SupportingFile("path_handler.yml.mustache", "application" + File.separator + "config", "path_handler.yml")); supportingFiles.add(new SupportingFile("data_transfer.yml.mustache", "application" + File.separator + "config", "data_transfer.yml")); - supportingFiles.add(new SupportingFile("ErrorMiddleware.php.mustache", srcBasePath, "ErrorMiddleware.php")); + supportingFiles.add(new SupportingFile("Factory.php.mustache", srcBasePath, "Factory.php")); + supportingFiles.add(new SupportingFile("InternalServerError.php.mustache", srcBasePath + File.separator + "Middleware", "InternalServerError.php")); supportingFiles.add(new SupportingFile("Date.php.mustache", srcBasePath + File.separator + "Strategy", "Date.php")); supportingFiles.add(new SupportingFile("DateTime.php.mustache", srcBasePath + File.separator + "Strategy", "DateTime.php")); supportingFiles.add(new SupportingFile("QueryParameter.php.mustache", srcBasePath + File.separator + "Strategy", "QueryParameter.php")); @@ -291,46 +295,42 @@ public class PhpZendExpressivePathHandlerServerCodegen extends AbstractPhpCodege objs = super.postProcessOperationsWithModels(objs, allModels); Map operations = (Map) objs.get("operations"); List operationList = (List) operations.get("operation"); - String interfaceToImplement; - StringBuilder interfacesToImplement = new StringBuilder(); - String classMethod; + String httpMethodDeclaration; String pathPattern = null; for (CodegenOperation op : operationList) { switch (op.httpMethod) { case "GET": - interfaceToImplement = "Operation\\GetInterface"; - classMethod = "handleGet"; + httpMethodDeclaration = "Get()"; break; case "POST": - interfaceToImplement = "Operation\\PostInterface"; - classMethod = "handlePost"; + httpMethodDeclaration = "Post()"; break; case "PATCH": - interfaceToImplement = "Operation\\PatchInterface"; - classMethod = "handlePatch"; + httpMethodDeclaration = "Patch()"; break; case "PUT": - interfaceToImplement = "Operation\\PutInterface"; - classMethod = "handlePut"; + httpMethodDeclaration = "Put()"; break; case "DELETE": - interfaceToImplement = "Operation\\DeleteInterface"; - classMethod = "handleDelete"; + httpMethodDeclaration = "Delete()"; break; default: - throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed"); + httpMethodDeclaration = "HttpMethod(\"" + op.httpMethod + "\")"; } - if (interfacesToImplement.length() > 0) { - interfacesToImplement.append(", "); + op.httpMethod = httpMethodDeclaration; + //Producing content with media type "*/*" is not supported + if (op.produces != null) { + for (Map p: op.produces) { + if (p.replace("mediaType", "*/*", "n/a")) { + LOGGER.warn("Media type range '*/*' is not supported, using 'n/a' for code generation instead"); + } + } } - interfacesToImplement.append(interfaceToImplement); - op.httpMethod = classMethod; //All operations have same path because of custom operation grouping, so path pattern can be calculated only once if (pathPattern == null) { pathPattern = generatePathPattern(op); } } - operations.put("interfacesToImplement", interfacesToImplement.toString()); operations.put("pathPattern", pathPattern); return objs; diff --git a/modules/openapi-generator/src/main/resources/ze-ph/.gitignore b/modules/openapi-generator/src/main/resources/ze-ph/.gitignore index be88ab49679..1ddcdf2a709 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/.gitignore +++ b/modules/openapi-generator/src/main/resources/ze-ph/.gitignore @@ -1,26 +1,9 @@ -# ref: https://github.com/github/gitignore/blob/master/ZendFramework.gitignore +#based on .gitignore generated by https://github.com/zendframework/zend-expressive-skeleton +.idea # Composer files composer.phar vendor/ -# Local configs -config/autoload/*.local.php - -# Binary gettext files -*.mo - -# Data -data/logs/ +# Default cache folder data/cache/ -data/sessions/ -data/tmp/ -temp/ - -#Doctrine 2 -data/DoctrineORMModule/Proxy/ -data/DoctrineORMModule/cache/ - -# Legacy ZF1 -demos/ -extras/documentation \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/ze-ph/Date.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/Date.php.mustache index 4edbca0486b..0c06196db3b 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/Date.php.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/Date.php.mustache @@ -1,4 +1,5 @@ process($request); - ErrorHandler::stop(true); - if (!($result instanceof Response)) { - throw new \RuntimeException(sprintf( - 'Invalid response: expecting %s, got %s', - Response::class, - is_object($result)? get_class($result) : gettype($result) - )); - } - } - catch (\Exception $error) { - $result = (new \Zend\Diactoros\Response())->withStatus(500, 'Internal server error'); - error_log((string)$error); - } - return $result; - } -} diff --git a/modules/openapi-generator/src/main/resources/ze-ph/Factory.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/Factory.php.mustache new file mode 100644 index 00000000000..0edc4a175ac --- /dev/null +++ b/modules/openapi-generator/src/main/resources/ze-ph/Factory.php.mustache @@ -0,0 +1,58 @@ +get(Middleware\InternalServerError::class); + if (!($errorMiddleware instanceof Middleware\InternalServerError)) { + throw new \LogicException(\sprintf( + 'Invalid error middleware: expecting %s, not %s.', + Middleware\InternalServerError::class, + \is_object($errorMiddleware) ? \get_class($errorMiddleware) : \gettype($errorMiddleware) + )); + } + $pipeline = new MiddlewarePipe(); + $runner = new RequestHandlerRunner( + $pipeline, + $container->get(EmitterInterface::class), + $container->get(ServerRequestInterface::class), + function(\Throwable $error) use ($errorMiddleware) : ResponseInterface + { + return $errorMiddleware->handleError($error); + } + ); + $application = new Application( + $container->get(MiddlewareFactory::class), + $pipeline, + $container->get(RouteCollector::class), + $runner + ); + $application->pipe($errorMiddleware); + $application->pipe(RouteMiddleware::class); + $application->pipe(MethodNotAllowedMiddleware::class); + $application->pipe(DispatchMiddleware::class); + $application->pipe(NotFoundHandler::class); + + return $application; + } +} diff --git a/modules/openapi-generator/src/main/resources/ze-ph/InternalServerError.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/InternalServerError.php.mustache new file mode 100644 index 00000000000..a13f14f871e --- /dev/null +++ b/modules/openapi-generator/src/main/resources/ze-ph/InternalServerError.php.mustache @@ -0,0 +1,54 @@ +responseGenerator = $responseGenerator; + } + + /** + * @inheritdoc + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $result = null; + try { + ErrorHandler::start(); + $result = $handler->handle($request); + ErrorHandler::stop(true); + } + catch (\Throwable $error) { + $result = $this->handleError($error); + } + return $result; + } + + public function handleError(\Throwable $error): ResponseInterface + { + \error_log((string)$error); + return $this->generateEmptyResponse()->withStatus(500, 'Internal server error'); + } + + protected function generateEmptyResponse(): ResponseInterface + { + return ($this->responseGenerator)(); + } +} diff --git a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameter.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameter.php.mustache index 1de1ca9e376..a9a25841c58 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameter.php.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameter.php.mustache @@ -1,4 +1,5 @@ type = $options['type']; } diff --git a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArray.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArray.php.mustache index 3504fb328ee..3df9aa3291f 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArray.php.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArray.php.mustache @@ -1,4 +1,5 @@ delimiter = self::DELIMITER_MAP[$options['format']]; } @@ -40,11 +41,11 @@ class QueryParameterArray extends QueryParameter public function extract($objectValue, $object = null) { $result = null; - if (is_array($objectValue)) { + if (\is_array($objectValue)) { if ($this->delimiter === null) { $result = $objectValue; } else { - $result = implode($this->delimiter, $objectValue); + $result = \implode($this->delimiter, $objectValue); } } return $result; @@ -59,9 +60,9 @@ class QueryParameterArray extends QueryParameter if ($arrayValue !== null) { $list = null; if ($this->delimiter === null) { - $list = (is_array($arrayValue))? $arrayValue : [$arrayValue]; + $list = (\is_array($arrayValue))? $arrayValue : [$arrayValue]; } else { - $list = explode($this->delimiter, $arrayValue); + $list = \explode($this->delimiter, $arrayValue); } $result = []; foreach ($list as $item) { diff --git a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArrayType.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArrayType.php.mustache index 728e0d616e6..b818360496e 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArrayType.php.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterArrayType.php.mustache @@ -1,4 +1,6 @@ format; } @@ -22,21 +24,21 @@ class QueryParameterArrayType extends QueryParameterType * @param string $format * @return self */ - public function setFormat($format) + public function setFormat(string $format): self { $this->format = $format; return $this; } - protected function checkType($value) + protected function checkType($value): bool { $result = true; - if (!array_key_exists($this->format, QueryParameterArray::DELIMITER_MAP)) { - throw new \InvalidArgumentException(sprintf('Can not check for format %s.', $this->format)); + if (!\array_key_exists($this->format, QueryParameterArray::DELIMITER_MAP)) { + throw new \InvalidArgumentException(\sprintf('Can not check for format %s.', $this->format)); } $delimiter = QueryParameterArray::DELIMITER_MAP[$this->format]; if ($delimiter === null) { - if (is_array($value)) { + if (\is_array($value)) { foreach ($value as $item) { $result = $result && parent::checkType($item); } @@ -46,27 +48,27 @@ class QueryParameterArrayType extends QueryParameterType } else { switch ($this->type) { case QueryParameterArray::TYPE_INT: - $result = is_string($value) && preg_match(self::prepareRepeatingTypeRegExp(self::RE_INT, $delimiter), $value); + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_INT, $delimiter), $value); break; case QueryParameterArray::TYPE_BOOL: - $result = is_string($value) && preg_match(self::prepareRepeatingTypeRegExp(self::RE_BOOL, $delimiter), $value); + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_BOOL, $delimiter), $value); break; case QueryParameterArray::TYPE_FLOAT: - $result = is_string($value) && preg_match(self::prepareRepeatingTypeRegExp(self::RE_FLOAT, $delimiter), $value); + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_FLOAT, $delimiter), $value); break; case QueryParameterArray::TYPE_STRING: - $result = is_string($value); + $result = \is_string($value); break; default: - throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type)); + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); } } return $result; } - protected static function prepareRepeatingTypeRegExp($typeRegExp, $delimiter) + protected static function prepareRepeatingTypeRegExp(string $typeRegExp, string $delimiter): string { - $escapedDelimiter = preg_quote($delimiter, '/'); + $escapedDelimiter = \preg_quote($delimiter, '/'); return '/^(' . $typeRegExp . ')(' . $escapedDelimiter . '('. $typeRegExp . '))*$/'; } } diff --git a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterType.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterType.php.mustache index 1393018d42a..98b59bcec16 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterType.php.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/QueryParameterType.php.mustache @@ -1,4 +1,6 @@ type) { case QueryParameter::TYPE_INT: - return is_string($value) && preg_match('/^(' . self::RE_INT . ')$/', $value); + return \is_string($value) && \preg_match('/^(' . self::RE_INT . ')$/', $value); case QueryParameter::TYPE_BOOL: - return is_string($value) && preg_match('/^(' . self::RE_BOOL . ')$/', $value); + return \is_string($value) && \preg_match('/^(' . self::RE_BOOL . ')$/', $value); case QueryParameter::TYPE_FLOAT: - return is_string($value) && preg_match('/^(' . self::RE_FLOAT . ')$/', $value); + return \is_string($value) && \preg_match('/^(' . self::RE_FLOAT . ')$/', $value); case QueryParameter::TYPE_STRING: - return is_string($value); + return \is_string($value); default: - throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type)); + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); } } } diff --git a/modules/openapi-generator/src/main/resources/ze-ph/README.md.mustache b/modules/openapi-generator/src/main/resources/ze-ph/README.md.mustache index 48a355ac7d2..973240bb44e 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/README.md.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/README.md.mustache @@ -5,14 +5,14 @@ Generated by the [OpenAPI Generator](https://openapi-generator.tech) project. ## Overview This server stub aims to provide light, yet comprehensive structure for your API project using: -- PHP: 5.6 or 7.* -- [Zend Expressive](https://zendframework.github.io/zend-expressive): 2.1 -- [Path Handler](https://github.com/Articus/PathHandler): 0.3 +- PHP: >=7.1 +- [Zend Expressive](https://zendframework.github.io/zend-expressive): >=3.2 +- [Path Handler](https://github.com/Articus/PathHandler): >=0.4 ## How to use All you have to do to start development is: -- install dependencies via [Composer](https://getcomposer.org/) +- install dependencies via [Composer](https://getcomposer.org/) (small note: [ext-yaml](https://pecl.php.net/package/yaml) is used only for configuration parsing, so if you want to drop this dependency, simply adjust `./application/container.php`) - create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/config.yml`) - start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`) @@ -24,11 +24,11 @@ After that you should be able to call all methods from your API spec. Most of th - `400 Malformed JSON` for unparsable JSON body - `422 Unprocessable entity` for parsable JSON body that fails validation -But for obvious reason you will not get any `200 OK`, only `500 Not implemented`. So your next steps are: +But for obvious reason you will not get any `200 OK`, only `501 Not implemented`. So your next steps are: - check all TODOs left in the stub code where generator was not smart enough and could not guarantee correct implementation - implement your API security mechanism (either special attribute or separate middleware) - generator does not do anything about it yet - implement your handlers - the most tricky part :) ## Enjoy! -Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [OpenAPI Generator repository](https://openapi-generator.tech/issues). +Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `php-ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [OpenAPI Generator repository](https://openapi-generator.tech/issues). diff --git a/modules/openapi-generator/src/main/resources/ze-ph/Type.php.mustache b/modules/openapi-generator/src/main/resources/ze-ph/Type.php.mustache index 812da6cb80b..6c72a6a00d0 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/Type.php.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/Type.php.mustache @@ -1,4 +1,6 @@ type; } @@ -34,14 +36,14 @@ class Type extends AbstractValidator * @param string $type * @return self */ - public function setType($type) + public function setType(string $type): self { $this->type = $type; return $this; } /** - * @inheritDoc + * @inheritdoc */ public function isValid($value) { @@ -53,19 +55,19 @@ class Type extends AbstractValidator return $result; } - protected function checkType($value) + protected function checkType($value): bool { switch ($this->type) { case 'int': - return is_int($value); + return \is_int($value); case 'bool': - return is_bool($value); + return \is_bool($value); case 'float': - return is_float($value) || is_int($value); + return \is_float($value) || \is_int($value); case 'string': - return is_string($value); + return \is_string($value); default: - throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type)); + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); } } } diff --git a/modules/openapi-generator/src/main/resources/ze-ph/api.mustache b/modules/openapi-generator/src/main/resources/ze-ph/api.mustache index 5c8081932e1..82f3782ab9b 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/api.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/api.mustache @@ -1,8 +1,8 @@ getAttribute("bodyData"); {{/isPrimitiveType}} {{/bodyParam}} - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } {{/operation}} } diff --git a/modules/openapi-generator/src/main/resources/ze-ph/app.yml.mustache b/modules/openapi-generator/src/main/resources/ze-ph/app.yml.mustache index 735c2ef1f68..8d8bac997f1 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/app.yml.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/app.yml.mustache @@ -1,14 +1,23 @@ dependencies: invokables: - Zend\Diactoros\Response\EmitterInterface: Zend\Diactoros\Response\SapiStreamEmitter - App\ErrorMiddleware: App\ErrorMiddleware + Zend\HttpHandlerRunner\Emitter\EmitterInterface: Zend\HttpHandlerRunner\Emitter\SapiStreamEmitter factories: - Zend\Expressive\Application: Zend\Expressive\Container\ApplicationFactory - Articus\PathHandler\Middleware: Articus\PathHandler\MiddlewareFactory + Zend\Expressive\Application: App\Factory + Zend\Expressive\MiddlewareContainer: Zend\Expressive\Container\MiddlewareContainerFactory + Zend\Expressive\MiddlewareFactory: Zend\Expressive\Container\MiddlewareFactoryFactory + Zend\Expressive\Router\RouteCollector: Zend\Expressive\Router\RouteCollectorFactory + Zend\Expressive\Router\RouterInterface: Articus\PathHandler\RouteInjection\Factory -middleware_pipeline: - error: - middleware: App\ErrorMiddleware - api: - middleware: Articus\PathHandler\Middleware - path: {{basePathWithoutHost}} + Psr\Http\Message\ServerRequestInterface: Zend\Expressive\Container\ServerRequestFactoryFactory + Psr\Http\Message\StreamInterface: Zend\Expressive\Container\StreamFactoryFactory + Psr\Http\Message\ResponseInterface: Zend\Expressive\Container\ResponseFactoryFactory + + Zend\Expressive\Router\Middleware\RouteMiddleware: Zend\Expressive\Router\Middleware\RouteMiddlewareFactory + Zend\Expressive\Router\Middleware\DispatchMiddleware: Zend\Expressive\Router\Middleware\DispatchMiddlewareFactory + Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware: Zend\Expressive\Router\Middleware\MethodNotAllowedMiddlewareFactory + Zend\Expressive\Handler\NotFoundHandler: Zend\Expressive\Container\NotFoundHandlerFactory + App\Middleware\InternalServerError: Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory + +Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory: + App\Middleware\InternalServerError: + - Psr\Http\Message\ResponseInterface diff --git a/modules/openapi-generator/src/main/resources/ze-ph/composer.json.mustache b/modules/openapi-generator/src/main/resources/ze-ph/composer.json.mustache index 5cab5ca6e2d..411ea721d1d 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/composer.json.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/composer.json.mustache @@ -5,15 +5,17 @@ "version": "{{artifactVersion}}", "type": "project", "require": { - "php": "^5.6 || ^7.0", - "ext-yaml" : "^1.2 || ^2.0", - "zendframework/zend-expressive": "^2.1", - "articus/path-handler": "^0.3", - "articus/data-transfer": "^0.1", - "zendframework/zend-serializer": "^2.8", - "zendframework/zend-config": "^3.1", - "nikic/fast-route": "^1.2", - "http-interop/http-middleware": "^0.4.1" + "php": "^7.1", + "ext-yaml": "^2.0", + "zendframework/zend-expressive": "^3.2", + "zendframework/zend-diactoros": "^2.1", + "articus/path-handler": "^0.4", + "articus/data-transfer": "^0.2", + "doctrine/annotations": "^1.6", + "zendframework/zend-cache": "^2.8", + "zendframework/zend-serializer": "^2.9", + "zendframework/zend-config": "^3.2", + "nikic/fast-route": "^1.3" }, "autoload": { "psr-4": { diff --git a/modules/openapi-generator/src/main/resources/ze-ph/config.yml b/modules/openapi-generator/src/main/resources/ze-ph/config.yml index f0f92e1589d..4eae86f67b3 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/config.yml +++ b/modules/openapi-generator/src/main/resources/ze-ph/config.yml @@ -4,38 +4,36 @@ #Enable configuration cache #cache_configuration: true -#Enable routing cache for handlers -#Articus\PathHandler\Router\FastRouteAnnotation: -# metadata_cache: -# adapter: -# name: filesystem +#Articus\PathHandler\RouteInjection\Factory: +# #Enable routing table cache +# router: +# cache: +# adapter: filesystem # options: # cache_dir: data/cache/ZendCache -# namespace: fast-route -# plugins: -# serializer: -# serializer: phpserialize - -#Enable consumer, attribute and producer cache for handlers -#Articus\PathHandler\Middleware: -# metadata_cache: -# adapter: -# name: filesystem +# namespace: ph-router +# plugins: +# serializer: +# serializer: phpserialize +# #Enable handler metadata cache +# metadata: +# cache: +# adapter: filesystem # options: # cache_dir: data/cache/ZendCache -# namespace: path-handler -# plugins: -# serializer: -# serializer: phpserialize +# namespace: ph-metadata +# plugins: +# serializer: +# serializer: phpserialize #Enable data transfer metadata cache for DTOs -#data_transfer: +#Articus\DataTransfer\Service: # metadata_cache: # adapter: # name: filesystem # options: # cache_dir: data/cache/ZendCache -# namespace: data-transfer +# namespace: dt # plugins: # serializer: # serializer: phpserialize diff --git a/modules/openapi-generator/src/main/resources/ze-ph/container.php b/modules/openapi-generator/src/main/resources/ze-ph/container.php index e70a0663cf3..3518792765f 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/container.php +++ b/modules/openapi-generator/src/main/resources/ze-ph/container.php @@ -1,9 +1,14 @@ setService('config', $config); diff --git a/modules/openapi-generator/src/main/resources/ze-ph/data_transfer.yml.mustache b/modules/openapi-generator/src/main/resources/ze-ph/data_transfer.yml.mustache index d5f734f5868..f6766802ac8 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/data_transfer.yml.mustache +++ b/modules/openapi-generator/src/main/resources/ze-ph/data_transfer.yml.mustache @@ -2,7 +2,7 @@ dependencies: factories: Articus\DataTransfer\Service: Articus\DataTransfer\ServiceFactory -data_transfer: +Articus\DataTransfer\Service: metadata_cache: adapter: name: blackhole @@ -12,7 +12,6 @@ data_transfer: {{invokerPackage}}\Strategy\DateTime: {{invokerPackage}}\Strategy\DateTime {{invokerPackage}}\Strategy\QueryParameter: {{invokerPackage}}\Strategy\QueryParameter {{invokerPackage}}\Strategy\QueryParameterArray: {{invokerPackage}}\Strategy\QueryParameterArray -# factories: aliases: Date: {{invokerPackage}}\Strategy\Date DateTime: {{invokerPackage}}\Strategy\DateTime diff --git a/modules/openapi-generator/src/main/resources/ze-ph/index.php b/modules/openapi-generator/src/main/resources/ze-ph/index.php index fcb6dd63852..2f84d8776b0 100644 --- a/modules/openapi-generator/src/main/resources/ze-ph/index.php +++ b/modules/openapi-generator/src/main/resources/ze-ph/index.php @@ -1,4 +1,5 @@ =7.1 +- [Zend Expressive](https://zendframework.github.io/zend-expressive): >=3.2 +- [Path Handler](https://github.com/Articus/PathHandler): >=0.4 + +## How to use +All you have to do to start development is: + +- install dependencies via [Composer](https://getcomposer.org/) (small note: [ext-yaml](https://pecl.php.net/package/yaml) is used only for configuration parsing, so if you want to drop this dependency, simply adjust `./application/container.php`) +- create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/config.yml`) +- start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`) + +After that you should be able to call all methods from your API spec. Most of the negative scenarios should be handled: + +- `404 Not found` for unknown routes +- `406 Not acceptable` for invalid `Accept` header +- `415 Unsupported media type` for invalid `Content-Type` header +- `400 Malformed JSON` for unparsable JSON body +- `422 Unprocessable entity` for parsable JSON body that fails validation + +But for obvious reason you will not get any `200 OK`, only `501 Not implemented`. So your next steps are: + +- check all TODOs left in the stub code where generator was not smart enough and could not guarantee correct implementation +- implement your API security mechanism (either special attribute or separate middleware) - generator does not do anything about it yet +- implement your handlers - the most tricky part :) + +## Enjoy! +Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `php-ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [OpenAPI Generator repository](https://openapi-generator.tech/issues). diff --git a/samples/server/openapi3/petstore/php-ze-ph/application/config.yml b/samples/server/openapi3/petstore/php-ze-ph/application/config.yml new file mode 100644 index 00000000000..4eae86f67b3 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/application/config.yml @@ -0,0 +1,39 @@ +#Empty configuration placeholder, remove when you add any real configuration settings to this file +{} + +#Enable configuration cache +#cache_configuration: true + +#Articus\PathHandler\RouteInjection\Factory: +# #Enable routing table cache +# router: +# cache: +# adapter: filesystem +# options: +# cache_dir: data/cache/ZendCache +# namespace: ph-router +# plugins: +# serializer: +# serializer: phpserialize +# #Enable handler metadata cache +# metadata: +# cache: +# adapter: filesystem +# options: +# cache_dir: data/cache/ZendCache +# namespace: ph-metadata +# plugins: +# serializer: +# serializer: phpserialize + +#Enable data transfer metadata cache for DTOs +#Articus\DataTransfer\Service: +# metadata_cache: +# adapter: +# name: filesystem +# options: +# cache_dir: data/cache/ZendCache +# namespace: dt +# plugins: +# serializer: +# serializer: phpserialize diff --git a/samples/server/openapi3/petstore/php-ze-ph/application/config/app.yml b/samples/server/openapi3/petstore/php-ze-ph/application/config/app.yml new file mode 100644 index 00000000000..8d8bac997f1 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/application/config/app.yml @@ -0,0 +1,23 @@ +dependencies: + invokables: + Zend\HttpHandlerRunner\Emitter\EmitterInterface: Zend\HttpHandlerRunner\Emitter\SapiStreamEmitter + factories: + Zend\Expressive\Application: App\Factory + Zend\Expressive\MiddlewareContainer: Zend\Expressive\Container\MiddlewareContainerFactory + Zend\Expressive\MiddlewareFactory: Zend\Expressive\Container\MiddlewareFactoryFactory + Zend\Expressive\Router\RouteCollector: Zend\Expressive\Router\RouteCollectorFactory + Zend\Expressive\Router\RouterInterface: Articus\PathHandler\RouteInjection\Factory + + Psr\Http\Message\ServerRequestInterface: Zend\Expressive\Container\ServerRequestFactoryFactory + Psr\Http\Message\StreamInterface: Zend\Expressive\Container\StreamFactoryFactory + Psr\Http\Message\ResponseInterface: Zend\Expressive\Container\ResponseFactoryFactory + + Zend\Expressive\Router\Middleware\RouteMiddleware: Zend\Expressive\Router\Middleware\RouteMiddlewareFactory + Zend\Expressive\Router\Middleware\DispatchMiddleware: Zend\Expressive\Router\Middleware\DispatchMiddlewareFactory + Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware: Zend\Expressive\Router\Middleware\MethodNotAllowedMiddlewareFactory + Zend\Expressive\Handler\NotFoundHandler: Zend\Expressive\Container\NotFoundHandlerFactory + App\Middleware\InternalServerError: Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory + +Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory: + App\Middleware\InternalServerError: + - Psr\Http\Message\ResponseInterface diff --git a/samples/server/openapi3/petstore/php-ze-ph/application/config/data_transfer.yml b/samples/server/openapi3/petstore/php-ze-ph/application/config/data_transfer.yml new file mode 100644 index 00000000000..7ed712ca9f1 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/application/config/data_transfer.yml @@ -0,0 +1,33 @@ +dependencies: + factories: + Articus\DataTransfer\Service: Articus\DataTransfer\ServiceFactory + +Articus\DataTransfer\Service: + metadata_cache: + adapter: + name: blackhole + strategies: + invokables: + App\Strategy\Date: App\Strategy\Date + App\Strategy\DateTime: App\Strategy\DateTime + App\Strategy\QueryParameter: App\Strategy\QueryParameter + App\Strategy\QueryParameterArray: App\Strategy\QueryParameterArray + aliases: + Date: App\Strategy\Date + DateTime: App\Strategy\DateTime + QueryParameter: App\Strategy\QueryParameter + QueryParameterArray: App\Strategy\QueryParameterArray + validators: + invokables: + App\Validator\Type: App\Validator\Type + App\Validator\QueryParameterType: App\Validator\QueryParameterType + App\Validator\QueryParameterArrayType: App\Validator\QueryParameterArrayType + factories: + Articus\DataTransfer\Validator\Dictionary: Articus\DataTransfer\Validator\Factory + Articus\DataTransfer\Validator\Collection: Articus\DataTransfer\Validator\Factory + aliases: + Dictionary: Articus\DataTransfer\Validator\Dictionary + Collection: Articus\DataTransfer\Validator\Collection + Type: App\Validator\Type + QueryParameterType: App\Validator\QueryParameterType + QueryParameterArrayType: App\Validator\QueryParameterArrayType diff --git a/samples/server/openapi3/petstore/php-ze-ph/application/config/path_handler.yml b/samples/server/openapi3/petstore/php-ze-ph/application/config/path_handler.yml new file mode 100644 index 00000000000..9ff5964b0eb --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/application/config/path_handler.yml @@ -0,0 +1,71 @@ +Articus\PathHandler\RouteInjection\Factory: + paths: + '/v2': + - App\Handler\AnotherFakeDummy + - App\Handler\Fake + - App\Handler\FakeBodyWithFileSchema + - App\Handler\FakeBodyWithQueryParams + - App\Handler\FakeInlineAdditionalProperties + - App\Handler\FakeJsonFormData + - App\Handler\FakeOuterBoolean + - App\Handler\FakeOuterComposite + - App\Handler\FakeOuterNumber + - App\Handler\FakeOuterString + - App\Handler\FakePetIdUploadImageWithRequiredFile + - App\Handler\FakeClassnameTest + - App\Handler\Foo + - App\Handler\Pet + - App\Handler\PetFindByStatus + - App\Handler\PetFindByTags + - App\Handler\PetPetId + - App\Handler\PetPetIdUploadImage + - App\Handler\StoreInventory + - App\Handler\StoreOrder + - App\Handler\StoreOrderOrderId + - App\Handler\User + - App\Handler\UserCreateWithArray + - App\Handler\UserCreateWithList + - App\Handler\UserLogin + - App\Handler\UserLogout + - App\Handler\UserUsername + handlers: + abstract_factories: + - Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory +# consumers: +# factories: +# invokables: +# attributes: +# factories: +# invokables: +# producers: +# factories: +# invokables: + +Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory: + App\Handler\AnotherFakeDummy: [] + App\Handler\Fake: [] + App\Handler\FakeBodyWithFileSchema: [] + App\Handler\FakeBodyWithQueryParams: [] + App\Handler\FakeInlineAdditionalProperties: [] + App\Handler\FakeJsonFormData: [] + App\Handler\FakeOuterBoolean: [] + App\Handler\FakeOuterComposite: [] + App\Handler\FakeOuterNumber: [] + App\Handler\FakeOuterString: [] + App\Handler\FakePetIdUploadImageWithRequiredFile: [] + App\Handler\FakeClassnameTest: [] + App\Handler\Foo: [] + App\Handler\Pet: [] + App\Handler\PetFindByStatus: [] + App\Handler\PetFindByTags: [] + App\Handler\PetPetId: [] + App\Handler\PetPetIdUploadImage: [] + App\Handler\StoreInventory: [] + App\Handler\StoreOrder: [] + App\Handler\StoreOrderOrderId: [] + App\Handler\User: [] + App\Handler\UserCreateWithArray: [] + App\Handler\UserCreateWithList: [] + App\Handler\UserLogin: [] + App\Handler\UserLogout: [] + App\Handler\UserUsername: [] diff --git a/samples/server/openapi3/petstore/php-ze-ph/application/container.php b/samples/server/openapi3/petstore/php-ze-ph/application/container.php new file mode 100644 index 00000000000..3518792765f --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/application/container.php @@ -0,0 +1,46 @@ +setService('config', $config); +$container->setAlias('Config', 'config'); + +return $container; diff --git a/samples/server/openapi3/petstore/php-ze-ph/composer.json b/samples/server/openapi3/petstore/php-ze-ph/composer.json new file mode 100644 index 00000000000..7496b7dfda3 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/composer.json @@ -0,0 +1,25 @@ +{ + "name": "GIT_USER_ID/GIT_REPO_ID", + "description": "", + "license": "proprietary", + "version": "1.0.0", + "type": "project", + "require": { + "php": "^7.1", + "ext-yaml": "^2.0", + "zendframework/zend-expressive": "^3.2", + "zendframework/zend-diactoros": "^2.1", + "articus/path-handler": "^0.4", + "articus/data-transfer": "^0.2", + "doctrine/annotations": "^1.6", + "zendframework/zend-cache": "^2.8", + "zendframework/zend-serializer": "^2.9", + "zendframework/zend-config": "^3.2", + "nikic/fast-route": "^1.3" + }, + "autoload": { + "psr-4": { + "": "src/" + } + } +} \ No newline at end of file diff --git a/samples/server/openapi3/petstore/php-ze-ph/public/index.php b/samples/server/openapi3/petstore/php-ze-ph/public/index.php new file mode 100644 index 00000000000..2f84d8776b0 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/public/index.php @@ -0,0 +1,11 @@ +get(\Zend\Expressive\Application::class); +$app->run(); diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/AdditionalPropertiesClass.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/AdditionalPropertiesClass.php new file mode 100644 index 00000000000..62b20fab02a --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/AdditionalPropertiesClass.php @@ -0,0 +1,31 @@ +get(Middleware\InternalServerError::class); + if (!($errorMiddleware instanceof Middleware\InternalServerError)) { + throw new \LogicException(\sprintf( + 'Invalid error middleware: expecting %s, not %s.', + Middleware\InternalServerError::class, + \is_object($errorMiddleware) ? \get_class($errorMiddleware) : \gettype($errorMiddleware) + )); + } + $pipeline = new MiddlewarePipe(); + $runner = new RequestHandlerRunner( + $pipeline, + $container->get(EmitterInterface::class), + $container->get(ServerRequestInterface::class), + function(\Throwable $error) use ($errorMiddleware) : ResponseInterface + { + return $errorMiddleware->handleError($error); + } + ); + $application = new Application( + $container->get(MiddlewareFactory::class), + $pipeline, + $container->get(RouteCollector::class), + $runner + ); + $application->pipe($errorMiddleware); + $application->pipe(RouteMiddleware::class); + $application->pipe(MethodNotAllowedMiddleware::class); + $application->pipe(DispatchMiddleware::class); + $application->pipe(NotFoundHandler::class); + + return $application; + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php new file mode 100644 index 00000000000..ee20ba08d24 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php @@ -0,0 +1,39 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/Fake.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/Fake.php new file mode 100644 index 00000000000..566f28ddff8 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/Fake.php @@ -0,0 +1,89 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } + /** + * Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 + * @PHA\Post() + * @param ServerRequestInterface $request + * + * @throws PHException\HttpCode 501 if the method is not implemented + */ + public function testEndpointParameters(ServerRequestInterface $request) + { + //TODO implement method + throw new PHException\HttpCode(501, "Not implemented"); + } + /** + * To test enum parameters + * @PHA\Get() + * @PHA\Attribute(name=PHAttribute\Transfer::class, options={ + * "type":\App\DTO\TestEnumParametersQueryData::class, + * "objectAttr":"queryData", + * "source": PHAttribute\Transfer::SOURCE_GET + * }) + * @param ServerRequestInterface $request + * + * @throws PHException\HttpCode 501 if the method is not implemented + */ + public function testEnumParameters(ServerRequestInterface $request) + { + //TODO implement method + /** @var \App\DTO\TestEnumParametersQueryData $queryData */ + $queryData = $request->getAttribute("queryData"); + throw new PHException\HttpCode(501, "Not implemented"); + } + /** + * Fake endpoint to test group parameters (optional) + * @PHA\Delete() + * @PHA\Attribute(name=PHAttribute\Transfer::class, options={ + * "type":\App\DTO\TestGroupParametersQueryData::class, + * "objectAttr":"queryData", + * "source": PHAttribute\Transfer::SOURCE_GET + * }) + * @param ServerRequestInterface $request + * + * @throws PHException\HttpCode 501 if the method is not implemented + */ + public function testGroupParameters(ServerRequestInterface $request) + { + //TODO implement method + /** @var \App\DTO\TestGroupParametersQueryData $queryData */ + $queryData = $request->getAttribute("queryData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php new file mode 100644 index 00000000000..47ce029df6e --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php @@ -0,0 +1,34 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php new file mode 100644 index 00000000000..facb461a64a --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php @@ -0,0 +1,41 @@ +getAttribute("queryData"); + /** @var \App\DTO\User $bodyData */ + $bodyData = $request->getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php new file mode 100644 index 00000000000..74991195dce --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php @@ -0,0 +1,39 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php new file mode 100644 index 00000000000..bb6868c68e3 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php @@ -0,0 +1,36 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php new file mode 100644 index 00000000000..ed3e0f6d2ef --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php @@ -0,0 +1,30 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php new file mode 100644 index 00000000000..a39bf98d488 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php @@ -0,0 +1,35 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } + /** + * Update an existing pet + * @PHA\Put() + * TODO check if consumer is valid, if it has correct priority and if it can be moved to class annotation + * @PHA\Consumer(name=PHConsumer\Json::class, mediaType="application/json") + * TODO check if consumer is valid, if it has correct priority and if it can be moved to class annotation + * @PHA\Consumer(name=PHConsumer\Json::class, mediaType="application/xml") + * @PHA\Attribute(name=PHAttribute\Transfer::class, options={"type":\App\DTO\Pet::class,"objectAttr":"bodyData"}) + * @param ServerRequestInterface $request + * + * @throws PHException\HttpCode 501 if the method is not implemented + */ + public function updatePet(ServerRequestInterface $request) + { + //TODO implement method + /** @var \App\DTO\Pet $bodyData */ + $bodyData = $request->getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php new file mode 100644 index 00000000000..c1c1ce75400 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php @@ -0,0 +1,44 @@ +getAttribute("queryData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByTags.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByTags.php new file mode 100644 index 00000000000..b722a310efb --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetFindByTags.php @@ -0,0 +1,44 @@ +getAttribute("queryData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetPetId.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetPetId.php new file mode 100644 index 00000000000..625bbf5832c --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/PetPetId.php @@ -0,0 +1,60 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php new file mode 100644 index 00000000000..28aa169a0c8 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php @@ -0,0 +1,48 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php new file mode 100644 index 00000000000..4a2204fb7c0 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php @@ -0,0 +1,36 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php new file mode 100644 index 00000000000..f61d9e4061e --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php @@ -0,0 +1,36 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogin.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogin.php new file mode 100644 index 00000000000..577e7c3f562 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogin.php @@ -0,0 +1,43 @@ +getAttribute("queryData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogout.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogout.php new file mode 100644 index 00000000000..4c0d8b1bb06 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Handler/UserLogout.php @@ -0,0 +1,30 @@ +getAttribute("bodyData"); + throw new PHException\HttpCode(501, "Not implemented"); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php new file mode 100644 index 00000000000..ba889f44ae2 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php @@ -0,0 +1,54 @@ +responseGenerator = $responseGenerator; + } + + /** + * @inheritdoc + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $result = null; + try { + ErrorHandler::start(); + $result = $handler->handle($request); + ErrorHandler::stop(true); + } + catch (\Throwable $error) { + $result = $this->handleError($error); + } + return $result; + } + + public function handleError(\Throwable $error): ResponseInterface + { + \error_log((string)$error); + return $this->generateEmptyResponse()->withStatus(500, 'Internal server error'); + } + + protected function generateEmptyResponse(): ResponseInterface + { + return ($this->responseGenerator)(); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/Date.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/Date.php new file mode 100644 index 00000000000..eedff2f01e2 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/Date.php @@ -0,0 +1,21 @@ +format(static::DATE_TIME_FORMAT); + } + return $result; + } + + /** + * @inheritdoc + */ + public function hydrate($arrayValue, $objectValue, array $array = null) + { + $result = null; + if (!empty($arrayValue)) { + $date = $this->parseDateString($arrayValue); + if ($date instanceof \DateTime) { + $result = $date; + } + } + return $result; + } + + /** + * @param $arrayValue + * @return bool|\DateTime + */ + protected function parseDateString($arrayValue) + { + return \DateTime::createFromFormat(static::DATE_TIME_FORMAT, $arrayValue, new \DateTimeZone('UTC')); + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameter.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameter.php new file mode 100644 index 00000000000..e9eac336a33 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameter.php @@ -0,0 +1,77 @@ + true, + self::TYPE_FLOAT => true, + self::TYPE_BOOL => true, + self::TYPE_STRING => true, + ]; + + /** + * @var string + */ + protected $type; + + /** + * QueryParameterArray constructor. + */ + public function __construct(array $options) + { + if (empty($options['type'])) { + throw new \InvalidArgumentException('Option "type" is required.'); + } elseif (!isset(self::TYPE_MAP[$options['type']])) { + throw new \InvalidArgumentException(\sprintf('Unknown type "%s".', $options['type'])); + } + $this->type = $options['type']; + } + + + /** + * @inheritdoc + */ + public function extract($objectValue, $object = null) + { + $result = null; + if ($objectValue !== null) { + $result = (string)$objectValue; + } + return $result; + } + + /** + * @inheritdoc + */ + public function hydrate($arrayValue, $objectValue, array $array = null) + { + $result = null; + if ($arrayValue !== null) { + switch ($this->type) { + case self::TYPE_INT: + $result = (int)$arrayValue; + break; + case self::TYPE_FLOAT: + $result = (float)$arrayValue; + break; + case self::TYPE_BOOL: + $result = ($arrayValue === 'true')? true : false; + break; + case self::TYPE_STRING: + $result = (string)$arrayValue; + break; + } + } + return $result; + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php new file mode 100644 index 00000000000..34f57d140fe --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php @@ -0,0 +1,74 @@ + ',', + self::FORMAT_SSV => ' ', + self::FORMAT_TSV => "\t", + self::FORMAT_PIPES => '|', + self::FORMAT_MULTI => null, + ]; + + /** + * @var string|null + */ + protected $delimiter; + + public function __construct(array $options) + { + parent::__construct($options); + if (empty($options['format'])) { + throw new \InvalidArgumentException('Option "format" is required.'); + } elseif (!\array_key_exists($options['format'], self::DELIMITER_MAP)) { + throw new \InvalidArgumentException(\sprintf('Unknown format "%s".', $options['format'])); + } + $this->delimiter = self::DELIMITER_MAP[$options['format']]; + } + + /** + * @inheritdoc + */ + public function extract($objectValue, $object = null) + { + $result = null; + if (\is_array($objectValue)) { + if ($this->delimiter === null) { + $result = $objectValue; + } else { + $result = \implode($this->delimiter, $objectValue); + } + } + return $result; + } + + /** + * @inheritdoc + */ + public function hydrate($arrayValue, $objectValue, array $array = null) + { + $result = null; + if ($arrayValue !== null) { + $list = null; + if ($this->delimiter === null) { + $list = (\is_array($arrayValue))? $arrayValue : [$arrayValue]; + } else { + $list = \explode($this->delimiter, $arrayValue); + } + $result = []; + foreach ($list as $item) { + $result[] = parent::hydrate($item, null); + } + } + return $result; + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php new file mode 100644 index 00000000000..069cfaa51a3 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php @@ -0,0 +1,74 @@ +format; + } + + /** + * @param string $format + * @return self + */ + public function setFormat(string $format): self + { + $this->format = $format; + return $this; + } + + protected function checkType($value): bool + { + $result = true; + if (!\array_key_exists($this->format, QueryParameterArray::DELIMITER_MAP)) { + throw new \InvalidArgumentException(\sprintf('Can not check for format %s.', $this->format)); + } + $delimiter = QueryParameterArray::DELIMITER_MAP[$this->format]; + if ($delimiter === null) { + if (\is_array($value)) { + foreach ($value as $item) { + $result = $result && parent::checkType($item); + } + } else { + $result = false; + } + } else { + switch ($this->type) { + case QueryParameterArray::TYPE_INT: + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_INT, $delimiter), $value); + break; + case QueryParameterArray::TYPE_BOOL: + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_BOOL, $delimiter), $value); + break; + case QueryParameterArray::TYPE_FLOAT: + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_FLOAT, $delimiter), $value); + break; + case QueryParameterArray::TYPE_STRING: + $result = \is_string($value); + break; + default: + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); + } + } + return $result; + } + + protected static function prepareRepeatingTypeRegExp(string $typeRegExp, string $delimiter): string + { + $escapedDelimiter = \preg_quote($delimiter, '/'); + return '/^(' . $typeRegExp . ')(' . $escapedDelimiter . '('. $typeRegExp . '))*$/'; + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php new file mode 100644 index 00000000000..6643990b744 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php @@ -0,0 +1,29 @@ +type) { + case QueryParameter::TYPE_INT: + return \is_string($value) && \preg_match('/^(' . self::RE_INT . ')$/', $value); + case QueryParameter::TYPE_BOOL: + return \is_string($value) && \preg_match('/^(' . self::RE_BOOL . ')$/', $value); + case QueryParameter::TYPE_FLOAT: + return \is_string($value) && \preg_match('/^(' . self::RE_FLOAT . ')$/', $value); + case QueryParameter::TYPE_STRING: + return \is_string($value); + default: + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); + } + } +} diff --git a/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/Type.php b/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/Type.php new file mode 100644 index 00000000000..a3b883d6488 --- /dev/null +++ b/samples/server/openapi3/petstore/php-ze-ph/src/App/Validator/Type.php @@ -0,0 +1,73 @@ + 'Invalid type given.', + ]; + + /** + * @var null|string + */ + protected $type; + + /** + * @return string + */ + public function getType(): ?string + { + return $this->type; + } + + /** + * @param string $type + * @return self + */ + public function setType(string $type): self + { + $this->type = $type; + return $this; + } + + /** + * @inheritdoc + */ + public function isValid($value) + { + $result = true; + if (!$this->checkType($value)) { + $this->error(self::INVALID); + $result = false; + } + return $result; + } + + protected function checkType($value): bool + { + switch ($this->type) { + case 'int': + return \is_int($value); + case 'bool': + return \is_bool($value); + case 'float': + return \is_float($value) || \is_int($value); + case 'string': + return \is_string($value); + default: + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); + } + } +} diff --git a/samples/server/petstore/php-ze-ph/.gitignore b/samples/server/petstore/php-ze-ph/.gitignore index be88ab49679..1ddcdf2a709 100644 --- a/samples/server/petstore/php-ze-ph/.gitignore +++ b/samples/server/petstore/php-ze-ph/.gitignore @@ -1,26 +1,9 @@ -# ref: https://github.com/github/gitignore/blob/master/ZendFramework.gitignore +#based on .gitignore generated by https://github.com/zendframework/zend-expressive-skeleton +.idea # Composer files composer.phar vendor/ -# Local configs -config/autoload/*.local.php - -# Binary gettext files -*.mo - -# Data -data/logs/ +# Default cache folder data/cache/ -data/sessions/ -data/tmp/ -temp/ - -#Doctrine 2 -data/DoctrineORMModule/Proxy/ -data/DoctrineORMModule/cache/ - -# Legacy ZF1 -demos/ -extras/documentation \ No newline at end of file diff --git a/samples/server/petstore/php-ze-ph/README.md b/samples/server/petstore/php-ze-ph/README.md index 48a355ac7d2..973240bb44e 100644 --- a/samples/server/petstore/php-ze-ph/README.md +++ b/samples/server/petstore/php-ze-ph/README.md @@ -5,14 +5,14 @@ Generated by the [OpenAPI Generator](https://openapi-generator.tech) project. ## Overview This server stub aims to provide light, yet comprehensive structure for your API project using: -- PHP: 5.6 or 7.* -- [Zend Expressive](https://zendframework.github.io/zend-expressive): 2.1 -- [Path Handler](https://github.com/Articus/PathHandler): 0.3 +- PHP: >=7.1 +- [Zend Expressive](https://zendframework.github.io/zend-expressive): >=3.2 +- [Path Handler](https://github.com/Articus/PathHandler): >=0.4 ## How to use All you have to do to start development is: -- install dependencies via [Composer](https://getcomposer.org/) +- install dependencies via [Composer](https://getcomposer.org/) (small note: [ext-yaml](https://pecl.php.net/package/yaml) is used only for configuration parsing, so if you want to drop this dependency, simply adjust `./application/container.php`) - create cache folder: `mkdir -p ./data/cache/ZendCache` (you will need it later for configuration and metadata caches - check comments in `./application/config.yml`) - start PHP development server: `php -S 0.0.0.0:8080 -t ./public` (or any other SAPI you prefer, just make sure that you configure webroot to `./public` and rewrites to `./public/index.php`) @@ -24,11 +24,11 @@ After that you should be able to call all methods from your API spec. Most of th - `400 Malformed JSON` for unparsable JSON body - `422 Unprocessable entity` for parsable JSON body that fails validation -But for obvious reason you will not get any `200 OK`, only `500 Not implemented`. So your next steps are: +But for obvious reason you will not get any `200 OK`, only `501 Not implemented`. So your next steps are: - check all TODOs left in the stub code where generator was not smart enough and could not guarantee correct implementation - implement your API security mechanism (either special attribute or separate middleware) - generator does not do anything about it yet - implement your handlers - the most tricky part :) ## Enjoy! -Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [OpenAPI Generator repository](https://openapi-generator.tech/issues). +Hopefully this stub will reduce the amount of boilerplate code you have to write manually. If you have any suggestions or questions about `php-ze-ph` generator, feel free to create issue either in [Path Handler repository](https://github.com/Articus/PathHandler/issues) or in [OpenAPI Generator repository](https://openapi-generator.tech/issues). diff --git a/samples/server/petstore/php-ze-ph/application/config.yml b/samples/server/petstore/php-ze-ph/application/config.yml index f0f92e1589d..4eae86f67b3 100644 --- a/samples/server/petstore/php-ze-ph/application/config.yml +++ b/samples/server/petstore/php-ze-ph/application/config.yml @@ -4,38 +4,36 @@ #Enable configuration cache #cache_configuration: true -#Enable routing cache for handlers -#Articus\PathHandler\Router\FastRouteAnnotation: -# metadata_cache: -# adapter: -# name: filesystem +#Articus\PathHandler\RouteInjection\Factory: +# #Enable routing table cache +# router: +# cache: +# adapter: filesystem # options: # cache_dir: data/cache/ZendCache -# namespace: fast-route -# plugins: -# serializer: -# serializer: phpserialize - -#Enable consumer, attribute and producer cache for handlers -#Articus\PathHandler\Middleware: -# metadata_cache: -# adapter: -# name: filesystem +# namespace: ph-router +# plugins: +# serializer: +# serializer: phpserialize +# #Enable handler metadata cache +# metadata: +# cache: +# adapter: filesystem # options: # cache_dir: data/cache/ZendCache -# namespace: path-handler -# plugins: -# serializer: -# serializer: phpserialize +# namespace: ph-metadata +# plugins: +# serializer: +# serializer: phpserialize #Enable data transfer metadata cache for DTOs -#data_transfer: +#Articus\DataTransfer\Service: # metadata_cache: # adapter: # name: filesystem # options: # cache_dir: data/cache/ZendCache -# namespace: data-transfer +# namespace: dt # plugins: # serializer: # serializer: phpserialize diff --git a/samples/server/petstore/php-ze-ph/application/config/app.yml b/samples/server/petstore/php-ze-ph/application/config/app.yml index a47d04b9bc4..8d8bac997f1 100644 --- a/samples/server/petstore/php-ze-ph/application/config/app.yml +++ b/samples/server/petstore/php-ze-ph/application/config/app.yml @@ -1,14 +1,23 @@ dependencies: invokables: - Zend\Diactoros\Response\EmitterInterface: Zend\Diactoros\Response\SapiStreamEmitter - App\ErrorMiddleware: App\ErrorMiddleware + Zend\HttpHandlerRunner\Emitter\EmitterInterface: Zend\HttpHandlerRunner\Emitter\SapiStreamEmitter factories: - Zend\Expressive\Application: Zend\Expressive\Container\ApplicationFactory - Articus\PathHandler\Middleware: Articus\PathHandler\MiddlewareFactory + Zend\Expressive\Application: App\Factory + Zend\Expressive\MiddlewareContainer: Zend\Expressive\Container\MiddlewareContainerFactory + Zend\Expressive\MiddlewareFactory: Zend\Expressive\Container\MiddlewareFactoryFactory + Zend\Expressive\Router\RouteCollector: Zend\Expressive\Router\RouteCollectorFactory + Zend\Expressive\Router\RouterInterface: Articus\PathHandler\RouteInjection\Factory -middleware_pipeline: - error: - middleware: App\ErrorMiddleware - api: - middleware: Articus\PathHandler\Middleware - path: /v2 + Psr\Http\Message\ServerRequestInterface: Zend\Expressive\Container\ServerRequestFactoryFactory + Psr\Http\Message\StreamInterface: Zend\Expressive\Container\StreamFactoryFactory + Psr\Http\Message\ResponseInterface: Zend\Expressive\Container\ResponseFactoryFactory + + Zend\Expressive\Router\Middleware\RouteMiddleware: Zend\Expressive\Router\Middleware\RouteMiddlewareFactory + Zend\Expressive\Router\Middleware\DispatchMiddleware: Zend\Expressive\Router\Middleware\DispatchMiddlewareFactory + Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware: Zend\Expressive\Router\Middleware\MethodNotAllowedMiddlewareFactory + Zend\Expressive\Handler\NotFoundHandler: Zend\Expressive\Container\NotFoundHandlerFactory + App\Middleware\InternalServerError: Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory + +Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory: + App\Middleware\InternalServerError: + - Psr\Http\Message\ResponseInterface diff --git a/samples/server/petstore/php-ze-ph/application/config/data_transfer.yml b/samples/server/petstore/php-ze-ph/application/config/data_transfer.yml index d6ee0e896a2..7ed712ca9f1 100644 --- a/samples/server/petstore/php-ze-ph/application/config/data_transfer.yml +++ b/samples/server/petstore/php-ze-ph/application/config/data_transfer.yml @@ -2,7 +2,7 @@ dependencies: factories: Articus\DataTransfer\Service: Articus\DataTransfer\ServiceFactory -data_transfer: +Articus\DataTransfer\Service: metadata_cache: adapter: name: blackhole @@ -12,7 +12,6 @@ data_transfer: App\Strategy\DateTime: App\Strategy\DateTime App\Strategy\QueryParameter: App\Strategy\QueryParameter App\Strategy\QueryParameterArray: App\Strategy\QueryParameterArray -# factories: aliases: Date: App\Strategy\Date DateTime: App\Strategy\DateTime diff --git a/samples/server/petstore/php-ze-ph/application/config/path_handler.yml b/samples/server/petstore/php-ze-ph/application/config/path_handler.yml index 7ac94df8539..33daffd998c 100644 --- a/samples/server/petstore/php-ze-ph/application/config/path_handler.yml +++ b/samples/server/petstore/php-ze-ph/application/config/path_handler.yml @@ -1,29 +1,6 @@ -dependencies: - factories: - Zend\Expressive\Router\RouterInterface: Articus\PathHandler\Router\Factory\FastRouteAnnotation - -Articus\PathHandler\Middleware: - metadata_cache: - adapter: - name: blackhole - handlers: - abstract_factories: - - Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory -# consumers: -# factories: -# invokables: -# attributes: -# factories: -# invokables: -# producers: -# factories: -# invokables: - -Articus\PathHandler\Router\FastRouteAnnotation: - metadata_cache: - adapter: - name: blackhole - handlers: +Articus\PathHandler\RouteInjection\Factory: + paths: + '/v2': - App\Handler\AnotherFakeDummy - App\Handler\Fake - App\Handler\FakeBodyWithFileSchema @@ -51,6 +28,18 @@ Articus\PathHandler\Router\FastRouteAnnotation: - App\Handler\UserLogin - App\Handler\UserLogout - App\Handler\UserUsername + handlers: + abstract_factories: + - Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory +# consumers: +# factories: +# invokables: +# attributes: +# factories: +# invokables: +# producers: +# factories: +# invokables: Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory: App\Handler\AnotherFakeDummy: [] diff --git a/samples/server/petstore/php-ze-ph/application/container.php b/samples/server/petstore/php-ze-ph/application/container.php index e70a0663cf3..3518792765f 100644 --- a/samples/server/petstore/php-ze-ph/application/container.php +++ b/samples/server/petstore/php-ze-ph/application/container.php @@ -1,9 +1,14 @@ setService('config', $config); diff --git a/samples/server/petstore/php-ze-ph/composer.json b/samples/server/petstore/php-ze-ph/composer.json index fd912caf6f4..7496b7dfda3 100644 --- a/samples/server/petstore/php-ze-ph/composer.json +++ b/samples/server/petstore/php-ze-ph/composer.json @@ -5,15 +5,17 @@ "version": "1.0.0", "type": "project", "require": { - "php": "^5.6 || ^7.0", - "ext-yaml" : "^1.2 || ^2.0", - "zendframework/zend-expressive": "^2.1", - "articus/path-handler": "^0.3", - "articus/data-transfer": "^0.1", - "zendframework/zend-serializer": "^2.8", - "zendframework/zend-config": "^3.1", - "nikic/fast-route": "^1.2", - "http-interop/http-middleware": "^0.4.1" + "php": "^7.1", + "ext-yaml": "^2.0", + "zendframework/zend-expressive": "^3.2", + "zendframework/zend-diactoros": "^2.1", + "articus/path-handler": "^0.4", + "articus/data-transfer": "^0.2", + "doctrine/annotations": "^1.6", + "zendframework/zend-cache": "^2.8", + "zendframework/zend-serializer": "^2.9", + "zendframework/zend-config": "^3.2", + "nikic/fast-route": "^1.3" }, "autoload": { "psr-4": { diff --git a/samples/server/petstore/php-ze-ph/public/index.php b/samples/server/petstore/php-ze-ph/public/index.php index fcb6dd63852..2f84d8776b0 100644 --- a/samples/server/petstore/php-ze-ph/public/index.php +++ b/samples/server/petstore/php-ze-ph/public/index.php @@ -1,4 +1,5 @@ process($request); - ErrorHandler::stop(true); - if (!($result instanceof Response)) { - throw new \RuntimeException(sprintf( - 'Invalid response: expecting %s, got %s', - Response::class, - is_object($result)? get_class($result) : gettype($result) - )); - } - } - catch (\Exception $error) { - $result = (new \Zend\Diactoros\Response())->withStatus(500, 'Internal server error'); - error_log((string)$error); - } - return $result; - } -} diff --git a/samples/server/petstore/php-ze-ph/src/App/Factory.php b/samples/server/petstore/php-ze-ph/src/App/Factory.php new file mode 100644 index 00000000000..0fb4a7d732a --- /dev/null +++ b/samples/server/petstore/php-ze-ph/src/App/Factory.php @@ -0,0 +1,58 @@ +get(Middleware\InternalServerError::class); + if (!($errorMiddleware instanceof Middleware\InternalServerError)) { + throw new \LogicException(\sprintf( + 'Invalid error middleware: expecting %s, not %s.', + Middleware\InternalServerError::class, + \is_object($errorMiddleware) ? \get_class($errorMiddleware) : \gettype($errorMiddleware) + )); + } + $pipeline = new MiddlewarePipe(); + $runner = new RequestHandlerRunner( + $pipeline, + $container->get(EmitterInterface::class), + $container->get(ServerRequestInterface::class), + function(\Throwable $error) use ($errorMiddleware) : ResponseInterface + { + return $errorMiddleware->handleError($error); + } + ); + $application = new Application( + $container->get(MiddlewareFactory::class), + $pipeline, + $container->get(RouteCollector::class), + $runner + ); + $application->pipe($errorMiddleware); + $application->pipe(RouteMiddleware::class); + $application->pipe(MethodNotAllowedMiddleware::class); + $application->pipe(DispatchMiddleware::class); + $application->pipe(NotFoundHandler::class); + + return $application; + } +} diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php b/samples/server/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php index 92d243b99f0..ee20ba08d24 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/AnotherFakeDummy.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/Fake.php b/samples/server/petstore/php-ze-ph/src/App/Handler/Fake.php index 2b70dfb709a..d93574b0367 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/Fake.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/Fake.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } /** * Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 + * @PHA\Post() * @param ServerRequestInterface $request * - * @throws PHException\HttpCode 500 if the method is not implemented + * @throws PHException\HttpCode 501 if the method is not implemented */ - public function handlePost(ServerRequestInterface $request) + public function testEndpointParameters(ServerRequestInterface $request) { //TODO implement method - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } /** * To test enum parameters + * @PHA\Get() * @param ServerRequestInterface $request * - * @throws PHException\HttpCode 500 if the method is not implemented + * @throws PHException\HttpCode 501 if the method is not implemented */ - public function handleGet(ServerRequestInterface $request) + public function testEnumParameters(ServerRequestInterface $request) { //TODO implement method - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } /** * Fake endpoint to test group parameters (optional) + * @PHA\Delete() * @param ServerRequestInterface $request * - * @throws PHException\HttpCode 500 if the method is not implemented + * @throws PHException\HttpCode 501 if the method is not implemented */ - public function handleDelete(ServerRequestInterface $request) + public function testGroupParameters(ServerRequestInterface $request) { //TODO implement method - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php index 219c81794fd..47ce029df6e 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithFileSchema.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php index e9fb79ea154..9844c597816 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeBodyWithQueryParams.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php index 6c01e08daf9..74991195dce 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeClassnameTest.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php index 7ef59dd1b91..bb6868c68e3 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeInlineAdditionalProperties.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php index 268ed06454c..ed3e0f6d2ef 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeJsonFormData.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php index 1ad2a520b82..595690de897 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/FakeOuterNumber.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } /** * Update an existing pet + * @PHA\Put() * TODO check if consumer is valid, if it has correct priority and if it can be moved to class annotation * @PHA\Consumer(name=PHConsumer\Json::class, mediaType="application/json") * TODO check if consumer is valid, if it has correct priority and if it can be moved to class annotation @@ -42,13 +44,13 @@ class Pet implements Operation\PostInterface, Operation\PutInterface * @PHA\Attribute(name=PHAttribute\Transfer::class, options={"type":\App\DTO\Pet::class,"objectAttr":"bodyData"}) * @param ServerRequestInterface $request * - * @throws PHException\HttpCode 500 if the method is not implemented + * @throws PHException\HttpCode 501 if the method is not implemented */ - public function handlePut(ServerRequestInterface $request) + public function updatePet(ServerRequestInterface $request) { //TODO implement method /** @var \App\DTO\Pet $bodyData */ $bodyData = $request->getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php b/samples/server/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php index 9369ee2ba0e..65fe83918d4 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/PetFindByStatus.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php b/samples/server/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php index 7206279d5bd..28aa169a0c8 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/StoreOrderOrderId.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php b/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php index 7ade895a442..8ac24b28f2f 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithArray.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php b/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php index 6cb9a9ad390..27ccafbb5d2 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/UserCreateWithList.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Handler/UserLogin.php b/samples/server/petstore/php-ze-ph/src/App/Handler/UserLogin.php index ac38c9c284c..2322e8f2317 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Handler/UserLogin.php +++ b/samples/server/petstore/php-ze-ph/src/App/Handler/UserLogin.php @@ -1,8 +1,8 @@ getAttribute("bodyData"); - throw new PHException\HttpCode(500, "Not implemented"); + throw new PHException\HttpCode(501, "Not implemented"); } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php b/samples/server/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php new file mode 100644 index 00000000000..ba889f44ae2 --- /dev/null +++ b/samples/server/petstore/php-ze-ph/src/App/Middleware/InternalServerError.php @@ -0,0 +1,54 @@ +responseGenerator = $responseGenerator; + } + + /** + * @inheritdoc + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $result = null; + try { + ErrorHandler::start(); + $result = $handler->handle($request); + ErrorHandler::stop(true); + } + catch (\Throwable $error) { + $result = $this->handleError($error); + } + return $result; + } + + public function handleError(\Throwable $error): ResponseInterface + { + \error_log((string)$error); + return $this->generateEmptyResponse()->withStatus(500, 'Internal server error'); + } + + protected function generateEmptyResponse(): ResponseInterface + { + return ($this->responseGenerator)(); + } +} diff --git a/samples/server/petstore/php-ze-ph/src/App/Strategy/Date.php b/samples/server/petstore/php-ze-ph/src/App/Strategy/Date.php index 8b1da8bf155..eedff2f01e2 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Strategy/Date.php +++ b/samples/server/petstore/php-ze-ph/src/App/Strategy/Date.php @@ -1,4 +1,5 @@ type = $options['type']; } diff --git a/samples/server/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php b/samples/server/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php index c506244c3f7..34f57d140fe 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php +++ b/samples/server/petstore/php-ze-ph/src/App/Strategy/QueryParameterArray.php @@ -1,4 +1,5 @@ delimiter = self::DELIMITER_MAP[$options['format']]; } @@ -40,11 +41,11 @@ class QueryParameterArray extends QueryParameter public function extract($objectValue, $object = null) { $result = null; - if (is_array($objectValue)) { + if (\is_array($objectValue)) { if ($this->delimiter === null) { $result = $objectValue; } else { - $result = implode($this->delimiter, $objectValue); + $result = \implode($this->delimiter, $objectValue); } } return $result; @@ -59,9 +60,9 @@ class QueryParameterArray extends QueryParameter if ($arrayValue !== null) { $list = null; if ($this->delimiter === null) { - $list = (is_array($arrayValue))? $arrayValue : [$arrayValue]; + $list = (\is_array($arrayValue))? $arrayValue : [$arrayValue]; } else { - $list = explode($this->delimiter, $arrayValue); + $list = \explode($this->delimiter, $arrayValue); } $result = []; foreach ($list as $item) { diff --git a/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php b/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php index 590ca3685a4..069cfaa51a3 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php +++ b/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterArrayType.php @@ -1,4 +1,6 @@ format; } @@ -22,21 +24,21 @@ class QueryParameterArrayType extends QueryParameterType * @param string $format * @return self */ - public function setFormat($format) + public function setFormat(string $format): self { $this->format = $format; return $this; } - protected function checkType($value) + protected function checkType($value): bool { $result = true; - if (!array_key_exists($this->format, QueryParameterArray::DELIMITER_MAP)) { - throw new \InvalidArgumentException(sprintf('Can not check for format %s.', $this->format)); + if (!\array_key_exists($this->format, QueryParameterArray::DELIMITER_MAP)) { + throw new \InvalidArgumentException(\sprintf('Can not check for format %s.', $this->format)); } $delimiter = QueryParameterArray::DELIMITER_MAP[$this->format]; if ($delimiter === null) { - if (is_array($value)) { + if (\is_array($value)) { foreach ($value as $item) { $result = $result && parent::checkType($item); } @@ -46,27 +48,27 @@ class QueryParameterArrayType extends QueryParameterType } else { switch ($this->type) { case QueryParameterArray::TYPE_INT: - $result = is_string($value) && preg_match(self::prepareRepeatingTypeRegExp(self::RE_INT, $delimiter), $value); + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_INT, $delimiter), $value); break; case QueryParameterArray::TYPE_BOOL: - $result = is_string($value) && preg_match(self::prepareRepeatingTypeRegExp(self::RE_BOOL, $delimiter), $value); + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_BOOL, $delimiter), $value); break; case QueryParameterArray::TYPE_FLOAT: - $result = is_string($value) && preg_match(self::prepareRepeatingTypeRegExp(self::RE_FLOAT, $delimiter), $value); + $result = \is_string($value) && \preg_match(self::prepareRepeatingTypeRegExp(self::RE_FLOAT, $delimiter), $value); break; case QueryParameterArray::TYPE_STRING: - $result = is_string($value); + $result = \is_string($value); break; default: - throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type)); + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); } } return $result; } - protected static function prepareRepeatingTypeRegExp($typeRegExp, $delimiter) + protected static function prepareRepeatingTypeRegExp(string $typeRegExp, string $delimiter): string { - $escapedDelimiter = preg_quote($delimiter, '/'); + $escapedDelimiter = \preg_quote($delimiter, '/'); return '/^(' . $typeRegExp . ')(' . $escapedDelimiter . '('. $typeRegExp . '))*$/'; } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php b/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php index d2b4fcd16b8..6643990b744 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php +++ b/samples/server/petstore/php-ze-ph/src/App/Validator/QueryParameterType.php @@ -1,4 +1,6 @@ type) { case QueryParameter::TYPE_INT: - return is_string($value) && preg_match('/^(' . self::RE_INT . ')$/', $value); + return \is_string($value) && \preg_match('/^(' . self::RE_INT . ')$/', $value); case QueryParameter::TYPE_BOOL: - return is_string($value) && preg_match('/^(' . self::RE_BOOL . ')$/', $value); + return \is_string($value) && \preg_match('/^(' . self::RE_BOOL . ')$/', $value); case QueryParameter::TYPE_FLOAT: - return is_string($value) && preg_match('/^(' . self::RE_FLOAT . ')$/', $value); + return \is_string($value) && \preg_match('/^(' . self::RE_FLOAT . ')$/', $value); case QueryParameter::TYPE_STRING: - return is_string($value); + return \is_string($value); default: - throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type)); + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); } } } diff --git a/samples/server/petstore/php-ze-ph/src/App/Validator/Type.php b/samples/server/petstore/php-ze-ph/src/App/Validator/Type.php index 69a148baca6..a3b883d6488 100644 --- a/samples/server/petstore/php-ze-ph/src/App/Validator/Type.php +++ b/samples/server/petstore/php-ze-ph/src/App/Validator/Type.php @@ -1,4 +1,6 @@ type; } @@ -34,14 +36,14 @@ class Type extends AbstractValidator * @param string $type * @return self */ - public function setType($type) + public function setType(string $type): self { $this->type = $type; return $this; } /** - * @inheritDoc + * @inheritdoc */ public function isValid($value) { @@ -53,19 +55,19 @@ class Type extends AbstractValidator return $result; } - protected function checkType($value) + protected function checkType($value): bool { switch ($this->type) { case 'int': - return is_int($value); + return \is_int($value); case 'bool': - return is_bool($value); + return \is_bool($value); case 'float': - return is_float($value) || is_int($value); + return \is_float($value) || \is_int($value); case 'string': - return is_string($value); + return \is_string($value); default: - throw new \InvalidArgumentException(sprintf('Can not check for type %s.', $this->type)); + throw new \InvalidArgumentException(\sprintf('Can not check for type %s.', $this->type)); } } }