Created
April 12, 2012 22:35
-
-
Save SocalNick/2371502 to your computer and use it in GitHub Desktop.
Action View Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* IGN Application Module | |
* | |
* @category Application | |
* @package Application_View | |
* @copyright Copyright (c) 2006-2011 IGN Entertainment, Inc. (http://corp.ign.com/) | |
*/ | |
namespace Application\View\Helper; | |
use Zend\Di\Locator, | |
Zend\Http\PhpEnvironment\Request, | |
Zend\Mvc\Controller\ActionController, | |
Zend\Mvc\MvcEvent, | |
Zend\Mvc\Router\Http\RouteMatch, | |
Zend\View, | |
Zend\View\Exception, | |
Zend\View\Helper; | |
/** | |
* Helper for rendering output of a controller action | |
* @category Application | |
* @package Application_View | |
* @copyright Copyright (c) 2006-2011 IGN Entertainment, Inc. (http://corp.ign.com/) | |
*/ | |
class Action extends Helper\AbstractHelper | |
{ | |
/** | |
* @var \Zend\Di\Locator | |
*/ | |
protected $locator; | |
/** | |
* Set locator instance | |
* | |
* @param Di $locator | |
* @return void | |
*/ | |
public function setLocator(Locator $locator) | |
{ | |
$this->locator = $locator; | |
} | |
/** | |
* Dispatch a controller action and render using view | |
* If the action results in anything besides a View Model, return empty string. | |
* | |
* In standalone mode, it validates the client supports ESI | |
* If so, it returns an ESI include tag | |
* | |
* @param string $action | |
* @param string $controller | |
* @param string $module Defaults to default module | |
* @param array $params | |
* @throws Zend\View\Exception\RuntimeException | |
* @return string | |
*/ | |
public function __invoke($action, $controller, array $params = array(), $standaloneRouteName = false) | |
{ | |
if (!$this->locator instanceof Locator) { | |
throw new Exception\RuntimeException('No Locator provided'); | |
} | |
// uniqid ensures we don't get a shared instance | |
$controllerObject = $this->locator->get($controller, array('uniqid' => uniqid())); | |
if (!$controllerObject instanceof ActionController) { | |
throw new Exception\RuntimeException('Invalid controller'); | |
} | |
$actionMethod = $controllerObject->getMethodFromAction($action); | |
if (!method_exists($controllerObject, $actionMethod)) { | |
throw new Exception\RuntimeException('Invalid action'); | |
} | |
$request = new Request(); | |
if ($standaloneRouteName) { | |
$headers = $request->headers(); | |
if ($headers->has('surrogate-capability') && $headers->get('surrogate-capability')->getFieldValue() == 'ESI/1.0') { | |
try { | |
$url = $this->view->url($standaloneRouteName, $params); | |
} catch (\Zend\Mvc\Router\Exception\RuntimeException $e) { | |
throw new Exception\RuntimeException('Invalid stand-alone route'); | |
} | |
return '<esi:include src="' | |
. \Zend\Uri\Uri::merge($request->uri(), $url) | |
. '" onerror="continue"/>'; | |
} | |
} | |
$routeMatch = new RouteMatch( | |
array_merge( | |
$params, | |
array('controller' => $controller, 'action' => $action) | |
) | |
); | |
$e = new MvcEvent(); | |
$e->setRouter($this->locator->get('Zend\Mvc\Router\RouteStack')); | |
$e->setRouteMatch($routeMatch); | |
$controllerObject->setEvent($e); | |
$request->query()->fromArray($params); | |
$result = $controllerObject->dispatch($request); | |
if ($result instanceof \Zend\View\Model) { | |
return $this->view->render($result); | |
} | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment