-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Introduction of syntax/shortcut aware Definition #15800
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\DependencyInjection\SyntaxAware; | ||
|
||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* A ContainerBuilder that facilitates using the shortcut (e.g. @service_name) syntax. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
class SyntaxAwareContainerBuilder extends ContainerBuilder | ||
{ | ||
public function __construct(ParameterBagInterface $parameterBag = null) | ||
{ | ||
parent::__construct($parameterBag); | ||
|
||
$this->parameterBag = new SyntaxAwareParameterBag($this->parameterBag); | ||
} | ||
|
||
protected function createDefinition($class) | ||
{ | ||
return new SyntaxAwareDefinition($class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?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\DependencyInjection\SyntaxAware; | ||
|
||
use Symfony\Component\ExpressionLanguage\Expression; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* A definition class that is aware of shortcut syntaxes referring to services and expressions. | ||
* | ||
* There are two main shortcuts that are available whenever it makes sense: | ||
* | ||
* A) Shortcut for referencing services: | ||
* - @logger becomes a Reference to logger | ||
* - @?logger becomes an optional Reference to logger | ||
* | ||
* B) Shortcut for creating Expressions: | ||
* - @=service("foo") becomes an Expression('service("foo")') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is missing the fact that arguments containing a string starting with |
||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
class SyntaxAwareDefinition extends Definition | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct($class = null, array $arguments = array()) | ||
{ | ||
$arguments = $this->resolveServices($arguments); | ||
|
||
parent::__construct($class, $arguments); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setArguments(array $arguments) | ||
{ | ||
$arguments = $this->resolveServices($arguments); | ||
|
||
return parent::setArguments($arguments); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addArgument($argument) | ||
{ | ||
$argument = $this->resolveServices($argument); | ||
|
||
return parent::addArgument($argument); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function replaceArgument($index, $argument) | ||
{ | ||
$argument = $this->resolveServices($argument); | ||
|
||
return parent::replaceArgument($index, $argument); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setProperties(array $properties) | ||
{ | ||
$properties = $this->resolveServices($properties); | ||
|
||
return parent::setProperties($properties); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setProperty($name, $value) | ||
{ | ||
$value = $this->resolveServices($value); | ||
|
||
return parent::setProperty($name, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setFactory($factory) | ||
{ | ||
if (is_string($factory)) { | ||
if (strpos($factory, ':') !== false && strpos($factory, '::') === false) { | ||
$parts = explode(':', $factory); | ||
$factory = array($this->resolveServices('@'.$parts[0]), $parts[1]); | ||
} | ||
} else { | ||
$factory = array($this->resolveServices($factory[0]), $factory[1]); | ||
} | ||
|
||
return parent::setFactory($factory); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setConfigurator($callable) | ||
{ | ||
if (is_array($callable)) { | ||
$callable = array($this->resolveServices($callable[0]), $callable[1]); | ||
} | ||
|
||
return parent::setConfigurator($callable); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addMethodCall($method, array $arguments = array()) | ||
{ | ||
$arguments = $this->resolveServices($arguments); | ||
|
||
return parent::addMethodCall($method, $arguments); | ||
} | ||
|
||
/** | ||
* Resolves services. | ||
* | ||
* @param string|array $value | ||
* | ||
* @return array|string|Reference | ||
*/ | ||
private function resolveServices($value) | ||
{ | ||
if (is_array($value)) { | ||
$value = array_map(array('self', 'resolveServices'), $value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use a return here directly, which would allow to use |
||
} elseif (is_string($value) && 0 === strpos($value, '@=')) { | ||
return new Expression(substr($value, 2)); | ||
} elseif (is_string($value) && 0 === strpos($value, '@')) { | ||
if (0 === strpos($value, '@@')) { | ||
$value = substr($value, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use a return here directly instead of doing this weird logic with |
||
$invalidBehavior = null; | ||
} elseif (0 === strpos($value, '@?')) { | ||
$value = substr($value, 2); | ||
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; | ||
} else { | ||
$value = substr($value, 1); | ||
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; | ||
} | ||
|
||
if ('=' === substr($value, -1)) { | ||
$value = substr($value, 0, -1); | ||
$strict = false; | ||
} else { | ||
$strict = true; | ||
} | ||
|
||
if (null !== $invalidBehavior) { | ||
$value = new Reference($value, $invalidBehavior, $strict); | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?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\DependencyInjection\SyntaxAware; | ||
|
||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; | ||
use Symfony\Component\ExpressionLanguage\Expression; | ||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; | ||
|
||
/** | ||
* A decorator that makes parameters able to use the @= syntax for parameters. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
class SyntaxAwareParameterBag implements ParameterBagInterface | ||
{ | ||
/** | ||
* @var ParameterBagInterface | ||
*/ | ||
private $parameterBag; | ||
|
||
public function __construct(ParameterBagInterface $parameterBag) | ||
{ | ||
$this->parameterBag = $parameterBag; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
$this->parameterBag->clear(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function add(array $parameters) | ||
{ | ||
$parameters = $this->resolveExpressions($parameters); | ||
|
||
$this->parameterBag->add($parameters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function all() | ||
{ | ||
return $this->parameterBag->all(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($name) | ||
{ | ||
return $this->parameterBag->get($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($name, $value) | ||
{ | ||
$value = $this->resolveExpressions($value); | ||
|
||
$this->parameterBag->set($name, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($name) | ||
{ | ||
return $this->parameterBag->has($name); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve() | ||
{ | ||
$this->parameterBag->resolve(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveValue($value) | ||
{ | ||
return $this->parameterBag->resolveValue($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function escapeValue($value) | ||
{ | ||
return $this->parameterBag->escapeValue($value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unescapeValue($value) | ||
{ | ||
return $this->parameterBag->unescapeValue($value); | ||
} | ||
|
||
/** | ||
* @return ParameterBagInterface | ||
*/ | ||
public function getParameterBag() | ||
{ | ||
return $this->parameterBag; | ||
} | ||
|
||
private function resolveExpressions($value) | ||
{ | ||
if (is_array($value)) { | ||
$value = array_map(array('self', 'resolveExpressions'), $value); | ||
} elseif (is_string($value) && 0 === strpos($value, '@=')) { | ||
return new Expression(substr($value, 2)); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is missing the description of
=
at the end of service references