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 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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.1.1 and later

## Installation & Usage

Expand Down Expand Up @@ -112,7 +112,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}}): {{#isArray}}iterable{{/isArray}}{{^isArray}}array|{{{vendorExtensions.x-comment-type}}}{{/isArray}}
{
// Implement the operation ...
}
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): {{#isArray}}iterable{{/isArray}}{{^isArray}}array|{{{vendorExtensions.x-comment-type}}}{{/isArray}};

{{/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 @@ -62,7 +62,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}}): {{#isArray}}iterable{{/isArray}}{{^isArray}}array|{{{vendorExtensions.x-comment-type}}}{{/isArray}}
{
// 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": ">=8.0.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be breaking to keep also >= 7.4 supported?
>=7.4
>=7.4.0|>=8.0.2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is: symfony6 only supports php>=8.0.2.
But now that I think about it, I can currently not see an obvious reason why symfony5 should not work.

I'll try it in the next days.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were no conflicts. So it works with symfony 5.

I'm not sure why I decided it needs to target symfony6 only in the first place.

"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"symfony/validator": "*",
"jms/serializer-bundle": "^2.0",
"symfony/framework-bundle": "^4.4.8"
"symfony/validator": "6.0.*",
"jms/serializer-bundle": "4.0.*",
"symfony/framework-bundle": "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": "6.0.*",
"symfony/yaml": "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,22 @@ namespace {{servicePackage}};
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\Visitor\Factory\XmlDeserializationVisitorFactory;
use JMS\Serializer\XmlDeserializationVisitor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unused import statement can be removed

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 +83,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
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,145 @@
* limitations under the License.
*/

declare(strict_types=1);

namespace {{servicePackage}};

use JMS\Serializer\Context;
use JMS\Serializer\JsonDeserializationVisitor;
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;

class StrictJsonDeserializationVisitor extends JsonDeserializationVisitor
class StrictJsonDeserializationVisitor implements DeserializationVisitorInterface
{
protected $jsonDeserializationVisitor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type missing -> JsonDeserializationVisitor


public function __construct(
int $options = 0,
int $depth = 512
) {
$this->jsonDeserializationVisitor = new JsonDeserializationVisitor($options, $depth);
}

/**
* {@inheritdoc}
*/
public function visitString($data, array $type, Context $context)
public function visitNull($data, array $type)
{
return $this->jsonDeserializationVisitor->visitNull($data, $type);
}

/**
* {@inheritdoc}
*/
public function visitString($data, array $type): string
{
if (!is_string($data)) {
throw TypeMismatchException::fromValue('string', $data, $context);
throw TypeMismatchException::fromValue('string', $data);
}

return parent::visitString($data, $type, $context);
return $this->jsonDeserializationVisitor->visitString($data, $type);
}

/**
* {@inheritdoc}
*/
public function visitBoolean($data, array $type, Context $context)
public function visitBoolean($data, array $type): bool
{
if (!is_bool($data)) {
throw TypeMismatchException::fromValue('boolean', $data, $context);
throw TypeMismatchException::fromValue('boolean', $data);
}

return parent::visitBoolean($data, $type, $context);
return $this->jsonDeserializationVisitor->visitBoolean($data, $type);
}

/**
* {@inheritdoc}
*/
public function visitInteger($data, array $type, Context $context)
public function visitInteger($data, array $type): int
{
if (!is_int($data)) {
throw TypeMismatchException::fromValue('integer', $data, $context);
throw TypeMismatchException::fromValue('integer', $data);
}

return parent::visitInteger($data, $type, $context);
return $this->jsonDeserializationVisitor->visitInteger($data, $type);
}

/**
* {@inheritdoc}
*/
public function visitDouble($data, array $type, Context $context)
public function visitDouble($data, array $type): float
{
if (!is_float($data) && !is_integer($data)) {
throw TypeMismatchException::fromValue('double', $data, $context);
throw TypeMismatchException::fromValue('double', $data);
}

return parent::visitDouble($data, $type, $context);
return $this->jsonDeserializationVisitor->visitDouble($data, $type);
}

/**
* {@inheritdoc}
*/
public function visitArray($data, array $type): array
{
return $this->jsonDeserializationVisitor->visitArray($data, $type);
}

/**
* {@inheritdoc}
*/
public function visitDiscriminatorMapProperty($data, ClassMetadata $metadata): string
{
return $this->jsonDeserializationVisitor->visitDiscriminatorMapProperty($data, $type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$type does not exist in this method scope.

i guess you meant to use $metadata?

}

/**
* {@inheritdoc}
*/
public function startVisitingObject(ClassMetadata $metadata, object $object, array $type): void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2nd param changed from object $data to object $object compared to the base method

{
$this->jsonDeserializationVisitor->startVisitingObject($metadata, $object, $type);
}

/**
* {@inheritdoc}
*/
public function visitProperty(PropertyMetadata $metadata, $data)
{
return $this->jsonDeserializationVisitor->visitProperty($metadata, $data);
}

/**
* {@inheritdoc}
*/
public function endVisitingObject(ClassMetadata $metadata, $data, array $type): object
{
return $this->jsonDeserializationVisitor->endVisitingObject($metadata, $data, $type);
}

/**
* {@inheritdoc}
*/
public function getResult($data)
{
return $this->jsonDeserializationVisitor->getResult($data);
}

/**
* {@inheritdoc}
*/
public function prepare($data)
{
return $this->jsonDeserializationVisitor->prepare($data);
}

/**
* {@inheritdoc}
*/
public function setNavigator(GraphNavigatorInterface $navigator): void
{
$this->jsonDeserializationVisitor->setNavigator($navigator);
}

}