Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php-symfony] Symfony6 support #11810

Merged
merged 27 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,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 @@ -430,11 +431,26 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
} 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* {{bundleClassName}}
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{invokerPackage}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Controller
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{controllerPackage}}
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{#operations}}/**
* {{controllerName}}
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{controllerPackage}}
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}}/"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* {{classname}}
*
* PHP version 7.1.3
* PHP version 8.1.1
*
* @category Class
* @package {{modelPackage}}
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading