コード例 #1
0
 /**
  * Call a action of controller
  * 
  * @access public
  * @param MVC $mvc         MVC Application object
  * @param string $method   Method or Function of the Class Controller
  * @param string $fileView String of the view file
  * @return array           Response array
  * @throws \LogicException
  */
 public final function call(MVC $mvc, $method, $fileView = null)
 {
     if (!method_exists($this, $method)) {
         throw new \LogicException(sprintf('Method "s" don\'t exists.', $method));
     }
     # Replace the view object
     $this->view = $mvc->view();
     # Arguments of method
     $arguments = array();
     # Create a reflection method
     $reflectionMethod = new \ReflectionMethod(get_class($this), $method);
     $reflectionParams = $reflectionMethod->getParameters();
     foreach ($reflectionParams as $param) {
         if ($paramClass = $param->getClass()) {
             $className = $paramClass->name;
             if ($className === 'MVC\\MVC' || $className === '\\MVC\\MVC') {
                 $arguments[] = $mvc;
             } elseif ($className === 'MVC\\Server\\HttpRequest' || $className === '\\MVC\\Server\\HttpRequest') {
                 $arguments[] = $mvc->request();
             }
         } else {
             foreach ($mvc->request()->params as $keyReqParam => $valueReqParam) {
                 if ($param->name === $keyReqParam) {
                     $arguments[] = $valueReqParam;
                     break;
                 }
             }
         }
     }
     $response = call_user_func_array($reflectionMethod->getClosure($this), $arguments);
     if (empty($response)) {
         throw new \LogicException('Response null returned.');
     }
     if (is_string($response)) {
         $this->response['body'] = $response;
     } elseif ($mvc->request()->isAjax()) {
         $this->response['body'] = $this->renderJson($response);
     } elseif (is_array($response)) {
         if (!$fileView) {
             throw new \LogicException('File view is null.');
         }
         $class = explode("\\", get_called_class());
         $classname = end($class);
         // Class without Controller
         $classname = str_replace('Controller', '', $classname);
         $file = $classname . "/{$fileView}";
         $this->response['body'] = $this->renderHtml($file, $response);
     }
     return $this->response;
 }
コード例 #2
0
 /**
  * With MVC Application, HttpRequest and Route Params Action
  */
 public function testWithRouteParamsAction()
 {
     $this->mvcApp->request()->params = array('foo_key' => 'foo_value');
     $response = $this->fooCtrl->call($this->mvcApp, 'withRouteParamsAction');
     $this->assertTrue(is_array($response) && !empty($response));
 }