8000 [DI] Add "PHP fluent format" for configuring the container · symfony/symfony@1d9f16f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d9f16f

Browse files
[DI] Add "PHP fluent format" for configuring the container
1 parent fea348c commit 1d9f16f

15 files changed

+980
-2
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader;
1313

14-
use Symfony\Component\Config\Resource\FileResource;
14+
require_once __DIR__.'/PhpFileLoader/functions.php';
15+
16+
use Symfony\Component\DependencyInjection\Definition;
1517

1618
/**
1719
* PhpFileLoader loads service definitions from a PHP file.
@@ -23,6 +25,8 @@
2325
*/
2426
class PhpFileLoader extends FileLoader
2527
{
28+
private $defaults;
29+
2630
/**
2731
* {@inheritdoc}
2832
*/
@@ -35,6 +39,8 @@ 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 = new Definition();
43+
$this->instanceof = array();
3844

3945
include $path;
4046
}
@@ -54,4 +60,18 @@ public function supports($resource, $type = null)
5460

5561
return 'php' === $type;
5662
}
63+
64+
/**
65+
* @internal
66+
*/
67+
public static function call(\Closure $callback)
68+
{
69+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 4);
70+
if (!isset($trace[3]['object']) || !$trace[3]['object'] instanceof self) {
71+
throw new \ErrorException(sprintf('Function "%s()" must be called in a "%s" context.', $trace[1]['function'], __CLASS__), 0, E_ERROR, $trace[1]['file'], $trace[1]['line']);
72+
}
73+
$callback = $callback->bindTo($trace[3]['object'], __CLASS__);
74+
75+
return $callback($trace[3]['object']->defaults, $trace[1]['file']);
76+
}
5777
}
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+
namespace Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
13+
14+
use Symfony\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+
public function __construct(Alias $alias)
27+
{
28+
$this->alias = $alias;
29+
}
30+
31+
/**
32+
* @return $this
33+
*/
34+
public function __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+
throw new \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+
namespace Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*
17+
* @method $this class()
18+
*/
19+
class ChildDefinitionConfigurator extends Internal\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+
public function decorate($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+
public function __call($method, $args)
43+
{
44+
if ('class' !== strtolower($method)) {
45+
return parent::__call($method, $args);
46+
}
47+
if (!array_key_exists(0, $args)) {
48+
throw new \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+
public function file($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+
public function synthetic($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+
namespace Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class DefaultsConfigurator extends Internal\AbstractDefaultsConfigurator
18+
{
19+
/**
20+
* Sets whether or not instanceof conditionals should be prepended with a global set.
21+
*
22+
* @return $this
23+
*/
24+
public function autoconfigure($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+
namespace Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class DefinitionConfigurator extends ChildDefinitionConfigurator
18+
{
19+
/**
20+
* Sets whether or not instanceof conditionals should be prepended with a global set.
21+
*
22+
* @return $this
23+
*/
24+
public function autoconfigure($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+
namespace Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
class InstanceofConfigurator extends Internal\AbstractInstanceofConfigurator
18+
{
19+
}
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+
namespace Symfony\Component\DependencyInjection\Loader\PhpFileLoader\Internal;
13+
14+
use Symfony\Component\DependencyInjection\Definition;
15+
use Symfony\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+
abstract class AbstractDefaultsConfigurator
26+
{
27+
protected $definition;
28+
29+
public function __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+
public function tag($name, array $attributes = array())
43+
{
44+
if (!is_string($name) || '' === $name) {
45+
throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string.'));
46+
}
47+
48+
foreach ($attributes as $attribute => $value) {
49+
if (!is_scalar($value) && null !== $value) {
50+
throw new InvalidArgumentException(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+
public function __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+
throw new \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+
public function autowire($autowired = true)
83+
{
84+
$this->definition->setAutowired($autowired);
85+
86+
return $this;
87+
}
88+
}

0 commit comments

Comments
 (0)
0