-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Generate Config
class
#58771
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexandre-daubois
wants to merge
1
commit into
symfony:7.3
Choose a base branch
from
alexandre-daubois:generate-array-shapes
base: 7.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/Symfony/Component/Config/Builder/ArrayShapeGenerator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Config\Builder; | ||
|
||
use Symfony\Component\Config\Definition\ArrayNode; | ||
use Symfony\Component\Config\Definition\BaseNode; | ||
use Symfony\Component\Config\Definition\BooleanNode; | ||
use Symfony\Component\Config\Definition\EnumNode; | ||
use Symfony\Component\Config\Definition\FloatNode; | ||
use Symfony\Component\Config\Definition\IntegerNode; | ||
use Symfony\Component\Config\Definition\NodeInterface; | ||
use Symfony\Component\Config\Definition\NumericNode; | ||
use Symfony\Component\Config\Definition\PrototypedArrayNode; | ||
use Symfony\Component\Config\Definition\ScalarNode; | ||
use Symfony\Component\Config\Definition\StringNode; | ||
use Symfony\Component\Config\Definition\VariableNode; | ||
|
||
/** | ||
* @author Alexandre Daubois <alex.daubois@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class ArrayShapeGenerator | ||
{ | ||
public static function generate(ArrayNode $node): string | ||
{ | ||
return self::prependPhpDocWithStar(self::doGeneratePhpDoc($node)); | ||
} | ||
|
||
private static function doGeneratePhpDoc(NodeInterface $node, int $nestingLevel = 1): string | ||
{ | ||
if (!$node instanceof ArrayNode) { | ||
return $node->getName(); | ||
} | ||
|
||
if ($node instanceof PrototypedArrayNode) { | ||
$isHashmap = (bool) $node->getKeyAttribute(); | ||
|
||
$prototype = $node->getPrototype(); | ||
if ($prototype instanceof ArrayNode) { | ||
return 'array<'.($isHashmap ? 'string, ' : '').self::doGeneratePhpDoc($prototype, $nestingLevel).'>'; | ||
} | ||
|
||
return 'array<'.($isHashmap ? 'string, ' : '').self::handleScalarNode($prototype).'>'; | ||
} | ||
|
||
if (!($children = $node->getChildren()) && !$node->getParent() instanceof PrototypedArrayNode) { | ||
return 'array<array-key, mixed>'; | ||
} | ||
|
||
$arrayShape = \sprintf("array{%s\n", self::generateInlinePhpDocForNode($node)); | ||
|
||
/** @var NodeInterface $child */ | ||
foreach ($children as $child) { | ||
$arrayShape .= str_repeat(' ', $nestingLevel * 4).self::dumpNodeKey($child).': '; | ||
|
||
if ($child instanceof PrototypedArrayNode) { | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$isHashmap = (bool) $child->getKeyAttribute(); | ||
|
||
$arrayShape .= 'array<'.($isHashmap ? 'string, ' : '').self::handleNode($child->getPrototype(), $nestingLevel).'>'; | ||
} else { | ||
$arrayShape .= self::handleNode($child, $nestingLevel); | ||
} | ||
|
||
$arrayShape .= \sprintf(",%s\n", !$child instanceof ArrayNode ? self::generateInlinePhpDocForNode($child) : ''); | ||
} | ||
|
||
return $arrayShape.str_repeat(' ', 4 * ($nestingLevel - 1)).'}'; | ||
} | ||
|
||
private static function dumpNodeKey(NodeInterface $node): string | ||
{ | ||
$name = $node->getName(); | ||
$quoted = str_starts_with($name, '@') | ||
|| \in_array(strtolower($name), ['int', 'float', 'bool', 'null', 'scalar'], true) | ||
|| strpbrk($name, '\'"'); | ||
|
||
if ($quoted) { | ||
$name = "'".addslashes($name)."'"; | ||
} | ||
|
||
return $name.($node->isRequired() ? '' : '?'); | ||
} | ||
|
||
private static function handleNumericNode(NumericNode $node): string | ||
{ | ||
$min = $node->getMin() ?? 'min'; | ||
$max = $node->getMax() ?? 'max'; | ||
|
||
if ($node instanceof IntegerNode) { | ||
return \sprintf('int<%s, %s>', $min, $max); | ||
} elseif ($node instanceof FloatNode) { | ||
return 'float'; | ||
} | ||
|
||
return \sprintf('int<%s, %s>|float', $min, $max); | ||
} | ||
|
||
private static function prependPhpDocWithStar(string $shape): string | ||
{ | ||
return str_replace("\n", "\n * ", $shape); | ||
} | ||
|
||
private static function generateInlinePhpDocForNode(BaseNode $node): string | ||
{ | ||
$comment = ''; | ||
if ($node->hasDefaultValue() || $node->getInfo() || $node->isDeprecated()) { | ||
if ($node->isDeprecated()) { | ||
$comment .= 'Deprecated: '.$node->getDeprecation($node->getName(), $node->getPath())['message'].' '; | ||
} | ||
|
||
if ($info = $node->getInfo()) { | ||
$comment .= $info.' '; | ||
} | ||
|
||
if ($node->hasDefaultValue() && !\is_array($defaultValue = $node->getDefaultValue())) { | ||
$comment .= 'Default: '.json_encode($defaultValue, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE | \JSON_PRESERVE_ZERO_FRACTION); | ||
} | ||
} | ||
|
||
return $comment ? ' // '.rtrim(preg_replace('/\s+/', ' ', $comment)) : ''; | ||
} | ||
|
||
private static function handleNode(NodeInterface $node, int $nestingLevel): string | ||
{ | ||
if ($node instanceof ArrayNode) { | ||
return self::doGeneratePhpDoc($node, 1 + $nestingLevel); | ||
} | ||
|
||
return self::handleScalarNode($node); | ||
} | ||
|
||
private static function handleScalarNode(NodeInterface $node): string | ||
{ | ||
return match (true) { | ||
$node instanceof BooleanNode => 'bool', | ||
$node instanceof StringNode => 'string', | ||
$node instanceof NumericNode => self::handleNumericNode($node), | ||
$node instanceof EnumNode => $node->getPermissibleValues('|'), | ||
$node instanceof ScalarNode => 'string|int|float|bool', | ||
$node instanceof VariableNode => 'mixed', | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.