Skip to content

Commit

Permalink
[php-symfony] Symfony6 support (#11810)
Browse files Browse the repository at this point in the history
* support symfony6

* fix issues with StrictJsonDeserialization

* regenerate samples

* add suggestions

* update samples

* support php 7.4 and symfony 5

* allow versions based on semantic versioning

* regenerate sample

* change method of determining result types

* update samples

* describe usage of bundle in symfony app

* better documentation

* fix duplicate auth methods

* do not set namespace for default types

* fix UploadedFile type

* next try fixing auth

* regenerate samples

* fix: auth method shall not be duplicated

* Revert "fix duplicate auth methods"

This reverts commit 0dc4187.

* chore: regenerate samples

* fix tests

* regenerate sample

* more fixes for tests

* update tests

* add kernel shutdown

Co-authored-by: Benjamin Haeublein <benjaminh@testing-vm.lan.benjaminh.de>
Co-authored-by: Renaud de Chivré <renaud@tahitiwebdesign.com>
  • Loading branch information
3 people committed May 25, 2022
1 parent 96dd6c5 commit 77fa028
Show file tree
Hide file tree
Showing 70 changed files with 603 additions and 776 deletions.
Expand Up @@ -348,6 +348,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("serialization/SerializerInterface.mustache", toSrcPath(servicePackage, srcBasePath), "SerializerInterface.php"));
supportingFiles.add(new SupportingFile("serialization/JmsSerializer.mustache", toSrcPath(servicePackage, srcBasePath), "JmsSerializer.php"));
supportingFiles.add(new SupportingFile("serialization/StrictJsonDeserializationVisitor.mustache", toSrcPath(servicePackage, srcBasePath), "StrictJsonDeserializationVisitor.php"));
supportingFiles.add(new SupportingFile("serialization/StrictJsonDeserializationVisitorFactory.mustache", toSrcPath(servicePackage, srcBasePath), "StrictJsonDeserializationVisitorFactory.php"));
supportingFiles.add(new SupportingFile("serialization/TypeMismatchException.mustache", toSrcPath(servicePackage, srcBasePath), "TypeMismatchException.php"));
// Validation components
supportingFiles.add(new SupportingFile("validation/ValidatorInterface.mustache", toSrcPath(servicePackage, srcBasePath), "ValidatorInterface.php"));
Expand Down Expand Up @@ -434,11 +435,26 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
} else {
op.vendorExtensions.put("x-comment-type", "void");
}
// Create a variable to add typing for return value of interface
if (op.returnType != null) {
if ("array".equals(op.returnContainer)) {
op.vendorExtensions.put("x-return-type", "iterable");
} else {
if (defaultIncludes.contains(op.returnType)) {
op.vendorExtensions.put("x-return-type", "array|" + op.returnType);
}
else {
op.vendorExtensions.put("x-return-type", "array|\\" + op.returnType);
}
}
} else {
op.vendorExtensions.put("x-return-type", "void");
}

// Add operation's authentication methods to whole interface
if (op.authMethods != null) {
for (CodegenSecurity am : op.authMethods) {
if (!authMethods.contains(am)) {
if (authMethods.stream().noneMatch(m -> Objects.equals(m.name, am.name))) {
authMethods.add(am);
}
}
Expand Down
@@ -1,8 +1,7 @@
language: php
dist: trusty
php:
- 7.1.3
- 7.2
- 8.1.1

install:
- composer install --dev --no-interaction
Expand Down
Expand Up @@ -2,7 +2,7 @@
/**
* {{bundleName}}ApiPass
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{invokerPackage}}\DependencyInjection\Compiler
Expand Down
Expand Up @@ -2,7 +2,7 @@
/**
* ApiServer
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{apiPackage}}
Expand All @@ -22,7 +22,7 @@ namespace {{apiPackage}};
/**
* ApiServer Class Doc Comment
*
* PHP version 5
* PHP version 8.1.1
*
* @category Class
* @package {{apiPackage}}
Expand Down
Expand Up @@ -2,7 +2,7 @@
/**
* {{bundleClassName}}
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{invokerPackage}}
Expand Down
Expand Up @@ -2,7 +2,7 @@
/**
* Controller
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{controllerPackage}}
Expand Down
Expand Up @@ -2,7 +2,7 @@
/**
* {{bundleExtensionName}}
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{invokerPackage}}\DependencyInjection
Expand Down Expand Up @@ -40,7 +40,7 @@ class {{bundleExtensionName}} extends Extension
$loader->load('services.yml');
}

public function getAlias()
public function getAlias(): string
{
return '{{bundleAlias}}';
}
Expand Down
Expand Up @@ -19,7 +19,7 @@ For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})

## Requirements

PHP 7.1.3 and later
PHP 8.0 and later

## Installation & Usage

Expand Down Expand Up @@ -56,26 +56,20 @@ composer install

Step 1: Please follow the [installation procedure](#installation--usage) first.

Step 2: Enable the bundle in the kernel:
Step 2: Enable the bundle in the bundle configuration:

```php
<?php
// app/AppKernel.php

public function registerBundles()
{
$bundles = array(
// ...
new {{invokerPackage}}\{{bundleClassName}}(),
// ...
);
}
// app/config/bundles.php
return [
// ...
{{invokerPackage}}\{{bundleClassName}}::class => ['all' => true],
];
```

Step 3: Register the routes:

```yaml
# app/config/routing.yml
# app/config/routes.yaml
{{bundleAlias}}:
resource: "@{{bundleName}}Bundle/Resources/config/routing.yml"
```
Expand Down Expand Up @@ -112,7 +106,7 @@ class {{baseName}}Api implements {{classname}} // An interface is autogenerated
/**
* Implementation of {{classname}}#{{operationId}}
*/
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}, &$responseCode, array &$responseHeaders): {{#isArray}}iterable{{/isArray}}{{^isArray}}array|{{{vendorExtensions.x-comment-type}}}{{/isArray}}
{
// Implement the operation ...
}
Expand All @@ -125,11 +119,10 @@ class {{baseName}}Api implements {{classname}} // An interface is autogenerated
Step 5: Tag your API implementation:

```yaml
# src/Acme/MyBundle/Resources/services.yml
# config/services.yml
services:
# ...
acme.my_bundle.api.{{pathPrefix}}:
class: Acme\MyBundle\Api\{{baseName}}Api
Acme\MyBundle\Api\{{baseName}}Api:
tags:
- { name: "{{bundleAlias}}.api", api: "{{pathPrefix}}" }
# ...
Expand Down
@@ -1,7 +1,8 @@
<?php
/**
* {{classname}}
* PHP version 7.1.3
*
* PHP version 8.1.1
*
* @category Class
* @package {{invokerPackage}}
Expand Down Expand Up @@ -59,15 +60,15 @@ interface {{classname}}
{{#allParams}}
* @param {{vendorExtensions.x-comment-type}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{#isDeprecated}} (deprecated){{/isDeprecated}}
{{/allParams}}
* @param \int $responseCode The HTTP response code to return
* @param \array $responseHeaders Additional HTTP headers to return with the response ()
*
* @return {{{vendorExtensions.x-comment-type}}}
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
*/
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}&$responseCode, array &$responseHeaders);
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/required}}, {{/allParams}}&$responseCode, array &$responseHeaders): {{{vendorExtensions.x-return-type}}};

{{/operation}}
}
{{/operations}}
Expand Up @@ -2,7 +2,7 @@

{{#operations}}/**
* {{controllerName}}
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{controllerPackage}}
Expand Down
Expand Up @@ -11,11 +11,10 @@ Method | HTTP request | Description
{{#operations}}
## Service Declaration
```yaml
# src/Acme/MyBundle/Resources/services.yml
# config/services.yml
services:
# ...
acme.my_bundle.api.{{pathPrefix}}:
class: Acme\MyBundle\Api\{{baseName}}Api
Acme\MyBundle\Api\{{baseName}}Api:
tags:
- { name: "{{bundleAlias}}.api", api: "{{pathPrefix}}" }
# ...
Expand Down Expand Up @@ -62,7 +61,7 @@ class {{baseName}}Api implements {{classname}}
/**
* Implementation of {{classname}}#{{operationId}}
*/
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}})
public function {{operationId}}({{#allParams}}{{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{paramName}}{{^required}} = {{#defaultValue}}'{{{.}}}'{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}, &$responseCode, array &$responseHeaders): {{{vendorExtensions.x-return-type}}}
{
// Implement the operation ...
}
Expand Down
Expand Up @@ -19,22 +19,24 @@
}
],
"require": {
"php": "^7.1.3",
"php": ">=7.4.0|>=8.0.2",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"symfony/validator": "*",
"jms/serializer-bundle": "^2.0",
"symfony/framework-bundle": "^4.4.8"
"symfony/validator": "^5.0|^6.0",
"jms/serializer-bundle": "^4.0",
"symfony/framework-bundle": "^5.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^2.16.3",
"symfony/browser-kit": "*",
"symfony/yaml": "^4.4.8",
"symfony/browser-kit": "^5.0|^6.0",
"symfony/yaml": "^5.0|^6.0",
"hoa/regex": "~1.0"
},
"autoload": {
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }
"psr-4": {
"{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/"
}
}
}
Expand Up @@ -4,7 +4,7 @@
/**
* {{classname}}
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{modelPackage}}
Expand Down
Expand Up @@ -11,7 +11,7 @@
path: {{path}}
methods: [{{httpMethod}}]
defaults:
_controller: {{bundleAlias}}.controller.{{pathPrefix}}:{{operationId}}Action
_controller: {{bundleAlias}}.controller.{{pathPrefix}}::{{operationId}}Action
{{#hasPathParams}}
requirements:
{{/hasPathParams}}
Expand Down
Expand Up @@ -5,18 +5,21 @@ namespace {{servicePackage}};
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\XmlDeserializationVisitor;
use JMS\Serializer\Visitor\Factory\XmlDeserializationVisitorFactory;
use DateTime;
use RuntimeException;

class JmsSerializer implements SerializerInterface
{
protected $serializer;
public function __construct()
{
$naming_strategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
$namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
$this->serializer = SerializerBuilder::create()
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitor($naming_strategy))
->setDeserializationVisitor('xml', new XmlDeserializationVisitor($naming_strategy))
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitorFactory())
->setDeserializationVisitor('xml', new XmlDeserializationVisitorFactory())
->setPropertyNamingStrategy($namingStrategy)
->build();
}

Expand Down Expand Up @@ -79,6 +82,11 @@ class JmsSerializer implements SerializerInterface
}

break;
case 'DateTime':
case '\DateTime':
return new DateTime($data);
default:
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}

// If we end up here, just return data
Expand Down

0 comments on commit 77fa028

Please sign in to comment.