コード例 #1
0
/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require_once dirname(__FILE__) . '/../../bootstrap/unit.php';
$t = new lime_test(21);
// __construct()
$t->diag('__construct()');
$def = new sfServiceDefinition('stdClass');
$t->is($def->getClass(), 'stdClass', '__construct() takes the class name as its first argument');
$def = new sfServiceDefinition('stdClass', array('foo'));
$t->is($def->getArguments(), array('foo'), '__construct() takes an optional array of arguments as its second argument');
// ->setConstructor() ->getConstructor()
$t->diag('->setConstructor() ->getConstructor()');
$def = new sfServiceDefinition('stdClass');
$t->is(spl_object_hash($def->setConstructor('foo')), spl_object_hash($def), '->setConstructor() implements a fluent interface');
$t->is($def->getConstructor(), 'foo', '->getConstructor() returns the constructor name');
// ->setClass() ->getClass()
$t->diag('->setClass() ->getClass()');
$def = new sfServiceDefinition('stdClass');
$t->is(spl_object_hash($def->setClass('foo')), spl_object_hash($def), '->setClass() implements a fluent interface');
$t->is($def->getClass(), 'foo', '->getClass() returns the class name');
// ->setArguments() ->getArguments() ->addArgument()
$t->diag('->setArguments() ->getArguments() ->addArgument()');
$def = new sfServiceDefinition('stdClass');
$t->is(spl_object_hash($def->setArguments(array('foo'))), spl_object_hash($def), '->setArguments() implements a fluent interface');
$t->is($def->getArguments(), array('foo'), '->getArguments() returns the arguments');
コード例 #2
0
 /**
  * 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());
     }
     $r = new ReflectionClass($this->resolveValue($definition->getClass()));
     $arguments = $this->resolveServices($this->resolveValue($definition->getArguments()));
     if (null !== $definition->getConstructor()) {
         $service = call_user_func_array(array($this->resolveValue($definition->getClass()), $definition->getConstructor()), $arguments);
     } else {
         $service = null === $r->getConstructor() ? $r->newInstance() : $r->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;
 }