예제 #1
0
 /**
  * @param object $controller
  * @param ReflectionMethod $method
  * @param RequestParams $requestParams
  * @return mixed
  */
 private function executeMethod($controller, ReflectionMethod $method, RequestParams $requestParams = null)
 {
     $params = null;
     $areParamsNamed = null;
     if ($requestParams) {
         $params = $requestParams->getParams() ?: null;
         if ($params) {
             $areParamsNamed = $requestParams->areParamsNamed();
         }
     }
     $args = [];
     foreach ($method->getParameters() as $parameter) {
         $class = $parameter->getClass();
         if ($class) {
             try {
                 $args[] = $this->container->make($class->name);
             } catch (ReflectionException $e) {
                 throw new InternalErrorException($e->getMessage(), 0, $e);
             }
         } else {
             if (null !== $params) {
                 if ($areParamsNamed) {
                     $name = $parameter->getName();
                     if (isset($params[$name])) {
                         $args[] = $params[$name];
                         unset($params[$name]);
                         continue;
                     }
                 } else {
                     if (count($params)) {
                         $args[] = array_shift($params);
                         continue;
                     }
                 }
             }
             try {
                 $args[] = $parameter->getDefaultValue();
             } catch (ReflectionException $e) {
                 throw new InvalidParamsException("\"{$parameter->getName()}\" is required", 0, $e);
             }
         }
     }
     return $method->invokeArgs($controller, $args);
 }
예제 #2
0
 /**
  * @param object $requestData
  * @return Request
  */
 public function createRequest($requestData)
 {
     if (!is_object($requestData)) {
         throw new BadRequestException();
     }
     if (!isset($requestData->jsonrpc) || $requestData->jsonrpc !== "2.0") {
         throw new BadRequestException();
     }
     if (empty($requestData->method) || !is_string($requestData->method)) {
         throw new BadRequestException();
     }
     $params = null;
     if (!empty($requestData->params)) {
         if (is_array($requestData->params)) {
             $params = RequestParams::constructPositional($requestData->params);
         } elseif (is_object($requestData->params)) {
             $params = RequestParams::constructNamed((array) $requestData->params);
         } else {
             throw new BadRequestException();
         }
     }
     return new Request($requestData->method, $params, isset($requestData->id) ? $requestData->id : null);
 }
 public function testRouteDispatcherControllerNamespaceIsIgnoredForRouteControllerClassDefinitionStartingWithBackslash()
 {
     $route = $this->getMockBuilder(RouteInterface::class)->getMock();
     $route->method('getControllerClass')->willReturn('\\RouteDispatcherTestNs\\UseThisController');
     $route->method('getActionName')->willReturn('returnArg');
     /** @var RouteInterface $route */
     $container = $this->getMockBuilder(Container::class)->getMock();
     $container->method('make')->will($this->returnCallback(function ($className) {
         return new $className();
     }));
     /** @var Container $container */
     eval('
         namespace RouteDispatcherTestNs {
             class UseThisController {
                 public function returnArg($value) {
                     return $value;
                 }
             }
         }
     ');
     $routeDispatcher = new RouteDispatcher($container, 'NoSuchNamespace');
     $result = $routeDispatcher->dispatch($route, RequestParams::constructPositional([100500]));
     $this->assertEquals(100500, $result);
 }