Example #1
0
 public function testAnnotations()
 {
     $def = new Definition('stdClass');
     $this->assertEquals(spl_object_hash($def->addAnnotation('foo')), spl_object_hash($def), '->addAnnotation() implements a fluent interface');
     $this->assertEquals($def->getAnnotation('foo'), array(array()), '->getAnnotation() returns attributes for an annotation name');
     $def->addAnnotation('foo', array('foo' => 'bar'));
     $this->assertEquals($def->getAnnotation('foo'), array(array(), array('foo' => 'bar')), '->addAnnotation() can adds the same annotation several times');
     $def->addAnnotation('bar', array('bar' => 'bar'));
     $this->assertEquals($def->getAnnotations(), array('foo' => array(array(), array('foo' => 'bar')), 'bar' => array(array('bar' => 'bar'))), '->getAnnotations() returns all annotations');
 }
 protected function parseDefinition(BuilderConfiguration $configuration, $id, $service, $file)
 {
     if (is_string($service) && 0 === strpos($service, '@')) {
         $configuration->setAlias($id, substr($service, 1));
         return;
     }
     $definition = new Definition($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]));
         }
     }
     if (isset($service['annotations'])) {
         foreach ($service['annotations'] as $annotation) {
             $name = $annotation['name'];
             unset($annotation['name']);
             $definition->addAnnotation($name, $annotation);
         }
     }
     $configuration->setDefinition($id, $definition);
 }
Example #3
0
 protected function parseDefinition(BuilderConfiguration $configuration, $id, $service, $file)
 {
     if ((string) $service['alias']) {
         $configuration->setAlias($id, (string) $service['alias']);
         return;
     }
     $definition = new Definition((string) $service['class']);
     foreach (array('shared', 'factory-method', 'factory-service', 'factory-class') as $key) {
         if (isset($service[$key])) {
             $method = 'set' . str_replace('-', '', $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 Reference((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'));
     }
     foreach ($service->annotation as $annotation) {
         $parameters = array();
         foreach ($annotation->attributes() as $name => $value) {
             if ('name' === $name) {
                 continue;
             }
             $parameters[$name] = SimpleXMLElement::phpize($value);
         }
         $definition->addAnnotation((string) $annotation['name'], $parameters);
     }
     $configuration->setDefinition($id, $definition);
 }
Example #4
0
$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');
// ->getAnnotations() ->getAnnotation() ->addAnnotation()
$t->diag('->getAnnotations() ->getAnnotation() ->addAnnotation()');
$def = new Definition('stdClass');
$t->is(spl_object_hash($def->addAnnotation('foo')), spl_object_hash($def), '->addAnnotation() implements a fluent interface');
$t->is($def->getAnnotation('foo'), array(array()), '->getAnnotation() returns attributes for an annotation name');
$def->addAnnotation('foo', array('foo' => 'bar'));
$t->is($def->getAnnotation('foo'), array(array(), array('foo' => 'bar')), '->addAnnotation() can adds the same annotation several times');
$def->addAnnotation('bar', array('bar' => 'bar'));
$t->is($def->getAnnotations(), array('foo' => array(array(), array('foo' => 'bar')), 'bar' => array(array('bar' => 'bar'))), '->getAnnotations() returns all annotations');