protected function parseDefinition($service)
 {
     if (is_string($service) && 0 === strpos($service, '@')) {
         return substr($service, 1);
     }
     $definition = new sfServiceDefinition($service['class']);
     if (isset($service['shared'])) {
         $definition->setShared($service['shared']);
     }
     if (isset($service['constructor'])) {
         $definition->setConstructor($service['constructor']);
     }
     if (isset($service['file'])) {
         $definition->setFile($service['file']);
     }
     if (isset($service['arguments'])) {
         $definition->setArguments($this->resolveServices($service['arguments']));
     }
     if (isset($service['configurator'])) {
         if (is_string($service['configurator'])) {
             $definition->setConfigurator($service['configurator']);
         } else {
             $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
         }
     }
     if (isset($service['calls'])) {
         foreach ($service['calls'] as $call) {
             $definition->addMethodCall($call[0], $this->resolveServices($call[1]));
         }
     }
     return $definition;
 }
$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');
$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 sfServiceDefinition('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 sfServiceDefinition('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 sfServiceDefinition('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 sfServiceDefinition('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');
Exemplo n.º 3
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;
 }
 protected function parseDefinition($service, $file)
 {
     if ((string) $service['alias']) {
         return (string) $service['alias'];
     }
     $definition = new sfServiceDefinition((string) $service['class']);
     foreach (array('shared', 'constructor') as $key) {
         $method = 'set' . ucfirst($key);
         if (isset($service[$key])) {
             $definition->{$method}((string) $service->getAttributeAsPhp($key));
         }
     }
     if ($service->file) {
         $definition->setFile((string) $service->file);
     }
     $definition->setArguments($service->getArgumentsAsPhp('argument'));
     if (isset($service->configurator)) {
         if (isset($service->configurator['function'])) {
             $definition->setConfigurator((string) $service->configurator['function']);
         } else {
             if (isset($service->configurator['service'])) {
                 $class = new sfServiceReference((string) $service->configurator['service']);
             } else {
                 $class = (string) $service->configurator['class'];
             }
             $definition->setConfigurator(array($class, (string) $service->configurator['method']));
         }
     }
     foreach ($service->call as $call) {
         $definition->addMethodCall((string) $call['method'], $call->getArgumentsAsPhp('argument'));
     }
     return $definition;
 }