Skip to content

Commit

Permalink
Merge branch hotfix/v2.3.28
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Sep 24, 2024
1 parent 0547c66 commit 7501ef4
Show file tree
Hide file tree
Showing 24 changed files with 570 additions and 608 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ jobs:
run: vendor/bin/phpcs --extensions=php --warning-severity=0 --standard=PSR12 -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
- name: Run atoum unit tests
run: vendor/bin/atoum -f tests/units/*
- name: Run unit tests
run: vendor/bin/phpunit -v --whitelist ./src tests
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2023 Ambroise Maupate
Copyright © 2024 Ambroise Maupate

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

24 changes: 14 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,34 @@
"php": ">=8.1",
"ext-json": "*",
"roadiz/nodetype-contracts": "~1.1.2",
"symfony/string": "5.4.*",
"symfony/yaml": "5.4.*",
"symfony/serializer": "5.4.*",
"symfony/options-resolver": "5.4.*"
"symfony/string": "6.4.*",
"symfony/yaml": "6.4.*",
"symfony/serializer": "6.4.*",
"symfony/options-resolver": "6.4.*"
},
"suggest": {
"api-platform/core": "If you need to create ApiFilter annotation right into your entities"
},
"autoload": {
"psr-4": {
"RZ\\Roadiz\\EntityGenerator\\": "src/",
"tests\\mocks\\": "tests/mocks/"
"RZ\\Roadiz\\EntityGenerator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"RZ\\Roadiz\\EntityGenerator\\Tests\\": "tests/"
}
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^1.5.3",
"atoum/atoum": "^4.0.0",
"api-platform/core": "~2.7.0"
"phpunit/phpunit": "^9.5",
"api-platform/core": "~3.2.14"
},
"extra": {
"branch-alias": {
"dev-main": "2.2.x-dev",
"dev-develop": "2.3.x-dev"
"dev-main": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
}
}
}
7 changes: 4 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
parameters:
level: max
level: 7
paths:
- src
excludePaths:
- */node_modules/*
- */bower_components/*
- */static/*
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.generics
83 changes: 61 additions & 22 deletions src/Attribute/AttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class AttributeGenerator
{
protected string $className;
/**
* @var array<string|int, string|int>
* @var array<string|int, string|int|array>
*/
protected array $parameters;

