Grégory Salvan
unread,Nov 4, 2011, 5:16:08 AM11/4/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Symfony Components
hi, project depency injection is great and useful but I found a bit of
code that may be improved.
The method that create a service in the builder, instanciate a
reflection class that's not necessary and call two times the class
name resolution. So, in hope it help, below the replacement I
suggest :
/**
* Creates a service for a service definition.
*
* @param sfServiceDefinition $definition A service definition
instance
*
* @return object The service described by the service
definition
*/
protected function createService(sfServiceDefinition $definition)
{
if (null !== $definition->getFile())
{
require_once $this->resolveValue($definition->getFile());
}
$class = $this->resolveValue($definition->getClass());
$arguments = $this->resolveServices($this-
>resolveValue($definition->getArguments()));
if (null !== $definition->getConstructor())
{
$service = call_user_func_array(array($class, $definition-
>getConstructor()), $arguments);
}
else
{
$rClass = new ReflectionClass($class));
$service = null === $rClass->getConstructor() ? $rClass-
>newInstance() : $rClass->newInstanceArgs($arguments);
}
foreach ($definition->getMethodCalls() as $call)
{
call_user_func_array(array($service, $call[0]), $this-
>resolveServices($this->resolveValue($call[1])));
}
if ($callable = $definition->getConfigurator())
{
if (is_array($callable) && is_object($callable[0]) &&
$callable[0] instanceof sfServiceReference)
{
$callable[0] = $this->getService((string) $callable[0]);
}
elseif (is_array($callable))
{
$callable[0] = $this->resolveValue($callable[0]);
}
if (!is_callable($callable))
{
throw new InvalidArgumentException(sprintf('The configure
callable for class "%s" is not a callable.', get_class($service)));
}
call_user_func($callable, $service);
}
return $service;
}