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

Commit469ea85

Browse files
[DependencyInjection][Routing] Define array-shapes to help writing PHP configs using yaml-like arrays
1 parent9359b31 commit469ea85

28 files changed

+746
-194
lines changed

‎composer.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@
197197
"Symfony\\Bridge\\Twig\\":"src/Symfony/Bridge/Twig/",
198198
"Symfony\\Bundle\\":"src/Symfony/Bundle/",
199199
"Symfony\\Component\\":"src/Symfony/Component/",
200+
"Symfony\\Config\\": [
201+
"src/Symfony/Component/DependencyInjection/Loader/Config/",
202+
"src/Symfony/Component/Routing/Loader/Config/"
203+
],
200204
"Symfony\\Runtime\\Symfony\\Component\\":"src/Symfony/Component/Runtime/Internal/"
201205
},
202206
"files": [

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Allow multiple`#[AsDecorator]` attributes
1313
* Handle returning arrays and config-builders from config files
1414
* Handle declaring services using PHP arrays that follow the same shape as corresponding yaml files
15+
* Add`ServicesConfig` to help writing PHP configs using yaml-like array-shapes
1516
* Deprecate using`$this` or its internal scope from PHP config files; use the`$loader` variable instead
1617
* Deprecate XML configuration format, use YAML or PHP instead
1718
* Deprecate`ExtensionInterface::getXsdValidationBasePath()` and`getNamespace()`
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Config;
13+
14+
/**
15+
* Pre-configured ServicesConfig for the App namespace.
16+
*
17+
* This class helps configuring the App namespace with autowiring and autoconfiguration enabled by default.
18+
* Use ServicesConfig directly if you don't want this behavior, or if you're a bundle author.
19+
*
20+
* @phpstan-import-type Services from ServicesConfig
21+
* @phpstan-import-type Imports from ServicesConfig
22+
* @phpstan-import-type Parameters from ServicesConfig
23+
* @phpstan-import-type Defaults from ServicesConfig
24+
* @phpstan-import-type Instanceof from ServicesConfig
25+
*/
26+
class AppServicesConfigextends ServicesConfig
27+
{
28+
/**
29+
* @param Imports $imports
30+
* @param Parameters $parameters
31+
* @param Defaults $defaults
32+
* @param Instanceof $instanceof
33+
* @param Services $services
34+
*/
35+
publicfunction__construct(
36+
array$services = [],
37+
array$imports = [],
38+
array$parameters = [],
39+
array$defaults = [],
40+
array$instanceof = [],
41+
) {
42+
$services =array_merge([
43+
'App\\' => ['resource' =>'%kernel.project_dir%/src'],
44+
],$services);
45+
46+
$defaults =array_merge([
47+
'autowire' =>true,
48+
'autoconfigure' =>true,
49+
],$defaults);
50+
51+
parent::__construct($services,$imports,$parameters,$defaults,$instanceof);
52+
}
53+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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\Config;
13+
14+
useSymfony\Component\DependencyInjection\ContainerInterface;
15+
useSymfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16+
useSymfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
17+
useSymfony\Component\DependencyInjection\Reference;
18+
useSymfony\Component\ExpressionLanguage\Expression;
19+
20+
require_once__DIR__.\DIRECTORY_SEPARATOR.'functions.php';
21+
22+
/**
23+
* @phpstan-type Imports list<string|array{
24+
* resource: string,
25+
* type?: string|null,
26+
* ignore_errors?: bool,
27+
* }>
28+
* @phpstan-type Parameters array<string, scalar|\UnitEnum|array<scalar|\UnitEnum|array|null>|null>
29+
* @phpstan-type Services array<string, Definition|Alias|Prototype|Stack>|array<class-string, Arguments|null>
30+
* @phpstan-type Defaults array{
31+
* public?: bool,
32+
* tags?: Tags,
33+
* resource_tags?: Tags,
34+
* autowire?: bool,
35+
* autoconfigure?: bool,
36+
* bind?: array<string, mixed>,
37+
* }
38+
* @phpstan-type Instanceof array{
39+
* shared?: bool,
40+
* lazy?: bool|string,
41+
* public?: bool,
42+
* properties?: array<string, mixed>,
43+
* configurator?: Callback,
44+
* calls?: list<Call>,
45+
* tags?: Tags,
46+
* resource_tags?: Tags,
47+
* autowire?: bool,
48+
* bind?: array<string, mixed>,
49+
* constructor?: string,
50+
* }
51+
* @phpstan-type Definition array{
52+
* class?: string,
53+
* file?: string,
54+
* parent?: string,
55+
* shared?: bool,
56+
* synthetic?: bool,
57+
* lazy?: bool|string,
58+
* public?: bool,
59+
* abstract?: bool,
60+
* deprecated?: Deprecation,
61+
* factory?: Callback,
62+
* configurator?: Callback,
63+
* arguments?: Arguments,
64+
* properties?: array<string, mixed>,
65+
* calls?: list<Call>,
66+
* tags?: Tags,
67+
* resource_tags?: Tags,
68+
* decorates?: string,
69+
* decoration_inner_name?: string,
70+
* decoration_priority?: int,
71+
* decoration_on_invalid?: 'exception'|'ignore'|null|ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE|ContainerInterface::IGNORE_ON_INVALID_REFERENCE|ContainerInterface::NULL_ON_INVALID_REFERENCE,
72+
* autowire?: bool,
73+
* autoconfigure?: bool,
74+
* bind?: array<string, mixed>,
75+
* constructor?: string,
76+
* from_callable?: mixed,
77+
* }
78+
* @phpstan-type Alias string|array{
79+
* alias: string,
80+
* public?: bool,
81+
* deprecated?: Deprecation,
82+
* }
83+
* @phpstan-type Prototype array{
84+
* resource: string,
85+
* namespace?: string,
86+
* exclude?: string|list<string>,
87+
* parent?: string,
88+
* shared?: bool,
89+
* lazy?: bool|string,
90+
* public?: bool,
91+
* abstract?: bool,
92+
* deprecated?: Deprecation,
93+
* factory?: Callback,
94+
* arguments?: Arguments,
95+
* properties?: array<string, mixed>,
96+
* configurator?: Callback,
97+
* calls?: list<Call>,
98+
* tags?: Tags,
99+
* resource_tags?: Tags,
100+
* autowire?: bool,
101+
* autoconfigure?: bool,
102+
* bind?: array<string, mixed>,
103+
* constructor?: string,
104+
* }
105+
* @phpstan-type Stack array{
106+
* stack: list<Definition|Alias|Prototype|array<class-string, Arguments|null>>,
107+
* public?: bool,
108+
* deprecated?: Deprecation,
109+
* }
110+
* @phpstan-type Arguments list<mixed>|array<string, mixed>
111+
* @phpstan-type Callback string|array{0:string|Reference|ReferenceConfigurator,1:string}|\Closure|Reference|ReferenceConfigurator|Expression
112+
* @phpstan-type Tags list<string|array<string, array<string, mixed>>>
113+
* @phpstan-type Deprecation array{package: string, version: string, message?: string}
114+
* @phpstan-type Call array<string, Arguments>|array{0:string, 1?:Arguments, 2?:bool}|array{method:string, arguments?:Arguments, returns_clone?:bool}
115+
*/
116+
class ServicesConfig
117+
{
118+
publicreadonlyarray$services;
119+
120+
/**
121+
* @param Services $services
122+
* @param Imports $imports
123+
* @param Parameters $parameters
124+
* @param Defaults $defaults
125+
* @param Instanceof $instanceof
126+
*/
127+
publicfunction__construct(
128+
array$services = [],
129+
publicreadonlyarray$imports = [],
130+
publicreadonlyarray$parameters = [],
131+
array$defaults = [],
132+
array$instanceof = [],
133+
) {
134+
if (isset($services['_defaults']) ||isset($services['_instanceof'])) {
135+
thrownewInvalidArgumentException('The $services argument should not contain "_defaults" or "_instanceof" keys, use the $defaults and $instanceof parameters instead.');
136+
}
137+
138+
$services['_defaults'] =$defaults;
139+
$services['_instanceof'] =$instanceof;
140+
$this->services =$services;
141+
}
142+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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\Config;
13+
14+
useSymfony\Component\Config\Loader\ParamConfigurator;
15+
useSymfony\Component\DependencyInjection\Argument\AbstractArgument;
16+
useSymfony\Component\DependencyInjection\Argument\IteratorArgument;
17+
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
18+
useSymfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
19+
useSymfony\Component\DependencyInjection\Definition;
20+
useSymfony\Component\DependencyInjection\Loader\Configurator\AbstractConfigurator;
21+
useSymfony\Component\DependencyInjection\Loader\Configurator\ClosureReferenceConfigurator;
22+
useSymfony\Component\DependencyInjection\Loader\Configurator\EnvConfigurator;
23+
useSymfony\Component\DependencyInjection\Loader\Configurator\InlineServiceConfigurator;
24+
useSymfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
25+
useSymfony\Component\ExpressionLanguage\Expression;
26+
27+
/**
28+
* Creates a parameter.
29+
*/
30+
functionparam(string$name):ParamConfigurator
31+
{
32+
returnnewParamConfigurator($name);
33+
}
34+
35+
/**
36+
* Creates a reference to a service.
37+
*/
38+
functionservice(string$serviceId):ReferenceConfigurator
39+
{
40+
returnnewReferenceConfigurator($serviceId);
41+
}
42+
43+
/**
44+
* Creates an inline service.
45+
*/
46+
functioninline_service(?string$class =null):InlineServiceConfigurator
47+
{
48+
returnnewInlineServiceConfigurator(newDefinition($class));
49+
}
50+
51+
/**
52+
* Creates a service locator.
53+
*
54+
* @param array<ReferenceConfigurator|InlineServiceConfigurator> $values
55+
*/
56+
functionservice_locator(array$values):ServiceLocatorArgument
57+
{
58+
$values = AbstractConfigurator::processValue($values,true);
59+
60+
returnnewServiceLocatorArgument($values);
61+
}
62+
63+
/**
64+
* Creates a lazy iterator.
65+
*
66+
* @param ReferenceConfigurator[] $values
67+
*/
68+
functioniterator(array$values):IteratorArgument
69+
{
70+
returnnewIteratorArgument(AbstractConfigurator::processValue($values,true));
71+
}
72+
73+
/**
74+
* Creates a lazy iterator by tag name.
75+
*/
76+
functiontagged_iterator(string$tag, ?string$indexAttribute =null, ?string$defaultIndexMethod =null, ?string$defaultPriorityMethod =null,string|array$exclude = [],bool$excludeSelf =true):TaggedIteratorArgument
77+
{
78+
returnnewTaggedIteratorArgument($tag,$indexAttribute,$defaultIndexMethod,false,$defaultPriorityMethod, (array)$exclude,$excludeSelf);
79+
}
80+
81+
/**
82+
* Creates a service locator by tag name.
83+
*/
84+
functiontagged_locator(string$tag, ?string$indexAttribute =null, ?string$defaultIndexMethod =null, ?string$defaultPriorityMethod =null,string|array$exclude = [],bool$excludeSelf =true):ServiceLocatorArgument
85+
{
86+
returnnewServiceLocatorArgument(newTaggedIteratorArgument($tag,$indexAttribute,$defaultIndexMethod,true,$defaultPriorityMethod, (array)$exclude,$excludeSelf));
87+
}
88+
89+
/**
90+
* Creates an expression.
91+
*/
92+
functionexpr(string$expression):Expression
93+
{
94+
returnnewExpression($expression);
95+
}
96+
97+
/**
98+
* Creates an abstract argument.
99+
*/
100+
functionabstract_arg(string$description):AbstractArgument
101+
{
102+
returnnewAbstractArgument($description);
103+
}
104+
105+
/**
106+
* Creates an environment variable reference.
107+
*/
108+
functionenv(string$name):EnvConfigurator
109+
{
110+
returnnewEnvConfigurator($name);
111+
}
112+
113+
/**
114+
* Creates a closure service reference.
115+
*/
116+
functionservice_closure(string$serviceId):ClosureReferenceConfigurator
117+
{
118+
returnnewClosureReferenceConfigurator($serviceId);
119+
}
120+
121+
/**
122+
* Creates a closure.
123+
*/
124+
functionclosure(string|array|\Closure|ReferenceConfigurator|Expression$callable):InlineServiceConfigurator
125+
{
126+
return (newInlineServiceConfigurator(newDefinition('Closure')))
127+
->factory(['Closure','fromCallable'])
128+
->args([$callable]);
129+
}

‎src/Symfony/Component/DependencyInjection/Loader/Configurator/AbstractConfigurator.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public static function processValue(mixed $value, bool $allowServices = false):
101101
case$valueinstanceof \UnitEnum:
102102
return$value;
103103

104+
case$valueinstanceof \Closure:
105+
returnself::processClosure($value);
106+
104107
case$valueinstanceof ArgumentInterface:
105108
case$valueinstanceof Definition:
106109
case$valueinstanceof Expression:
@@ -120,7 +123,7 @@ public static function processValue(mixed $value, bool $allowServices = false):
120123
*
121124
* @throws InvalidArgumentException if the closure is anonymous or references a non-static method
122125
*/
123-
finalpublicstaticfunctionprocessClosure(\Closure$closure):callable
126+
privatestaticfunctionprocessClosure(\Closure$closure):callable
124127
{
125128
$function =new \ReflectionFunction($closure);
126129
if ($function->isAnonymous()) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp