Esempio n. 1
0
 private function parseDefinition($id, $service)
 {
     $definition = new Definition($id);
     if (isset($service['synthetic'])) {
         $definition->setSynthetic($service['synthetic']);
     }
     if (isset($service['class'])) {
         $definition->setClass($service['class']);
     }
     if (isset($service['scope'])) {
         $definition->setScope($service['scope']);
     }
     if (isset($service['lazy'])) {
         $definition->setLazy($service['lazy']);
     }
     if (isset($service['arguments'])) {
         $definition->setArguments($service['arguments']);
     }
     if (isset($service['calls'])) {
         $definition->addCalls($service['calls']);
     }
     if (isset($service['configurator'])) {
         $definition->addConfigurator($service['configurator']);
     }
     if (isset($service['factory'])) {
         $definition->setFactory($service['factory']);
     }
     if (isset($service['tags'])) {
         $definition->setTags($service['tags']);
     }
     if (isset($service['aspects'])) {
         $definition->setAspects($service['aspects']);
     }
     return $definition;
 }
 public function testDefinition()
 {
     $definition = new Definition();
     $definition->setClass('Foo');
     $definition->setArguments([1, 2, 3]);
     $this->assertEquals('Foo', $definition->getClass());
     $this->assertEquals([1, 2, 3], $definition->getArguments());
 }
Esempio n. 3
0
 public function create(Definition $serviceConf, \Pimple $container)
 {
     $serviceName = $serviceConf->getName();
     $factoryFunction = null;
     if (!$serviceConf->isSynthetic()) {
         // we dont know how to create a synthetic service, its set later
         // the class name can be a parameter reference
         $serviceConf->setClass($this->normalize($serviceConf->getClass(), $container));
         $that = $this;
         $aspectFactory = $this->aspectFactory;
         $shared = $this->shared;
         // the instantiator closure function
         $factoryFunction = function ($c) use($that, $serviceConf, $aspectFactory, &$shared) {
             $serviceName = $serviceConf->getName();
             if (!empty($shared[$serviceName])) {
                 return $shared[$serviceName];
             }
             $instance = $that->createInstance($serviceConf, $c);
             // add aspects
             if (null !== $aspectFactory && $serviceConf->hasAspects()) {
                 $instance = $that->addAspects($serviceConf->getAspects(), $instance, $c);
             }
             // add some method calls
             if ($serviceConf->hasCalls()) {
                 $instance = $that->addMethodCalls($serviceConf->getCalls(), $instance, $c);
             }
             // let another object modify this instance
             if ($serviceConf->hasConfigurators()) {
                 $instance = $that->addConfigurators($serviceConf->getConfigurators(), $instance, $c);
             }
             // if the service is a shared instance we save it for later use, this is default
             if (true === $serviceConf->isShared()) {
                 $shared[$serviceName] = $instance;
             }
             return $instance;
         };
         if ($serviceConf->hasTags()) {
             $tags = $serviceConf->getTags();
             /** @var TagHandlerInterface $handler */
             foreach ($this->tagHandlers as $handler) {
                 $handler->process($serviceConf, $tags, $container);
             }
         }
         // create a lazy proxy
         if (null !== $this->proxyFactory && $serviceConf->isLazy()) {
             $factoryFunction = $this->proxyFactory->createProxy($serviceConf->getClass(), $factoryFunction);
         }
     }
     $container[$serviceName] = $factoryFunction;
     return $container;
 }
Esempio n. 4
0
 public function testDefinition()
 {
     $definition = new Definition('Bar');
     $definition->setClass('Foo');
     static::assertEquals('Foo', $definition->getClass());
     $arguments = array(1, 2, 3);
     static::assertFalse($definition->hasArguments());
     $definition->setArguments($arguments);
     static::assertTrue($definition->hasArguments());
     static::assertEquals($arguments, $definition->getArguments());
     $calls = array(array('method', array(1, 2)));
     static::assertFalse($definition->hasCalls());
     $definition->addCalls($calls);
     static::assertTrue($definition->hasCalls());
     static::assertEquals($calls, $definition->getCalls());
 }
 private function parseDefinition($id, $service)
 {
     $definition = new Definition();
     if (isset($service['class'])) {
         $definition->setClass($service['class']);
     }
     if (isset($service['arguments'])) {
         $definition->setArguments($this->resolveServices($service['arguments']));
     }
     $this->container['services'][$id] = $definition;
 }