/** * 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()); } }
/** * Handle the admin_init action. All page handling is done on admin_init * so we have the opportunity to run code prior to WP rendering any output. * * @param string $page The name of the page to route to (e.g. "Index" or "Edit"). * @param Response $response Inject a response object, usually for tests. * * @param ComponentInterface $component * @param string|null $page * @param Response|null $response * @return ComponentInterface */ public function adminInit(ComponentInterface $component, $page = null, Response $response = null) { if ($this->componentIsCurrentlyActive($component)) { $page = $component->createPageObject($component->getRequest()->getQuery('route', 'Index')); $this->enqueueClientSideDependencies($page->getView()); if (null === $response) { $response = new Response(); } if ($page) { $response->setPage($page); $this->output = $component->dispatchPage($page, $response); // If AJAX or non-layout response, render now to avoid WP shell mucking it up if ($component->getRequest()->isAjax() || !$component->shouldRenderLayout()) { echo $this->output; exit; } } } return $this; }