Example #1
0
 /**
  * Dispatch the routed page, working through its init(), process() and
  * render() methods.
  *
  * Each abstract page class can specify whether the process() method
  * should actually be run by implementing a shouldProcess() method.
  * The page's init() method is guaranteed to always be called.  If
  * after calling render there is no output, the component will attempt
  * to render the page's default view script automatically.
  *
  * @param mixed $page
  * @param Response $response
  * @return mixed
  */
 public function dispatchPage($page = null, Response $response = null)
 {
     $this->active = true;
     if (!$this->getPermissions()->can('access')) {
         return $this->env->redirect('/admin/');
     }
     if (is_string($page)) {
         $page = $this->createPageObject($page);
     }
     $this->executePageDispatchCallbacks($page);
     if (null === $response) {
         $response = new Response($page, array($this->env, 'redirect'));
     }
     $page->init();
     if ($page->shouldProcess()) {
         $responseHelper = $page->createResponseHelper(array($this->env, 'redirect'));
         $page->process($responseHelper);
         $response->setWasProcessed(true)->setHelper($responseHelper);
         $result = $response->executeQueuedActions();
         if ($result) {
             return $result;
         }
     }
     ob_start();
     $output = $page->render();
     if (is_array($output)) {
         $this->renderJsonResponse($output);
     } elseif ($output instanceof SymfonyResponse) {
         return $output;
     } elseif (!$output) {
         // Capture output generated during render rather than returned
         $output = ob_get_clean();
     }
     // Automatically render view if no output is generated
     if (!$output) {
         $output = $page->renderView();
     }
     if (!$this->shouldRenderLayout) {
         return $output;
     } else {
         return $this->env->setActiveComponent($this)->renderLayout($output, $page->getView()->headScript(), $page->getView()->headLink());
     }
 }