Example #1
0
 public function testConstructor()
 {
     $def = new Definition('stdClass');
     $this->assertEquals($def->getClass(), 'stdClass', '__construct() takes the class name as its first argument');
     $def = new Definition('stdClass', array('foo'));
     $this->assertEquals($def->getArguments(), array('foo'), '__construct() takes an optional array of arguments as its second argument');
 }
<?php

/*
 * 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 __DIR__ . '/../../../bootstrap.php';
use Symfony\Components\DependencyInjection\Definition;
$t = new LimeTest(21);
// __construct()
$t->diag('__construct()');
$def = new Definition('stdClass');
$t->is($def->getClass(), 'stdClass', '__construct() takes the class name as its first argument');
$def = new Definition('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 Definition('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 Definition('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 Definition('stdClass');
Example #3
0
 /**
  * Creates a service for a service definition.
  *
  * @param  Definition $definition A service definition instance
  * @param  string     $id         The service identifier
  *
  * @return object              The service described by the service definition
  *
  * @throws \InvalidArgumentException When configure callable is not callable
  */
 protected function createService(Definition $definition, $id)
 {
     if (null !== $definition->getFile()) {
         require_once $this->getParameterBag()->resolveValue($definition->getFile());
     }
     $arguments = $this->resolveServices($this->getParameterBag()->resolveValue($definition->getArguments()));
     if (null !== $definition->getFactoryMethod()) {
         if (null !== $definition->getFactoryService()) {
             $factory = $this->get($this->getParameterBag()->resolveValue($definition->getFactoryService()));
         } else {
             $factory = $this->getParameterBag()->resolveValue($definition->getClass());
         }
         $service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
     } else {
         $r = new \ReflectionClass($this->getParameterBag()->resolveValue($definition->getClass()));
         $service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
     }
     if ($definition->isShared()) {
         $this->services[$id] = $service;
     }
     foreach ($definition->getMethodCalls() as $call) {
         $services = self::getServiceConditionals($call[1]);
         $ok = true;
         foreach ($services as $s) {
             if (!$this->has($s)) {
                 $ok = false;
                 break;
             }
         }
         if ($ok) {
             call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
         }
     }
     if ($callable = $definition->getConfigurator()) {
         if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
             $callable[0] = $this->get((string) $callable[0]);
         } elseif (is_array($callable)) {
             $callable[0] = $this->getParameterBag()->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;
 }