Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ public function getAlias($id)
*/
public function register($id, $class = null)
{
return $this->setDefinition($id, new Definition($class));
return $this->setDefinition($id, $this->createDefinition($class));
}

/**
Expand Down Expand Up @@ -1120,6 +1120,17 @@ public static function getServiceConditionals($value)
return $services;
}

/**
* Creates a new Definition used by register().
*
* @param string $class
* @return Definition
*/
protected function createDefinition($class)
{
return new Definition($class);
}

/**
* Retrieves the currently set proxy instantiator or instantiates one.
*
Expand Down
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
Copy link
Member

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

*
* B) Shortcut for creating Expressions:
* - @=service("foo") becomes an Expression('service("foo")')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is missing the fact that arguments containing a string starting with @ need to escape it by doubling it (which makes this class violate OOP rules btw, as the behavior of addArgument('@Stof70') would change totally depending of the Definition class used to build the service

*
* @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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a return here directly, which would allow to use if rather than elseif for next cases

} 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);
Copy link
Member

Choose a reason for hiding this comment

The 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 to detect that it should not be a reference

$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;
}
}
Loading