improvement suggest for sfServiceContainerBuilder createService method

5 views
Skip to first unread message

Grégory Salvan

unread,
Nov 4, 2011, 5:16:08 AM11/4/11
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;
}
Reply all
Reply to author
Forward
0 new messages