/**
* @param string $className
* @param int[]|string[] $parameters
* @param array<string|int, string|int|array|null> $parameters
*/
public function __construct(string $className, array $parameters = [])
{
Expand All @@ -24,55 +24,94 @@ public function __construct(string $className, array $parameters = [])

public static function wrapString(string $string): string
{
return sprintf('"%s"', $string);
return sprintf('"%s"', str_replace('"', '\\"', $string));
}

public function generate(int $currentIndentation = 0): string
{
$formattedParams = [];
if (count($this->parameters) > 3) {
foreach ($this->parameters as $name => $parameter) {
if (is_string($name) && !empty($name)) {
$formattedParams[] = sprintf(
'%s%s: %s',
str_repeat(' ', $currentIndentation + 4),
$name,
$parameter
);
} else {
$formattedParams[] = sprintf(
'%s%s',
str_repeat(' ', $currentIndentation + 4),
$parameter
);
if (empty($parameter)) {
continue;
}
$formattedParams[] = $this->formatProperties($name, $parameter, $currentIndentation);
}
return
str_repeat(' ', $currentIndentation) .
$this->className .
sprintf(
'(%s%s%s)',
PHP_EOL,
implode(',' . PHP_EOL, $formattedParams),
implode(',' . PHP_EOL, array_filter($formattedParams)),
PHP_EOL . str_repeat(' ', $currentIndentation),
);
} elseif (count($this->parameters) > 0) {
foreach ($this->parameters as $name => $parameter) {
if (is_string($name) && !empty($name)) {
$formattedParams[] = sprintf('%s: %s', $name, $parameter);
} else {
$formattedParams[] = $parameter;
if (empty($parameter)) {
continue;
}
$formattedParams[] = $this->formatProperties($name, $parameter, -4);
}
return
str_repeat(' ', $currentIndentation) .
$this->className .
sprintf(
'(%s)',
implode(', ', $formattedParams)
implode(', ', array_filter($formattedParams))
);
} else {
return str_repeat(' ', $currentIndentation) . $this->className;
}
}

/**
* @param string $name
* @param array<string, mixed> $parameter
* @param int $currentIndentation
* @return string
* @throws \JsonException
*/
protected function formatArrayObject(string $name, array $parameter, int $currentIndentation = 0): string
{
$encodedParameterContent = [];
foreach ($parameter as $key => $value) {
if (is_string($key)) {
$encodedParameterContent[] = sprintf(
'%s => %s',
self::wrapString($key),
\json_encode($value, \JSON_THROW_ON_ERROR)
);
}
}
return sprintf(
'%s%s: %s',
str_repeat(' ', $currentIndentation + 4),
$name,
'[' . implode(', ', $encodedParameterContent) . ']'
);
}

protected function formatProperties(string|int $name, mixed $parameter, int $currentIndentation = 0): ?string
{
if (empty($parameter)) {
return null;
}
if (is_string($name) && \is_array($parameter)) {
return $this->formatArrayObject($name, $parameter, $currentIndentation);
}
if (is_string($name) && !empty($name)) {
return sprintf(
'%s%s: %s',
str_repeat(' ', $currentIndentation + 4),
$name,
$parameter
);
}
return sprintf(
'%s%s',
str_repeat(' ', $currentIndentation + 4),
$parameter
);
}
}
9 changes: 7 additions & 2 deletions src/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,15 @@ protected function getClassAttributes(): string
*/
protected function getClassAnnotations(): string
{
$annotations = [
$this->nodeType->getName() . ' node-source entity.',
$this->nodeType->getDescription()
];
$annotations = array_filter($annotations);

return '
/**
* DO NOT EDIT
* Generated custom node-source type by Roadiz.
* ' . implode(PHP_EOL . ' * ', $annotations) . '
*/' . PHP_EOL;
}

Expand Down
13 changes: 3 additions & 10 deletions src/EntityGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@

final class EntityGeneratorFactory
{
private NodeTypeResolverInterface $nodeTypeResolverBag;
private DefaultValuesResolverInterface $defaultValuesResolver;
private array $options;

public function __construct(
NodeTypeResolverInterface $nodeTypeResolverBag,
DefaultValuesResolverInterface $defaultValuesResolver,
array $options
private readonly NodeTypeResolverInterface $nodeTypeResolverBag,
private readonly DefaultValuesResolverInterface $defaultValuesResolver,
private readonly array $options
) {
$this->nodeTypeResolverBag = $nodeTypeResolverBag;
$this->defaultValuesResolver = $defaultValuesResolver;
$this->options = $options;
}

public function create(NodeTypeInterface $nodeType): EntityGeneratorInterface
Expand Down
21 changes: 21 additions & 0 deletions src/Field/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ protected function getFieldAttributes(bool $exclude = false): array
$attributes[] = new AttributeGenerator('SymfonySerializer\Groups', [
$this->getSerializationGroups()
]);

$description = $this->field->getLabel();
if (!empty($this->field->getDescription())) {
$description .= ': ' . $this->field->getDescription();
}
if ($this->field->isEnum() && null !== $defaultValues = $this->field->getDefaultValues()) {
$enumValues = explode(',', $defaultValues);
$enumValues = array_filter(array_map('trim', $enumValues));
$openapiContext = [
'type' => 'string',
'enum' => $enumValues,
'example' => $enumValues[0] ?? null,
];
}
$attributes[] = new AttributeGenerator('\ApiPlatform\Metadata\ApiProperty', [
'description' => AttributeGenerator::wrapString($description),
'schema' => $openapiContext ?? null,
'example' => $this->field->getPlaceholder() ?
AttributeGenerator::wrapString($this->field->getPlaceholder()) :
null,
]);
if ($this->getSerializationMaxDepth() > 0) {
$attributes[] = new AttributeGenerator('SymfonySerializer\MaxDepth', [
$this->getSerializationMaxDepth()
Expand Down
35 changes: 12 additions & 23 deletions src/Field/CustomFormsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,12 @@ public function getFieldGetter(): string
public function ' . $this->field->getGetterName() . '(): array
{
if (null === $this->' . $this->field->getVarName() . ') {
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
if (null !== $this->objectManager) {
$this->' . $this->field->getVarName() . ' = $this->objectManager
->getRepository(' . $this->options['custom_form_class'] . '::class)
->findByNodeAndField(
->findByNodeAndFieldName(
$this->getNode(),
$this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '")
\'' . $this->field->getName() . '\'
);
} else {
$this->' . $this->field->getVarName() . ' = [];
Expand All @@ -84,22 +80,15 @@ protected function getFieldSetter(): string
*/
public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['custom_form_class'] . ' $customForm): static
{
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$field = $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '");
if (null !== $field) {
$nodeCustomForm = new ' . $this->options['custom_form_proxy_class'] . '(
$this->getNode(),
$customForm,
$field
);
$this->objectManager->persist($nodeCustomForm);
$this->getNode()->addCustomForm($nodeCustomForm);
$this->' . $this->field->getVarName() . ' = null;
}
if (null !== $this->objectManager) {
$nodeCustomForm = new ' . $this->options['custom_form_proxy_class'] . '(
$this->getNode(),
$customForm
);
$nodeCustomForm->setFieldName(\'' . $this->field->getName() . '\');
$this->objectManager->persist($nodeCustomForm);
$this->getNode()->addCustomForm($nodeCustomForm);
$this->' . $this->field->getVarName() . ' = null;
}
return $this;
}' . PHP_EOL;
Expand Down
37 changes: 13 additions & 24 deletions src/Field/DocumentsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ public function getFieldGetter(): string
public function ' . $this->field->getGetterName() . '(): array
{
if (null === $this->' . $this->field->getVarName() . ') {
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
if (null !== $this->objectManager) {
$this->' . $this->field->getVarName() . ' = $this->objectManager
->getRepository(' . $this->options['document_class'] . '::class)
->findByNodeSourceAndField(
->findByNodeSourceAndFieldName(
$this,
$this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '")
\'' . $this->field->getName() . '\'
);
} else {
$this->' . $this->field->getVarName() . ' = [];
Expand All @@ -92,23 +88,16 @@ protected function getFieldSetter(): string
*/
public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['document_class'] . ' $document): static
{
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$field = $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '");
if (null !== $field) {
$nodeSourceDocument = new ' . $this->options['document_proxy_class'] . '(
$this,
$document,
$field
);
if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) {
$this->objectManager->persist($nodeSourceDocument);
$this->addDocumentsByFields($nodeSourceDocument);
$this->' . $this->field->getVarName() . ' = null;
}
if (null !== $this->objectManager) {
$nodeSourceDocument = new ' . $this->options['document_proxy_class'] . '(
$this,
$document
);
$nodeSourceDocument->setFieldName(\'' . $this->field->getName() . '\');
if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) {
$this->objectManager->persist($nodeSourceDocument);
$this->addDocumentsByFields($nodeSourceDocument);
$this->' . $this->field->getVarName() . ' = null;
}
}
return $this;
Expand Down
Loading

0 comments on commit 7501ef4

Please sign in to comment.