Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function __invoke($event, &$dispatched = NULL, EventParamResolverInterface $resolver = NULL)
 {
     $object = $this->container->get($this->typeName);
     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" does not exist 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);
 }
Exemplo n.º 2
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));
     }
 }