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

Commit6ba0ba1

Browse files
[DI] Add "PHP fluent format" for configuring the container
1 parentfea348c commit6ba0ba1

14 files changed

+964
-2
lines changed

‎src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespaceSymfony\Component\DependencyInjection\Loader;
1313

14-
useSymfony\Component\Config\Resource\FileResource;
14+
require_once__DIR__.'/PhpFileLoader/functions.php';
15+
16+
useSymfony\Component\DependencyInjection\Definition;
1517

1618
/**
1719
* PhpFileLoader loads service definitions from a PHP file.
@@ -23,6 +25,8 @@
2325
*/
2426
class PhpFileLoaderextends FileLoader
2527
{
28+
private$defaults;
29+
2630
/**
2731
* {@inheritdoc}
2832
*/
@@ -35,8 +39,13 @@ public function load($resource, $type = null)
3539
$path =$this->locator->locate($resource);
3640
$this->setCurrentDir(dirname($path));
3741
$this->container->fileExists($path);
42+
$this->defaults =newDefinition();
3843

39-
include$path;
44+
try {
45+
include$path;
46+
}finally {
47+
$this->instanceof =array();
48+
}
4049
}
4150

4251
/**
@@ -54,4 +63,18 @@ public function supports($resource, $type = null)
5463

5564
return'php' ===$type;
5665
}
66+
67+
/**
68+
* @internal
69+
*/
70+
publicstaticfunctioncall(\Closure$callback)
71+
{
72+
$trace =debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT |DEBUG_BACKTRACE_IGNORE_ARGS,4);
73+
if (!isset($trace[3]['object']) || !$trace[3]['object']instanceof self) {
74+
thrownew \ErrorException(sprintf('Function "%s()" must be called in a "%s" context.',$trace[1]['function'],__CLASS__),0,E_ERROR,$trace[1]['file'],$trace[1]['line']);
75+
}
76+
$callback =$callback->bindTo($trace[3]['object'],__CLASS__);
77+
78+
return$callback($trace[3]['object']->defaults,$trace[1]['file']);
79+
}
5780
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\DependencyInjection\Loader\PhpFileLoader;
13+
14+
useSymfony\Component\DependencyInjection\Alias;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*
19+
* @method $this public()
20+
* @method $this private()
21+
*/
22+
class AliasConfigurator
23+
{
24+
private$alias;
25+
26+
publicfunction__construct(Alias$alias)
27+
{
28+
$this->alias =$alias;
29+
}
30+
31+
/**
32+
* @return $this
33+
*/
34+
publicfunction__call($method,$args)
35+
{
36+
if ('public' ===strtolower($method)) {
37+
$this->alias->setPublic(true);
38+
}elseif ('private' ===strtolower($method)) {
39+
$this->alias->setPublic(false);
40+
}else {
41+
thrownew \BadMethodCallException(sprintf('Call to undefined method %s::%s()',get_class($this),$method));
42+
}
43+
44+
return$this;
45+
}
46+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*
17+
* @method $this class()
18+
*/
19+
class ChildDefinitionConfiguratorextendsInternal\AbstractPrototypeConfigurator
20+
{
21+
/**
22+
* Sets the service that this service is decorating.
23+
*
24+
* @param null|string $id The decorated service id, use null to remove decoration
25+
* @param null|string $renamedId The new decorated service id
26+
* @param int $priority The priority of decoration
27+
*
28+
* @return $this
29+
*
30+
* @throws InvalidArgumentException In case the decorated service id and the new decorated service id are equals.
31+
*/
32+
publicfunctiondecorate($id,$renamedId =null,$priority =0)
33+
{
34+
$this->definition->setDecoratedService($id,$renamedId,$priority);
35+
36+
return$this;
37+
}
38+
39+
/**
40+
* @return $this
41+
*/
42+
publicfunction__call($method,$args)
43+
{
44+
if ('class' !==strtolower($method)) {
45+
returnparent::__call($method,$args);
46+
}
47+
if (!array_key_exists(0,$args)) {
48+
thrownew \BadMethodCallException(sprintf('Missing argument 1 when calling method %s::%s()',get_class($this),$method));
49+
}
50+
$this->definition->setClass($args[0]);
51+
52+
return$this;
53+
}
54+
55+
/**
56+
* Sets a file to require before creating the service.
57+
*
58+
* @param string $file A full pathname to include
59+
*
60+
* @return $this
61+
*/
62+
publicfunctionfile($file)
63+
{
64+
$this->definition->setFile($file);
65+
66+
return$this;
67+
}
68+
69+
/**
70+
* Sets whether this definition is synthetic, that is not constructed by the
71+
* container, but dynamically injected.
72+
*
73+
* @param bool $synthetic
74+
*
75+
* @return $this
76+
*/
77+
publicfunctionsynthetic($synthetic =true)
78+
{
79+
$this->definition->setSynthetic($synthetic);
80+
81+
return$this;
82+
}
83+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class DefaultsConfiguratorextendsInternal\AbstractDefaultsConfigurator
18+
{
19+
/**
20+
* Sets whether or not instanceof conditionals should be prepended with a global set.
21+
*
22+
* @return $this
23+
*/
24+
publicfunctionautoconfigure($autoconfigured =true)
25+
{
26+
$this->definition->setAutoconfigured($autoconfigured);
27+
28+
return$this;
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class DefinitionConfiguratorextends ChildDefinitionConfigurator
18+
{
19+
/**
20+
* Sets whether or not instanceof conditionals should be prepended with a global set.
21+
*
22+
* @return $this
23+
*/
24+
publicfunctionautoconfigure($autoconfigured =true)
25+
{
26+
$this->definition->setAutoconfigured($autoconfigured);
27+
28+
return$this;
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class InstanceofConfiguratorextendsInternal\AbstractInstanceofConfigurator
18+
{
19+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\DependencyInjection\Loader\PhpFileLoader\Internal;
13+
14+
useSymfony\Component\DependencyInjection\Definition;
15+
useSymfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*
20+
* @method $this public()
21+
* @method $this private()
22+
*
23+
* @internal
24+
*/
25+
abstractclass AbstractDefaultsConfigurator
26+
{
27+
protected$definition;
28+
29+
publicfunction__construct(Definition$definition)
30+
{
31+
$this->definition =$definition;
32+
}
33+
34+
/**
35+
* Adds a tag for this definition.
36+
*
37+
* @param string $name The tag name
38+
* @param array $attributes An array of attributes
39+
*
40+
* @return $this
41+
*/
42+
publicfunctiontag($name,array$attributes =array())
43+
{
44+
if (!is_string($name) ||'' ===$name) {
45+
thrownewInvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string.'));
46+
}
47+
48+
foreach ($attributesas$attribute =>$value) {
49+
if (!is_scalar($value) &&null !==$value) {
50+
thrownewInvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type.',$name,$attribute));
51+
}
52+
}
53+
54+
$this->definition->addTag($name,$attributes);
55+
56+
return$this;
57+
}
58+
59+
/**
60+
* @return $this
61+
*/
62+
publicfunction__call($method,$args)
63+
{
64+
if ('public' ===strtolower($method)) {
65+
$this->definition->setPublic(true);
66+
}elseif ('private' ===strtolower($method)) {
67+
$this->definition->setPublic(false);
68+
}else {
69+
thrownew \BadMethodCallException(sprintf('Call to undefined method %s::%s()',get_class($this),$method));
70+
}
71+
72+
return$this;
73+
}
74+
75+
/**
76+
* Enables/disables autowiring.
77+
*
78+
* @param bool $autowired
79+
*
80+
* @return $this
81+
*/
82+
publicfunctionautowire($autowired =true)
83+
{
84+
$this->definition->setAutowired($autowired);
85+
86+
return$this;
87+
}
88+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp