Пример #1
0
 /**
  * @param Action $action
  * @param Request $request
  * @param array $data
  * @param int $invoke_type
  *
  * @throws Http\Exception\UnauthorizedException
  * @throws Http\Exception\NotFoundException
  * @throws Exception
  * @throws Http\Exception\ForbiddenException
  * @return Http\Response
  * @throws Http\Exception\ForbiddenException
  */
 public static final function invoke(Action $action, Request $request, $data = array(), $invoke_type = self::INVOKE_NORMAL)
 {
     list($controller_class, $action_method) = explode('.', $action->getAction(), 2) + array(null, null);
     if (!isset($action_method)) {
         $action_method = Config::get('wave')->controller->default_method;
     }
     if (class_exists($controller_class, true) && method_exists($controller_class, $action_method)) {
         /** @var \Wave\Controller $controller */
         $controller = new $controller_class();
         $controller->_action = $action;
         $controller->_request = $request;
         $controller->_response_method = $request->getFormat();
         $controller->_invoke_method = $invoke_type;
         switch ($controller->_request->getMethod()) {
             case Request::METHOD_GET:
                 $controller->_is_get = true;
                 break;
             case Request::METHOD_POST:
                 $controller->_is_post = true;
                 break;
         }
         $data = array_replace($controller->_request->getData(), $data);
         $controller->_data = $data;
         Hook::triggerAction('controller.before_init', array(&$controller));
         $controller->init();
         if ($invoke_type !== self::INVOKE_SUB_REQUEST && !$action->canRespondWith($request->getFormat())) {
             throw new NotFoundException('The requested action ' . $action->getAction() . ' can not respond with ' . $request->getFormat() . '. (Accepts: ' . implode(', ', $action->getRespondsWith()) . ')', $request);
         } else {
             if (!$action->checkRequiredLevel($request)) {
                 throw new UnauthorizedException('You are not authorized to view this resource');
             } else {
                 if ($action->needsValidation() && !$controller->inputValid($action->getValidationSchema($data))) {
                     return $controller->request();
                 }
             }
         }
         Hook::triggerAction('controller.before_dispatch', array(&$controller));
         $parameters = array();
         foreach ($action->getMethodParameters() as $parameter) {
             list($parameter_name, $parameter_type) = $parameter;
             if (isset($controller->_cleaned[$parameter_name])) {
                 //Try first in validator output
                 $parameters[] = $controller->_cleaned[$parameter_name];
             } elseif (isset($controller->_data[$parameter_name])) {
                 //Then if just using the passed data - there may be a legitimate use for this?
                 $parameters[] = $controller->_data[$parameter_name];
             } elseif ($parameter_type === 'Wave\\Validator\\Result') {
                 //If the validator is requested, give it
                 $parameters[] = $controller->_cleaned;
             } elseif ($parameter_type === get_class($request)) {
                 //If the request is requested, give it
                 $parameters[] = $request;
             } else {
                 //Otherwise place hold. Could maybe get the default value during generation and pass that instead
                 $parameters[] = null;
             }
         }
         try {
             $response = call_user_func_array(array($controller, $action_method), $parameters);
         } catch (InvalidInputException $e) {
             $controller->_input_errors = $e->getViolations();
             $response = $controller->request();
         }
         Hook::triggerAction('controller.after_dispatch', array(&$controller, &$response));
         return $response;
     } else {
         throw new Exception('Could not invoke action ' . $action->getAction() . '. Method ' . $controller_class . '::' . $action_method . '() does not exist', Response::STATUS_SERVER_ERROR);
     }
 }