Skip to content

Commit

Permalink
New FilePostRedirectGet plugin; Refactored PostRedirectGet plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmartin committed Nov 4, 2012
1 parent 2b297ca commit 2849b99
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 42 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
progress identifiers.
- New Zend\Filter\File\RenameUpload filter for securely moving uploaded
files.
- New Zend\Mvc\Controller\Plugin\FilePostRedirectGet plugin for file
upload forms.


## 2.0.2:
Expand Down
127 changes: 127 additions & 0 deletions library/Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/

namespace Zend\Mvc\Controller\Plugin;

use Zend\Form\Form;
use Zend\Mvc\Exception\RuntimeException;
use Zend\Session\Container;

/**
* Plugin to help facilitate Post/Redirect/Get (http://en.wikipedia.org/wiki/Post/Redirect/Get)
*
* @category Zend
* @package Zend_Mvc
* @subpackage Controller
*/
class FilePostRedirectGet extends AbstractPlugin
{
/**
* @var Container
*/
protected $sessionContainer;

public function __invoke($form, $redirect = null, $redirectToUrl = false)
{
$controller = $this->getController();
$request = $controller->getRequest();
$container = $this->getSessionContainer();

$returnObj = new \stdClass(); // TODO: Create a value object class / interface?
$returnObj->post = null;
$returnObj->isValid = null;
$returnObj->response = null;

if ($request->isPost()) {
$post = array_merge(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);
$returnObj->post = $container->post = $post;
$form->setData($post);

$returnObj->isValid = $isValid = $form->isValid();
if (!$isValid) {
$container->errors = $form->getMessages();
}

$returnObj->response = $this->redirect($redirect, $redirectToUrl);
return $returnObj;
} else {
if (null !== $container->post) {
$post = $container->post;
$errors = $container->errors;
unset($container->post);
unset($container->errors);

$form->setData($post);

$returnObj->isValid = $isValid = (null === $errors);
if (!$isValid) {
$form->setMessages($errors);
}

$returnObj->post = $post;
return $returnObj;
}

return false;
}
}

protected function getSessionContainer()
{
if (!isset($this->sessionContainer)) {
$this->sessionContainer = new Container('file_prg_post1');
$this->sessionContainer->setExpirationHops(1, array('post', 'errors'));
}
return $this->sessionContainer;
}

protected function redirect($redirect, $redirectToUrl)
{
$controller = $this->getController();
$params = array();

if (null === $redirect) {
$routeMatch = $controller->getEvent()->getRouteMatch();

$redirect = $routeMatch->getMatchedRouteName();
$params = $routeMatch->getParams();
}

if (method_exists($controller, 'getPluginManager')) {
// get the redirect plugin from the plugin manager
$redirector = $controller->getPluginManager()->get('Redirect');
} else {
/*
* If the user wants to redirect to a route, the redirector has to come
* from the plugin manager -- otherwise no router will be injected
*/
if ($redirectToUrl === false) {
throw new RuntimeException('Could not redirect to a route without a router');
}

$redirector = new Redirect();
}

if ($redirectToUrl === false) {
$response = $redirector->toRoute($redirect, $params);
$response->setStatusCode(303);
return $response;
}

$response = $redirector->toUrl($redirect);
$response->setStatusCode(303);

return $response;
}
}
86 changes: 52 additions & 34 deletions library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,43 @@
*/
class PostRedirectGet extends AbstractPlugin
{
/**
* @var Container
*/
protected $sessionContainer;

public function __invoke($redirect = null, $redirectToUrl = false)
{
$controller = $this->getController();
$request = $controller->getRequest();
$container = $this->getSessionContainer();

if ($request->isPost()) {
$container->post = $request->getPost()->toArray();
return $this->redirect($redirect, $redirectToUrl);
} else {
if ($container->post !== null) {
$post = $container->post;
unset($container->post);
return $post;
}

return false;
}
}

protected function getSessionContainer()
{
if (!isset($this->sessionContainer)) {
$this->sessionContainer = new Container('prg_post1');
$this->sessionContainer->setExpirationHops(1, 'post');
}
return $this->sessionContainer;
}

protected function redirect($redirect, $redirectToUrl)
{
$controller = $this->getController();
$params = array();

if (null === $redirect) {
Expand All @@ -36,45 +69,30 @@ public function __invoke($redirect = null, $redirectToUrl = false)
$params = $routeMatch->getParams();
}

$container = new Container('prg_post1');

if ($request->isPost()) {
$container->setExpirationHops(1, 'post');
$container->post = $request->getPost()->toArray();

if (method_exists($controller, 'getPluginManager')) {
// get the redirect plugin from the plugin manager
$redirector = $controller->getPluginManager()->get('Redirect');
} else {
/*
* If the user wants to redirect to a route, the redirector has to come
* from the plugin manager -- otherwise no router will be injected
*/
if ($redirectToUrl === false) {
throw new RuntimeException('Could not redirect to a route without a router');
}

$redirector = new Redirect();
}

if (method_exists($controller, 'getPluginManager')) {
// get the redirect plugin from the plugin manager
$redirector = $controller->getPluginManager()->get('Redirect');
} else {
/*
* If the user wants to redirect to a route, the redirector has to come
* from the plugin manager -- otherwise no router will be injected
*/
if ($redirectToUrl === false) {
$response = $redirector->toRoute($redirect, $params);
$response->setStatusCode(303);
return $response;
throw new RuntimeException('Could not redirect to a route without a router');
}

$response = $redirector->toUrl($redirect);
$response->setStatusCode(303);
$redirector = new Redirect();
}

if ($redirectToUrl === false) {
$response = $redirector->toRoute($redirect, $params);
$response->setStatusCode(303);
return $response;
} else {
if ($container->post !== null) {
$post = $container->post;
unset($container->post);
return $post;
}

return false;
}

$response = $redirector->toUrl($redirect);
$response->setStatusCode(303);

return $response;
}
}
18 changes: 10 additions & 8 deletions library/Zend/Mvc/Controller/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class PluginManager extends AbstractPluginManager
* @var array
*/
protected $invokableClasses = array(
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
'forward' => 'Zend\Mvc\Controller\Plugin\Forward',
'layout' => 'Zend\Mvc\Controller\Plugin\Layout',
'params' => 'Zend\Mvc\Controller\Plugin\Params',
'postredirectget' => 'Zend\Mvc\Controller\Plugin\PostRedirectGet',
'redirect' => 'Zend\Mvc\Controller\Plugin\Redirect',
'url' => 'Zend\Mvc\Controller\Plugin\Url',
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
'forward' => 'Zend\Mvc\Controller\Plugin\Forward',
'layout' => 'Zend\Mvc\Controller\Plugin\Layout',
'params' => 'Zend\Mvc\Controller\Plugin\Params',
'postredirectget' => 'Zend\Mvc\Controller\Plugin\PostRedirectGet',
'filepostredirectget' => 'Zend\Mvc\Controller\Plugin\FilePostRedirectGet',
'redirect' => 'Zend\Mvc\Controller\Plugin\Redirect',
'url' => 'Zend\Mvc\Controller\Plugin\Url',
);

/**
Expand All @@ -48,7 +49,8 @@ class PluginManager extends AbstractPluginManager
* @var array
*/
protected $aliases = array(
'prg' => 'postredirectget',
'prg' => 'postredirectget',
'fileprg' => 'filepostredirectget',
);

/**
Expand Down
Loading

0 comments on commit 2849b99

Please sign in to comment.