Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit81124f2

Browse files
[FrameworkBundle] Add theconfig() function
1 parent3b5f623 commit81124f2

File tree

25 files changed

+1400
-14
lines changed

25 files changed

+1400
-14
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add the`Symfony\Config\config()` function
8+
49
7.2
510
---
611

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigBuilderCacheWarmer.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
usePsr\Log\LoggerInterface;
1515
useSymfony\Component\Config\Builder\ConfigBuilderGenerator;
16-
useSymfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
16+
useSymfony\Component\Config\Builder\ConfigFunctionAwareBuilderGeneratorInterface;
1717
useSymfony\Component\Config\Definition\ConfigurationInterface;
1818
useSymfony\Component\DependencyInjection\Container;
1919
useSymfony\Component\DependencyInjection\ContainerBuilder;
@@ -29,6 +29,7 @@
2929
* Generate all config builders.
3030
*
3131
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
32+
* @author Alexandre Daubois <alex.daubois@gmail.com>
3233
*
3334
* @final since Symfony 7.1
3435
*/
@@ -68,19 +69,30 @@ public function warmUp(string $cacheDir, ?string $buildDir = null): array
6869
}
6970
}
7071

72+
$configurations = [];
7173
foreach ($extensionsas$extension) {
74+
if (null ===$configuration =$this->getConfigurationFromExtension($extension)) {
75+
continue;
76+
}
77+
78+
$configurations[$extension->getAlias()] =$configuration;
79+
7280
try {
73-
$this->dumpExtension($extension,$generator);
81+
$generator->build($configurations[$extension->getAlias()]);
7482
}catch (\Exception$e) {
7583
$this->logger?->warning('Failed to generate ConfigBuilder for extension {extensionClass}:'.$e->getMessage(), ['exception' =>$e,'extensionClass' =>$extension::class]);
7684
}
7785
}
7886

87+
if (class_exists(ConfigFunctionAwareBuilderGeneratorInterface::class) &&$configurations) {
88+
$generator->buildConfigFunction($configurations);
89+
}
90+
7991
// No need to preload anything
8092
return [];
8193
}
8294

