[Beeframework-svn] SF.net SVN: beeframework:[160] trunk/framework/Bee
Brought to you by:
b_hartmann,
m_plomer
From: <m_p...@us...> - 2014-07-02 08:11:06
|
Revision: 160 http://sourceforge.net/p/beeframework/code/160 Author: m_plomer Date: 2014-07-02 08:10:59 +0000 (Wed, 02 Jul 2014) Log Message: ----------- - MVC Dispatcher: introduced IRequestBuilder, DefaultRequestBuilder - MVC/Views: RequestStoringRedirectView and RedirectedRequestBuilder Modified Paths: -------------- trunk/framework/Bee/MVC/Dispatcher.php trunk/framework/Bee/MVC/FilterChainProxy.php trunk/framework/Bee/MVC/HttpRequest.php trunk/framework/Bee/MVC/IFilter.php trunk/framework/Bee/MVC/View/Redirect.php trunk/framework/Bee/Security/Context/HttpSessionIntegrationFilter.php trunk/framework/Bee/Utils/HashManager.php Added Paths: ----------- trunk/framework/Bee/MVC/DefaultRequestBuilder.php trunk/framework/Bee/MVC/IRequestBuilder.php trunk/framework/Bee/MVC/Redirect/ trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php trunk/framework/Bee/MVC/View/RequestStoringRedirectView.php Added: trunk/framework/Bee/MVC/DefaultRequestBuilder.php =================================================================== --- trunk/framework/Bee/MVC/DefaultRequestBuilder.php (rev 0) +++ trunk/framework/Bee/MVC/DefaultRequestBuilder.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,19 @@ +<?php + +namespace Bee\MVC; +use Bee_MVC_HttpRequest; + + +/** + * Class DefaultRequestBuilder + * @package Bee\MVC + */ +class DefaultRequestBuilder implements IRequestBuilder { + + /** + * @return \Bee_MVC_IHttpRequest + */ + public function buildRequestObject() { + return new Bee_MVC_HttpRequest(); + } +} \ No newline at end of file Modified: trunk/framework/Bee/MVC/Dispatcher.php =================================================================== --- trunk/framework/Bee/MVC/Dispatcher.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/MVC/Dispatcher.php 2014-07-02 08:10:59 UTC (rev 160) @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +use Bee\MVC\DefaultRequestBuilder; /** * The dispatcher is the main entry point into an Bee MVC application. It acts as a front controller, i.e. it handles incoming @@ -41,6 +42,8 @@ */ class Bee_MVC_Dispatcher implements Bee_MVC_IFilterChain { + const REQUEST_BUILDER_BEAN_NAME = 'requestBuilder'; + const HANDLER_MAPPING_BEAN_NAME = 'handlerMapping'; const VIEW_RESOLVER_BEAN_NAME = 'viewResolver'; @@ -83,6 +86,10 @@ */ private $context; + /** + * @var \Bee\MVC\IRequestBuilder + */ + private $requestBuilder; /** * The handler mapping used by this dispatcher @@ -180,7 +187,26 @@ * @return void */ protected function init() { - self::$currentRequest = $this->buildRequestObject(); + if ($this->context->containsBean(Bee_MVC_Session_DispatcherAdapter::SESSION_HANDLER_NAME)) { + $this->getLog()->info('custom session handler configured, setting it as PHP session_set_save_handler()'); + $sessionAdapter = new Bee_MVC_Session_DispatcherAdapter($this->context); + session_set_save_handler( + array(&$sessionAdapter, "open"), + array(&$sessionAdapter, "close"), + array(&$sessionAdapter, "read"), + array(&$sessionAdapter, "write"), + array(&$sessionAdapter, "destroy"), + array(&$sessionAdapter, "gc") + ); + } + + try { + $this->requestBuilder = $this->context->getBean(self::HANDLER_MAPPING_BEAN_NAME, '\Bee\MVC\IRequestBuilder'); + } catch (Bee_Context_NoSuchBeanDefinitionException $ex) { + $this->getLog()->info('no RequestBuilder configured, using DefaultRequestBuilder'); + $this->requestBuilder = new DefaultRequestBuilder(); + } + $this->handlerMapping = $this->context->getBean(self::HANDLER_MAPPING_BEAN_NAME, 'Bee_MVC_IHandlerMapping'); $this->viewResolver = $this->context->getBean(self::VIEW_RESOLVER_BEAN_NAME, 'Bee_MVC_IViewResolver'); @@ -196,18 +222,7 @@ $this->getLog()->info('no exception resolver configured'); } - if ($this->context->containsBean(Bee_MVC_Session_DispatcherAdapter::SESSION_HANDLER_NAME)) { - $this->getLog()->info('custom session handler configured, setting it as PHP session_set_save_handler()'); - $sessionAdapter = new Bee_MVC_Session_DispatcherAdapter($this->context); - session_set_save_handler( - array(&$sessionAdapter, "open"), - array(&$sessionAdapter, "close"), - array(&$sessionAdapter, "read"), - array(&$sessionAdapter, "write"), - array(&$sessionAdapter, "destroy"), - array(&$sessionAdapter, "gc") - ); - } + self::$currentRequest = $this->requestBuilder->buildRequestObject(); } /** @@ -327,15 +342,6 @@ } } } - - /** - * Enter description here... - * - * @return Bee_MVC_HttpRequest - */ - private function buildRequestObject() { - return new Bee_MVC_HttpRequest(); - } } class B_DISPATCHER extends Bee_MVC_Dispatcher { Modified: trunk/framework/Bee/MVC/FilterChainProxy.php =================================================================== --- trunk/framework/Bee/MVC/FilterChainProxy.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/MVC/FilterChainProxy.php 2014-07-02 08:10:59 UTC (rev 160) @@ -75,15 +75,15 @@ */ private $currentPosition = 0; - /** - * @param Bee_MVC_IFilterChain $origFilterChain - * @param Bee_MVC_IFilter[] $filters - * @return void - */ + /** + * @param Bee_MVC_IFilterChain $origFilterChain + * @param Bee_MVC_IFilter[] $filters + */ public function __construct(Bee_MVC_IFilterChain $origFilterChain, array $filters) { $this->origFilterChain = $origFilterChain; $this->filters = $filters; } + public function doFilter(Bee_MVC_IHttpRequest $request) { if($this->currentPosition == sizeof($this->filters)) { $this->origFilterChain->doFilter($request); @@ -94,5 +94,4 @@ } } -} -?> \ No newline at end of file +} \ No newline at end of file Modified: trunk/framework/Bee/MVC/HttpRequest.php =================================================================== --- trunk/framework/Bee/MVC/HttpRequest.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/MVC/HttpRequest.php 2014-07-02 08:10:59 UTC (rev 160) @@ -103,17 +103,19 @@ } /** - * Returns the PATH_INFO (i.e. any additional path trailing the actual PHP file) + * Check whether the given parameter exists * - * @return string + * @param $name + * @return bool */ public function hasParameter($name) { return array_key_exists($name, $this->parameters); } /** - * Returns the PATH_INFO (i.e. any additional path trailing the actual PHP file) + * Returns the value of the given parameter, or null if no parameter with that name exists * + * @param string $name * @return string */ public function getParameter($name) { @@ -122,9 +124,14 @@ // $val = $val[0]; // } // return $val; - return array_key_exists($name, $this->parameters) ? $this->parameters[$name] : null; + return $this->hasParameter($name) ? $this->parameters[$name] : null; } + /** + * Set the value of the given parameter + * @param $name + * @param $value + */ public function setParameter($name, $value) { if(is_null($value)) { unset($this->parameters[$name]); @@ -162,9 +169,6 @@ } public function getHeaderNames() { - if(is_null($this->headerNames)) { - $this->headerNames = array_keys($this->headers); - } return $this->headerNames; } Modified: trunk/framework/Bee/MVC/IFilter.php =================================================================== --- trunk/framework/Bee/MVC/IFilter.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/MVC/IFilter.php 2014-07-02 08:10:59 UTC (rev 160) @@ -25,4 +25,3 @@ public function doFilter(Bee_MVC_IHttpRequest $request, Bee_MVC_IFilterChain $filterChain); } -?> \ No newline at end of file Added: trunk/framework/Bee/MVC/IRequestBuilder.php =================================================================== --- trunk/framework/Bee/MVC/IRequestBuilder.php (rev 0) +++ trunk/framework/Bee/MVC/IRequestBuilder.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,16 @@ +<?php + +namespace Bee\MVC; + +/** + * Interface IRequestBuilder + * + * @package Bee\MVC + */ +interface IRequestBuilder { + + /** + * @return \Bee_MVC_IHttpRequest + */ + public function buildRequestObject(); +} \ No newline at end of file Added: trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php (rev 0) +++ trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,48 @@ +<?php + +namespace Bee\MVC\Redirect; +use Bee\Utils\HashManager; +use Bee_MVC_IHttpRequest; + +/** + * Class AbstractRedirectStorage + * @package Bee\MVC\Redirect + */ +abstract class AbstractRedirectStorage { + + /** + * @var string + */ + private $id = false; + + /** + * @return string + */ + public function getId() { + if(!$this->id) { + $this->id = HashManager::createHash(); + } + return $this->id; + } + + /** + * @param array $model + * @return string + */ + public final function storeData(array $model = array()) { + $this->doStoreData($model); + $_SESSION[$this->getId()] = $this; + return $this->getId(); + } + + /** + * @param array $model + * @return mixed + */ + abstract protected function doStoreData(array $model); + + /** + * @return \Bee_MVC_IHttpRequest + */ + abstract public function restoreRequestObject(); +} \ No newline at end of file Added: trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php (rev 0) +++ trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,54 @@ +<?php + +namespace Bee\MVC\Redirect; +use Bee_MVC_HttpRequest; +use Bee_MVC_IHttpRequest; + + +/** + * Class RedirectInfoStorage + * @package Bee\MVC\View + */ +class RedirectInfoStorage extends AbstractRedirectStorage { + + /** + * @var array + */ + private $storeModelKeys = array(); + + /** + * @var array + */ + private $storedData; + + /** + * @param array $storeModelKeys + */ + public function setStoreModelKeys(array $storeModelKeys) { + $this->storeModelKeys = $storeModelKeys; + } + + /** + * @return array + */ + public function getStoreModelKeys() { + return $this->storeModelKeys; + } + + /** + * @param array $model + * @return mixed + */ + protected function doStoreData(array $model = array()) { + $this->storedData = array_intersect_key($model, array_fill_keys($this->storeModelKeys, true)); + } + + /** + * @return \Bee_MVC_IHttpRequest + */ + public function restoreRequestObject() { + $request = new Bee_MVC_HttpRequest(); + $request->addParameters($this->storedData); + return $request; + } +} \ No newline at end of file Added: trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php (rev 0) +++ trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,61 @@ +<?php + +namespace Bee\MVC\Redirect; +use Bee_MVC_IHttpRequest; +use Bee_Utils_Env; + + +/** + * Class RedirectRequestStorage + * @package Bee\MVC\View + */ +class RedirectRequestStorage extends AbstractRedirectStorage { + + /** + * @var array + */ + private $getParams; + + /** + * @var array + */ + private $postParams; + + /** + * @var array + */ + private $requestParams; + + /** + * @var array + */ + private $serverVars; + + /** + * @var array + */ + private $headers; + + /** + * @param array $model + * @return mixed + */ + protected function doStoreData(array $model = array()) { + $this->getParams = $_GET; + $this->postParams = $_POST; + $this->requestParams = $_REQUEST; + $this->serverVars = $_SERVER; + $this->headers = Bee_Utils_Env::getRequestHeaders(); + } + + /** + * @return \Bee_MVC_IHttpRequest + */ + public function restoreRequestObject() { + $_GET = $this->getParams; + $_POST = $this->postParams; + $_REQUEST = $this->requestParams; + $_SERVER = $this->serverVars; + return new \Bee_MVC_HttpRequest(null, null, null, $this->headers); + } +} \ No newline at end of file Added: trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php (rev 0) +++ trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,53 @@ +<?php + +namespace Bee\MVC\Redirect; +use Bee\MVC\IRequestBuilder; + +/** + * Class RedirectedRequestBuilder - restores a previously stored request from the session if applicable, or creates a + * default request otherwise. + * + * @package Bee\MVC\Redirect + */ +class RedirectedRequestBuilder implements IRequestBuilder { + + private $requestIdParamName = 'storedRequestId'; + + /** + * @param string $requestIdParamName + */ + public function setRequestIdParamName($requestIdParamName) { + $this->requestIdParamName = $requestIdParamName; + } + + /** + * @return string + */ + public function getRequestIdParamName() { + return $this->requestIdParamName; + } + + /** + * @return \Bee_MVC_IHttpRequest + */ + public function buildRequestObject() { + if(array_key_exists($this->requestIdParamName, $_GET)) { + $id = $_GET[$this->requestIdParamName]; + if(array_key_exists($id, $_SESSION) && ($storage = $_SESSION[$id]) instanceof AbstractRedirectStorage) { + /** @var AbstractRedirectStorage $storage */ + return $storage->restoreRequestObject(); + } + $this->throwNotFoundException($id); + } + return new \Bee_MVC_HttpRequest(); + } + + /** + * @param $id + * @throws \Exception + */ + protected function throwNotFoundException($id) { + throw new \Exception('Stored request with ID ' . $id . ' not found'); + } + +} \ No newline at end of file Modified: trunk/framework/Bee/MVC/View/Redirect.php =================================================================== --- trunk/framework/Bee/MVC/View/Redirect.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/MVC/View/Redirect.php 2014-07-02 08:10:59 UTC (rev 160) @@ -54,10 +54,7 @@ if (array_key_exists(self::MODEL_KEY_GET_PARAMS, $model)) { $params = $model[self::MODEL_KEY_GET_PARAMS]; Bee_Utils_Assert::isTrue(is_array($params), 'Bee_MVC_View_Redirect: getParams must be an array'); - array_walk($params, function (&$value, $key) { - $value = urlencode($key) . '=' . urlencode($value); - }); - $redirectUrl .= (strpos($redirectUrl, '?') !== false ? '&' : '?') . implode('&', $params); + $redirectUrl .= (strpos($redirectUrl, '?') !== false ? '&' : '?') . http_build_query($params); } header('Location: ' . $redirectUrl, true, $this->getStatusCode()); } Added: trunk/framework/Bee/MVC/View/RequestStoringRedirectView.php =================================================================== --- trunk/framework/Bee/MVC/View/RequestStoringRedirectView.php (rev 0) +++ trunk/framework/Bee/MVC/View/RequestStoringRedirectView.php 2014-07-02 08:10:59 UTC (rev 160) @@ -0,0 +1,68 @@ +<?php + +namespace Bee\MVC\View; +use Bee\MVC\Redirect\AbstractRedirectStorage; +use Bee_MVC_View_Redirect; + +/** + * Class RequestStoringRedirectView + * @package Bee\MVC\View + */ +class RequestStoringRedirectView extends \Bee_MVC_View_Redirect { + + /** + * @var string + */ + private $requestIdParamName = 'requestId'; + + /** + * @var AbstractRedirectStorage[] + */ + private $stores = array(); + + /** + * @param string $requestIdParamName + */ + public function setRequestIdParamName($requestIdParamName) { + $this->requestIdParamName = $requestIdParamName; + } + + /** + * @return string + */ + public function getRequestIdParamName() { + return $this->requestIdParamName; + } + + /** + * @param array $stores + */ + public function setStores(array $stores) { + $this->stores = $stores; + } + + /** + * @return array + */ + public function getStores() { + return $this->stores; + } + + public function render(array $model = array()) { + $this->augmentModel($model); + $getParams = array_key_exists(self::MODEL_KEY_GET_PARAMS, $model) ? $model[self::MODEL_KEY_GET_PARAMS] : array(); + foreach($this->stores as $paramName => $store) { + $getParams[$paramName] = $store->storeData($model); + } + $model[self::MODEL_KEY_GET_PARAMS] = $getParams; + parent::render($model); + } + + /** + * Extension point for subclasses + * @param array $model + */ + protected function augmentModel(array &$model) { + // do nothing by default + } +} \ No newline at end of file Modified: trunk/framework/Bee/Security/Context/HttpSessionIntegrationFilter.php =================================================================== --- trunk/framework/Bee/Security/Context/HttpSessionIntegrationFilter.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/Security/Context/HttpSessionIntegrationFilter.php 2014-07-02 08:10:59 UTC (rev 160) @@ -69,4 +69,3 @@ } } -?> \ No newline at end of file Modified: trunk/framework/Bee/Utils/HashManager.php =================================================================== --- trunk/framework/Bee/Utils/HashManager.php 2014-07-01 16:04:58 UTC (rev 159) +++ trunk/framework/Bee/Utils/HashManager.php 2014-07-02 08:10:59 UTC (rev 160) @@ -57,8 +57,8 @@ return $row['hash']; } catch (Exception $e) { - $hash = $this->createHash(); - if ($this->persistHash($this->createHash(), $id, $group)) { + $hash = self::createHash(); + if ($this->persistHash(self::createHash(), $id, $group)) { return $hash; } return false; @@ -93,7 +93,10 @@ } } - private function createHash() { + /** + * @return mixed + */ + public static function createHash() { return preg_replace('/\./', 'b', uniqid('', true)); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |