public function testSetIsShared() { $def = new Definition('stdClass'); $this->assertEquals($def->isShared(), true, '->isShared() returns true by default'); $this->assertEquals(spl_object_hash($def->setShared(false)), spl_object_hash($def), '->setShared() implements a fluent interface'); $this->assertEquals($def->isShared(), false, '->isShared() returns false if the instance must not be shared'); }
$t->is($def->getClass(), 'foo', '->getClass() returns the class name'); // ->setArguments() ->getArguments() ->addArgument() $t->diag('->setArguments() ->getArguments() ->addArgument()'); $def = new Definition('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'); $t->is(spl_object_hash($def->addArgument('bar')), spl_object_hash($def), '->addArgument() implements a fluent interface'); $t->is($def->getArguments(), array('foo', 'bar'), '->addArgument() adds an argument'); // ->setMethodCalls() ->getMethodCalls() ->addMethodCall() $t->diag('->setMethodCalls() ->getMethodCalls() ->addMethodCall()'); $def = new Definition('stdClass'); $t->is(spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), spl_object_hash($def), '->setMethodCalls() implements a fluent interface'); $t->is($def->getMethodCalls(), array(array('foo', array('foo'))), '->getMethodCalls() returns the methods to call'); $t->is(spl_object_hash($def->addMethodCall('bar', array('bar'))), spl_object_hash($def), '->addMethodCall() implements a fluent interface'); $t->is($def->getMethodCalls(), array(array('foo', array('foo')), array('bar', array('bar'))), '->addMethodCall() adds a method to call'); // ->setFile() ->getFile() $t->diag('->setFile() ->getFile()'); $def = new Definition('stdClass'); $t->is(spl_object_hash($def->setFile('foo')), spl_object_hash($def), '->setFile() implements a fluent interface'); $t->is($def->getFile(), 'foo', '->getFile() returns the file to include'); // ->setShared() ->isShared() $t->diag('->setShared() ->isShared()'); $def = new Definition('stdClass'); $t->is($def->isShared(), true, '->isShared() returns true by default'); $t->is(spl_object_hash($def->setShared(false)), spl_object_hash($def), '->setShared() implements a fluent interface'); $t->is($def->isShared(), false, '->isShared() returns false if the instance must not be shared'); // ->setConfigurator() ->getConfigurator() $t->diag('->setConfigurator() ->getConfigurator()'); $def = new Definition('stdClass'); $t->is(spl_object_hash($def->setConfigurator('foo')), spl_object_hash($def), '->setConfigurator() implements a fluent interface'); $t->is($def->getConfigurator(), 'foo', '->getConfigurator() returns the configurator');
/** * 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; }