83-
privatefunctiondumpExtension(ExtensionInterface$extension,ConfigBuilderGeneratorInterface$generator):void
95+
privatefunctiongetConfigurationFromExtension(ExtensionInterface$extension):?ConfigurationInterface
8496
{
8597
$configuration =null;
8698
if ($extensioninstanceof ConfigurationInterface) {
@@ -90,11 +102,7 @@ private function dumpExtension(ExtensionInterface $extension, ConfigBuilderGener
90102
$configuration =$extension->getConfiguration([],newContainerBuilder($containerinstanceof Container ?newContainerBag($container) :newParameterBag()));
91103
}
92104

93-
if (!$configuration) {
94-
return;
95-
}
96-
97-
$generator->build($configuration);
105+
return$configuration;
98106
}
99107

100108
publicfunctionisOptional():bool
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Config\Builder;
13+
14+
useSymfony\Component\Config\Definition\ArrayNode;
15+
useSymfony\Component\Config\Definition\BaseNode;
16+
useSymfony\Component\Config\Definition\BooleanNode;
17+
useSymfony\Component\Config\Definition\EnumNode;
18+
useSymfony\Component\Config\Definition\FloatNode;
19+
useSymfony\Component\Config\Definition\IntegerNode;
20+
useSymfony\Component\Config\Definition\NodeInterface;
21+
useSymfony\Component\Config\Definition\NumericNode;
22+
useSymfony\Component\Config\Definition\PrototypedArrayNode;
23+
useSymfony\Component\Config\Definition\ScalarNode;
24+
useSymfony\Component\Config\Definition\StringNode;
25+
useSymfony\Component\Config\Definition\VariableNode;
26+
27+
/**
28+
* @author Alexandre Daubois <alex.daubois@gmail.com>
29+
*
30+
* @internal
31+
*/
32+
finalclass ArrayShapeGenerator
33+
{
34+
publicconstFORMAT_PHPDOC ='phpdoc';
35+
publicconstFORMAT_JETBRAINS_ATTRIBUTE ='jetbrains_attribute';
36+
37+
/**
38+
* @param self::FORMAT_* $format
39+
*/
40+
publicstaticfunctiongenerate(ArrayNode$node,string$format):string
41+
{
42+
if (self::FORMAT_PHPDOC ===$format) {
43+
returnself::prependPhpDocWithStar(self::doGeneratePhpDoc($node));
44+
}
45+
46+
if (self::FORMAT_JETBRAINS_ATTRIBUTE ===$format) {
47+
returnself::doGenerateJetBrainsArrayShape($node);
48+
}
49+
50+
thrownew \LogicException(\sprintf('Unsupported format to generate array shape. Expected one of "%s", got "%s".',implode('", "', [self::FORMAT_PHPDOC,self::FORMAT_JETBRAINS_ATTRIBUTE]),$format));
51+
}
52+
53+
privatestaticfunctiondoGeneratePhpDoc(NodeInterface$node,int$nestingLevel =1):string
54+
{
55+
if (!$nodeinstanceof ArrayNode) {
56+
return$node->getName();
57+
}
58+
59+
if (!$children =$node->getChildren()) {
60+
return'array<array-key, mixed>';
61+
}
62+
63+
if ($nodeinstanceof PrototypedArrayNode) {
64+
$prototype =$node->getPrototype();
65+
if ($prototypeinstanceof ArrayNode) {
66+
return'array<'.self::doGeneratePhpDoc($prototype,1 +$nestingLevel).'>';
67+
}
68+
69+
return'array<'.self::handleNodeType($prototype).'>';
70+
}
71+
72+
$arrayShape ='array{'.\PHP_EOL;
73+
74+
$handleNode =function (NodeInterface$node,int$nestingLevel):string {
75+
if ($nodeinstanceof ArrayNode) {
76+
returnself::doGeneratePhpDoc($node,1 +$nestingLevel);
77+
}else {
78+
returnself::handleNodeType($node);
79+
}
80+
};
81+
82+
/** @var NodeInterface $child */
83+
foreach ($childrenas$child) {
84+
$arrayShape .=str_repeat('',$nestingLevel *4).self::dumpNodeKey($child).':';
85+
86+
if ($childinstanceof PrototypedArrayNode) {
87+
$arrayShape .='array<'.$handleNode($child->getPrototype(),$nestingLevel).'>';
88+
}else {
89+
$arrayShape .=$handleNode($child,$nestingLevel);
90+
}
91+
92+
$arrayShape .=','.\PHP_EOL;
93+
}
94+
95+
return$arrayShape.str_repeat('',4 * ($nestingLevel -1)).'}';
96+
}
97+
98+
privatestaticfunctiondoGenerateJetBrainsArrayShape(NodeInterface$node,int$nestingLevel =1):string
99+
{
100+
if (!$nodeinstanceof ArrayNode) {
101+
return$node->getName();
102+
}
103+
104+
$children =$node->getChildren();
105+
106+
$shape ='';
107+
if (1 ===$nestingLevel) {
108+
$shape ='#[ArrayShape(';
109+
}elseif (!$children) {
110+
return"'array<array-key, mixed>'";
111+
}
112+
113+
if ($nodeinstanceof PrototypedArrayNode) {
114+
$prototype =$node->getPrototype();
115+
if ($prototypeinstanceof ArrayNode) {
116+
return'array<'.self::doGeneratePhpDoc($prototype,1 +$nestingLevel).'>';
117+
}
118+
119+
return'array<'.self::handleNodeType($prototype).'>';
120+
}
121+
122+
$shape .='['.\PHP_EOL;
123+
124+
$handleNode =function (NodeInterface$node,int$nestingLevel):string {
125+
if ($nodeinstanceof ArrayNode) {
126+
returnself::doGenerateJetBrainsArrayShape($node,1 +$nestingLevel);
127+
}else {
128+
return"'".self::handleNodeType($node)."'";
129+
}
130+
};
131+
132+
/** @var BaseNode $child */
133+
foreach ($childrenas$child) {
134+
$shape .=\sprintf("%s'%s' =>",str_repeat('',4 *$nestingLevel),$child->getName());
135+
136+
if ($childinstanceof PrototypedArrayNode) {
137+
$shape .='['.$handleNode($child->getPrototype(),$nestingLevel).']';
138+
}else {
139+
$shape .=$handleNode($child,$nestingLevel);
140+
}
141+
142+
$shape .=','.self::generateInlinePhpDocForNode($child).\PHP_EOL;
143+
}
144+
145+
$shape .=str_repeat('',4 * ($nestingLevel -1)).']';
146+
147+
return$shape.(1 ===$nestingLevel ?')]' :'');
148+
}
149+
150+
privatestaticfunctiondumpNodeKey(NodeInterface$node):string
151+
{
152+
return$node->getName().($node->isRequired() ?'' :'?');
153+
}
154+
155+
privatestaticfunctionhandleNumericNode(NumericNode$node):string
156+
{
157+
if ($nodeinstanceof IntegerNode) {
158+
$type ='int<%s, %s>';
159+
}elseif ($nodeinstanceof FloatNode) {
160+
$type ='float<%s, %s>';
161+
}else {
162+
$type ='int<%s, %s>|float<%1$s, %2$s>';
163+
}
164+
165+
$min =$node->getMin() ??'min';
166+
$max =$node->getMax() ??'max';
167+
168+
return\sprintf($type,$min,$max);
169+
}
170+
171+
privatestaticfunctionprependPhpDocWithStar(string$shape):string
172+
{
173+
returnstr_replace("\n","\n *",$shape);
174+
}
175+
176+
privatestaticfunctiongenerateInlinePhpDocForNode(BaseNode$node):string
177+
{
178+
$hasContent =false;
179+
$comment =' /*';
180+
181+
if ($node->hasDefaultValue() ||$node->getInfo() ||$node->isDeprecated()) {
182+
if ($node->isDeprecated()) {
183+
$hasContent =true;
184+
$comment .='Deprecated:'.$node->getDeprecation($node->getName(),$node->getPath())['message'].'';
185+
}
186+
187+
if ($info =$node->getInfo()) {
188+
$hasContent =true;
189+
$comment .=$info.'';
190+
}
191+
192+
if ($node->hasDefaultValue() && !\is_array($defaultValue =$node->getDefaultValue())) {
193+
$hasContent =true;
194+
$comment .='Default:'.json_encode($defaultValue).'.';
195+
}
196+
197+
$comment .='*/';
198+
}
199+
200+
return$hasContent ?$comment :'';
201+
}
202+
203+
privatestaticfunctionhandleNodeType(NodeInterface$node):string
204+
{
205+
returnmatch (true) {
206+
$nodeinstanceof BooleanNode =>'bool',
207+
$nodeinstanceof StringNode =>'string',
208+
$nodeinstanceof NumericNode =>self::handleNumericNode($node),
209+
$nodeinstanceof EnumNode =>$node->getPermissibleValues('|'),
210+
$nodeinstanceof ScalarNode =>'string|int|float|bool',
211+
$nodeinstanceof VariableNode =>'mixed',
212+
};
213+
}
214+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp