コード例 #1
0
 /**
  * Get service.
  *
  * @param string $id
  * @param array  $args
  * @param int    $invalidBehavior
  *
  * @return mixed|object|null
  */
 public function get($id, array $args = array(), $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
 {
     if (null !== ($service = parent::get($id, $args, self::NULL_ON_INVALID_REFERENCE))) {
         return $service;
     }
     $id = $this->getServiceId($id);
     try {
         $definition = $this->getDefinition($id);
     } catch (InvalidArgumentException $e) {
         if (self::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
             return null;
         }
         throw $e;
     }
     return $this->registerDefinition($id, $definition, $args);
 }
コード例 #2
0
 public function testAutomaticResolution()
 {
     $container = new Container();
     $container->getDi()->getInjector()->resolveUnknownClass(true);
     $container->getParameterBag()->add(array('status' => 'offline', 'message' => 'Hits', 'counter' => new Reference('service')));
     // Define singleton service
     $container->share('service', function () {
         return new Counter();
     });
     $container->set('view', function (CounterComponent $view, $status) {
         return sprintf('Show - %s', $view->display($status));
     });
     $this->assertEquals('Show - Hits: 1. offline', $container->get('view'));
     $this->assertEquals('Show - Hits: 2. offline', $container->get('view'));
     $this->assertEquals('Show - Hits: 3. status offline', $container->get('view', array('status' => 'status %status%')));
 }