コード例 #1
0
 /**
  * @param   null|string     $controller
  * @param   null|string     $action
  * @param   array           $arguments
  * @return  string
  * @throws  \MVCFundamental\Exception\InternalServerErrorException
  */
 public function callControllerAction($controller = null, $action = null, array $arguments = array())
 {
     $this->boot();
     $controller_name = $controller;
     if (empty($controller)) {
         $controller = $this->getOption('default_controller_name');
     }
     if (empty($action)) {
         $action = $this->getOption('default_action_name');
     }
     if (!empty($controller)) {
         if (!is_object($controller)) {
             $ctrl = $this->get('locator')->locateController($controller);
             if (!empty($ctrl)) {
                 $controller = new $ctrl();
             } else {
                 throw new InternalServerErrorException(sprintf('Unknown controller "%s"!', $controller_name));
             }
         }
         $arguments = array_merge(Helper::getDefaultEnvParameters(), $arguments);
         $action = $this->get('locator')->locateControllerAction($action, $controller);
         if (!method_exists($controller, $action) || !is_callable(array($controller, $action))) {
             throw new InternalServerErrorException(sprintf('Action "%s" in controller "%s" is not known or not callable!', $action, $controller_name));
         }
         $result = Helper::fetchArguments($action, $arguments, $controller);
         // result as a raw content: a string
         if (is_string($result)) {
             return $result;
             // result as an array like ( view_file , params )
         } elseif (is_array($result)) {
             $view_file = $this->get('template_engine')->getTemplate($result[0]);
             if (!empty($view_file)) {
                 return $this->render($view_file, isset($result[1]) && is_array($result[1]) ? array_merge($arguments, $result[1]) : $arguments);
             }
             // a reponse object
         } elseif (is_object($result) && ($ri = AppKernel::getApi('response')) && $result instanceof $ri) {
             $this->set('response', $result);
         }
     }
     return '';
 }