Beispiel #1
0
 public function testCaptureToValueIsMutable()
 {
     $model = new ViewModel();
     $model->setCaptureTo('foo');
     $this->assertEquals('foo', $model->captureTo());
 }
 /**
  * Inspect the result, and cast it to a ViewModel
  *
  * @param  MvcEvent $e
  * @return void
  */
 public function canonizeActionResult(MvcEvent $e)
 {
     $result = $e->getResult();
     if ($result instanceof Response) {
         return;
     }
     // MVC controller
     $controller = $e->getTarget();
     // Controller view
     $controllerVew = $controller->plugin('view');
     // Collect captured contents
     if ($this->obStarted) {
         $content = ob_get_clean();
         if ($content) {
             if (null === $result && !$controllerVew->hasViewModel()) {
                 $result = $content;
             } elseif (Pi::service()->hasService('log')) {
                 Pi::service('log')->info('Captured content: ' . $content);
             }
         }
     }
     // Skip scalar result
     if (!$this->type && is_scalar($result) && !is_bool($result)) {
         if ($controllerVew->hasViewModel()) {
             $viewModel = $controllerVew->getViewModel();
         } else {
             $viewModel = new ViewModel();
         }
         $viewModel->setTemplate('__NULL__');
         $viewModel->setVariable($viewModel->captureTo(), $result);
         $e->setResult($viewModel);
         return;
     }
     // ViewModel generated by controller
     $viewModel = null;
     // Cast controller ViewModel
     if ($controllerVew->hasViewModel()) {
         $viewModel = $controllerVew->getViewModel();
         $template = $viewModel->getTemplate();
         // Controller ViewModel is as the main model if if if is specified
         // with template, MvcEvent result is converted to variables of
         // the ViewModel
         if ($viewModel instanceof ViewModel && !$viewModel instanceof JsonModel && !$viewModel instanceof FeedModel && $template && '__NULL__' != $template) {
             $variables = array();
             $options = array();
             if ($result instanceof ViewModel) {
                 $variables = $result->getVariables();
                 $options = $result->getOptions();
             } elseif ($result instanceof FeedDataModel) {
                 $variables = (array) $result;
                 $options = array('feed_type' => $result->getType());
             } elseif (ArrayUtils::hasStringKeys($result, true)) {
                 $variables = array_merge_recursive($variables, $result);
             }
             $viewModel->setVariables($variables);
             $viewModel->setOptions($options);
             $e->setResult($viewModel);
             return;
         }
     }
     // Set type to json if no template is specified
     if (!$this->type && null !== $result) {
         $skip = false;
         if ($result instanceof ViewModel && $result->getTemplate() && $result->getTemplate() != '__NULL__' || $result instanceof JsonModel || $result instanceof FeedModel) {
             $skip = true;
         }
         if (!$skip) {
             $this->type = 'json';
             Pi::service('log')->mute();
         }
     }
     // Cast controller view model to result ViewModel
     switch ($this->type) {
         // For Feed
         case 'feed':
             if ($result instanceof FeedModel) {
                 $model = $result;
             } else {
                 $variables = array();
                 //$options = array();
                 if ($result instanceof ViewModel) {
                     $variables = $result->getVariables();
                     $options = $result->getOptions();
                 } elseif ($result instanceof FeedDataModel) {
                     $variables = (array) $result;
                     $options = array('feed_type' => $result->getType());
                 } else {
                     if ($viewModel) {
                         $variables = $viewModel->getVariables();
                         //$options = $viewModel->getOptions();
                     }
                     if (ArrayUtils::hasStringKeys($result, true)) {
                         $variables = array_merge_recursive((array) $variables, (array) $result);
                     }
                     $model = new FeedDataModel($variables);
                     $variables = (array) $model;
                     $options = array('feed_type' => $model->getType());
                 }
                 $model = new FeedModel($variables, $options);
             }
             break;
             // For Json
         // For Json
         case 'json':
             if ($result instanceof JsonModel) {
                 $model = $result;
             } else {
                 $options = array();
                 //$data       = array();
                 if ($result instanceof ViewModel) {
                     $data = $result->getVariables();
                     $options = $result->getOptions();
                 } elseif (ArrayUtils::hasStringKeys($result, true)) {
                     if ($viewModel) {
                         $variables = $viewModel->getVariables();
                         $options = $viewModel->getOptions();
                     }
                     $data = array_merge_recursive((array) $variables, $result);
                 } else {
                     $data = $result;
                 }
                 $model = new JsonModel($data, $options);
             }
             break;
             // For AJAX/Flash
         // For AJAX/Flash
         case 'ajax':
             // MISC
         // MISC
         default:
             if ($viewModel) {
                 $model = $viewModel;
             } elseif ($result instanceof ViewModel) {
                 $model = $result;
                 $result = null;
             } else {
                 $model = new ViewModel();
             }
             if (null !== $result) {
                 $template = $model->getTemplate();
                 if ($this->type && (!$template || '__NULL_' == $template)) {
                     $model->setVariable('content', is_scalar($result) ? $result : json_encode($result));
                 } elseif (ArrayUtils::hasStringKeys($result, true)) {
                     $model->setVariables($result);
                 } elseif (is_scalar($result)) {
                     $model->setVariable('content', $result);
                 } else {
                     $model->setVariable('content', json_encode($result));
                 }
             }
             if ($this->type) {
                 $model->terminate(true);
             }
             break;
     }
     $e->setResult($model);
 }