Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function __invoke($event, &$dispatched = NULL, EventParamResolverInterface $resolver = NULL)
 {
     $object = $this->container->getBound($this->binding);
     if ($object instanceof ScopedProxyInterface) {
         if ($this->lazy && !$object->K2IsProxyBound()) {
             return;
         }
         $object = $object->K2UnwrapScopedProxy();
     }
     if (!method_exists($object, $this->methodName)) {
         throw new \BadMethodCallException(sprintf('Method "%s" is not defined in %s', $this->methodName, get_class($object)));
     }
     if (!is_callable([$object, $this->methodName])) {
         throw new \BadMethodCallException(sprintf('%s->%s() is not callable from event listener', get_class($object), $this->methodName));
     }
     $dispatched = true;
     if ($resolver === NULL) {
         return $object->{$this->methodName}($event);
     }
     $args = array_merge([$event], $this->loadArguments(new \ReflectionMethod(get_class($object), $this->methodName), $resolver));
     switch (count($args)) {
         case 1:
             return $object->{$this->methodName}($args[0]);
         case 2:
             return $object->{$this->methodName}($args[0], $args[1]);
         case 3:
             return $object->{$this->methodName}($args[0], $args[1], $args[2]);
         case 4:
             return $object->{$this->methodName}($args[0], $args[1], $args[2], $args[3]);
         case 5:
             return $object->{$this->methodName}($args[0], $args[1], $args[2], $args[3], $args[4]);
     }
     return call_user_func_array([$object, $this->methodName], $args);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function getService($name)
 {
     if ($this->shared) {
         if (empty($this->bound[$name])) {
             if (empty($this->bindings[$name])) {
                 throw new ServiceNotFoundException(sprintf('Service "%s" is not registered', $name));
             }
             $this->bound[$name] = $this->container->getBound($this->bindings[$name]);
         }
         return $this->bound[$name];
     }
     if (empty($this->bindings[$name])) {
         throw new ServiceNotFoundException(sprintf('Service "%s" is not registered', $name));
     }
     return $this->container->getBound($this->bindings[$name]);
 }
Esempio n. 3
0
 /**
  * Bind initial object instances to the DI container.
  */
 protected function bindInitialInstances(ContainerInterface $container)
 {
     foreach ($this->komponents as $komponent) {
         $container->bindInstance(get_class($komponent), $komponent);
     }
 }
Esempio n. 4
0
 public function performInitialization(ContainerInterface $container)
 {
     $this->container = $container;
     $this->config = $container->get(Configuration::class);
     $this->eventDispatcher = $container->get(EventDispatcherInterface::class);
     foreach ($this->findRules() as $rule) {
         $rule->bind($this->testKernel, $this->container);
     }
     static $injects = [];
     $typeName = get_class($this);
     if (!array_key_exists($typeName, $injects)) {
         $injects[$typeName] = [];
         $ref = new \ReflectionClass(get_class($this));
         foreach ($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
             if ($method->isStatic() || $method->isAbstract()) {
                 continue;
             }
             if (0 === stripos($method->name, 'inject')) {
                 $params = (array) $method->getParameters();
                 if (empty($params)) {
                     throw new \RuntimeException(sprintf('Missing injected type param in %s->%s()', $typeName, $method->name));
                 }
                 if (count($params) > 1) {
                     throw new \RuntimeException(sprintf('Unable to inject more than 1 type into %s->%s()', $typeName, $method->name));
                 }
                 $type = $this->getParamType($params[0]);
                 if ($type === NULL) {
                     throw new \RuntimeException(sprintf('Missing type hint for injection param "%s" in %s->%s()', $params[0]->name, $typeName, $method->name));
                 }
                 if (!class_exists($type) && !interface_exists($type, false)) {
                     if ($params[0]->isOptional()) {
                         continue;
                     }
                     throw new \RuntimeException(sprintf('Missing dependency %s in injection method %s->%s()', $type, $typeName, $method->name));
                 }
                 $injects[$typeName][$method->name] = $type;
             }
         }
     }
     foreach ($injects[$typeName] as $k => $v) {
         $ref = new \ReflectionMethod(get_class($this), $k);
         $point = new InjectionPoint($ref->getDeclaringClass()->name, $ref->name);
         $this->{$k}($this->container->get($v, $point));
     }
 }
Esempio n. 5
0
 public function createView(Configuration $config, ContainerInterface $container)
 {
     $view = new View();
     $container->eachMarked(function (ViewRendererFactory $factory, BindingInterface $binding) use($view, $container) {
         $view->registerFactory($container->getBound($binding));
     });
     return $view;
 }
 public function bootRestResources(RouteCollector $collector, ContainerInterface $container)
 {
     $container->eachMarked(function (RestResource $resource, BindingInterface $binding) use($collector) {
         $collector->addMountHandler($resource->name, $resource->pattern, ResourceMountHandler::class, [$binding->getTypeName()]);
     });
 }
Esempio n. 7
0
 public function createExpressionContextFactory(ContainerInterface $container)
 {
     $factory = new ExpressionContextFactory();
     $container->eachMarked(function (ExpressionExtension $extension, BindingInterface $binding) use($factory, $container) {
         $factory->registerExtension($container->getBound($binding));
     });
     $container->eachMarked(function (ExpressionResolver $resolver, BindingInterface $binding) use($factory, $container) {
         $factory->getResolvers()->registerResolver($container->getBound($binding));
     });
     return $factory;
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(ContainerInterface $container, InjectionPointInterface $point = NULL)
 {
     return $container->createInstance($this, $point);
 }
Esempio n. 9
0
 public function performInitialization(ContainerInterface $container)
 {
     $container->bindInstance(TestSystem::class, $this);
     $this->performInitUsingTrait($container);
 }