[Beeframework-svn] SF.net SVN: beeframework:[161] trunk/framework/Bee
Brought to you by:
b_hartmann,
m_plomer
From: <m_p...@us...> - 2014-07-02 09:43:49
|
Revision: 161 http://sourceforge.net/p/beeframework/code/161 Author: m_plomer Date: 2014-07-02 09:43:40 +0000 (Wed, 02 Jul 2014) Log Message: ----------- - MVC Dispatcher: introduced IRequestBuilder, DefaultRequestBuilder - MVC/Views: RequestStoringRedirectView and RedirectedRequestBuilder Modified Paths: -------------- trunk/framework/Bee/Context/BeansException.php trunk/framework/Bee/MVC/DefaultRequestBuilder.php trunk/framework/Bee/MVC/Dispatcher.php trunk/framework/Bee/MVC/HttpRequest.php trunk/framework/Bee/MVC/IHttpRequest.php trunk/framework/Bee/MVC/IViewResolver.php 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/SimpleMappingExceptionResolver.php trunk/framework/Bee/MVC/ViewResolver/Basic.php trunk/framework/Bee/MVC/ViewResolver/Loopback.php Added Paths: ----------- trunk/framework/Bee/MVC/Request/ trunk/framework/Bee/MVC/Request/DefaultAjaxDetectionStrategy.php trunk/framework/Bee/MVC/Request/IAjaxDetectionStrategy.php Modified: trunk/framework/Bee/Context/BeansException.php =================================================================== --- trunk/framework/Bee/Context/BeansException.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/Context/BeansException.php 2014-07-02 09:43:40 UTC (rev 161) @@ -23,5 +23,3 @@ */ class Bee_Context_BeansException extends Bee_Exceptions_Base { } - -?> \ No newline at end of file Modified: trunk/framework/Bee/MVC/DefaultRequestBuilder.php =================================================================== --- trunk/framework/Bee/MVC/DefaultRequestBuilder.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/DefaultRequestBuilder.php 2014-07-02 09:43:40 UTC (rev 161) @@ -1,9 +1,10 @@ <?php namespace Bee\MVC; +use Bee\MVC\Request\DefaultAjaxDetectionStrategy; +use Bee\MVC\Request\IAjaxDetectionStrategy; use Bee_MVC_HttpRequest; - /** * Class DefaultRequestBuilder * @package Bee\MVC @@ -11,9 +12,40 @@ class DefaultRequestBuilder implements IRequestBuilder { /** + * @var IAjaxDetectionStrategy + */ + private $ajaxDetectionStrategy; + + /** * @return \Bee_MVC_IHttpRequest */ public function buildRequestObject() { - return new Bee_MVC_HttpRequest(); + return $this->massageConstructedRequest(new Bee_MVC_HttpRequest()); } + + /** + * @param Bee_MVC_HttpRequest $request + * @return Bee_MVC_HttpRequest + */ + public function massageConstructedRequest(Bee_MVC_HttpRequest $request) { + $request->setAjax($this->getAjaxDetectionStrategy()->isAjaxRequest($request)); + return $request; + } + + /** + * @param IAjaxDetectionStrategy $ajaxDetectionStrategy + */ + public function setAjaxDetectionStrategy(IAjaxDetectionStrategy $ajaxDetectionStrategy) { + $this->ajaxDetectionStrategy = $ajaxDetectionStrategy; + } + + /** + * @return IAjaxDetectionStrategy + */ + public function getAjaxDetectionStrategy() { + if(is_null($this->ajaxDetectionStrategy)) { + $this->ajaxDetectionStrategy = new DefaultAjaxDetectionStrategy(); + } + return $this->ajaxDetectionStrategy; + } } \ No newline at end of file Modified: trunk/framework/Bee/MVC/Dispatcher.php =================================================================== --- trunk/framework/Bee/MVC/Dispatcher.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/Dispatcher.php 2014-07-02 09:43:40 UTC (rev 161) @@ -298,7 +298,7 @@ if ($mav instanceof Bee_MVC_ModelAndView) { $mav->addModelValue(Bee_MVC_Model::CURRENT_REQUEST_KEY, $request); - $this->resolveModelAndView($mav); + $this->resolveModelAndView($mav, $request); if(!is_null($handlerException) && !count($interceptors)) { // We were unable to resolve a handler and its interceptors due to an exception being thrown along @@ -319,8 +319,8 @@ } } - public function resolveModelAndView(Bee_MVC_ModelAndView $mav) { - $resolvedView = $this->viewResolver->resolveViewName($mav->getViewName()); + public function resolveModelAndView(Bee_MVC_ModelAndView $mav, Bee_MVC_IHttpRequest $request) { + $resolvedView = $this->viewResolver->resolveViewName($mav->getViewName(), $request); $mav->setResolvedView($resolvedView); if ($resolvedView instanceof Bee_MVC_View_Abstract) { $statics = $resolvedView->getStaticAttributes(); @@ -330,15 +330,15 @@ $model = array_merge($statics, $mav->getModel()); $mav->setModel($model); } - $this->resolveModelInternals($mav->getModel()); + $this->resolveModelInternals($mav->getModel(), $request); } - private function resolveModelInternals(array $model) { + private function resolveModelInternals(array $model, Bee_MVC_IHttpRequest $request) { foreach ($model as $modelElem) { if ($modelElem instanceof Bee_MVC_ModelAndView) { - $this->resolveModelAndView($modelElem); + $this->resolveModelAndView($modelElem, $request); } else if (is_array($modelElem)) { - $this->resolveModelInternals($modelElem); + $this->resolveModelInternals($modelElem, $request); } } } Modified: trunk/framework/Bee/MVC/HttpRequest.php =================================================================== --- trunk/framework/Bee/MVC/HttpRequest.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/HttpRequest.php 2014-07-02 09:43:40 UTC (rev 161) @@ -60,7 +60,12 @@ * @var array */ private $headerNames; - + + /** + * @var bool + */ + private $ajax; + public function __construct(array $parameters = null, $pathInfo = null, $method = null, array $headers = null) { if(is_null($headers)) { $headers = Bee_Utils_Env::getRequestHeaders(); @@ -195,4 +200,18 @@ } return new Bee_MVC_HttpRequest($params, $pathInfo, $method, $headers); } + + /** + * @param boolean $ajax + */ + public function setAjax($ajax) { + $this->ajax = $ajax; + } + + /** + * @return boolean + */ + public function getAjax() { + return $this->ajax; + } } Modified: trunk/framework/Bee/MVC/IHttpRequest.php =================================================================== --- trunk/framework/Bee/MVC/IHttpRequest.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/IHttpRequest.php 2014-07-02 09:43:40 UTC (rev 161) @@ -44,13 +44,14 @@ * @return String */ public function getMethod(); - - - /** - * Enter description here... - * - * @return bool - */ + + + /** + * Enter description here... + * + * @param string $name + * @return bool + */ public function hasParameter($name); /** @@ -99,4 +100,10 @@ // public function getParamArray(); public function addParameters(array $params); + + /** + * True if this is an AJAX request, false in case of a regular GET or POSTback. + * @return bool + */ + public function getAjax(); } \ No newline at end of file Modified: trunk/framework/Bee/MVC/IViewResolver.php =================================================================== --- trunk/framework/Bee/MVC/IViewResolver.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/IViewResolver.php 2014-07-02 09:43:40 UTC (rev 161) @@ -28,19 +28,17 @@ * @author Michael Plomer <mic...@it...> */ interface Bee_MVC_IViewResolver { - /** + /** * Resolve the given view by name. * <p>Note: To allow for ViewResolver chaining, a ViewResolver should * return <code>null</code> if a view with the given name is not defined in it. * However, this is not required: Some ViewResolvers will always attempt - * to build View objects with the given name, unable to return <code>null</code> + * to build View objects with the given name, unable to return <code>null</code> * (rather throwing an exception when View creation failed). - * @param String viewName name of the view to resolve + * @param String $viewName name of the view to resolve + * @param Bee_MVC_IHttpRequest $request * @return Bee_MVC_IView the IView object, or <code>null</code> if not found * (optional, to allow for ViewResolver chaining) - * @throws Exception if the view cannot be resolved - * (typically in case of problems creating an actual View object) */ - public function resolveViewName($viewName); + public function resolveViewName($viewName, Bee_MVC_IHttpRequest $request); } -?> \ No newline at end of file Modified: trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/Redirect/AbstractRedirectStorage.php 2014-07-02 09:43:40 UTC (rev 161) @@ -42,7 +42,8 @@ abstract protected function doStoreData(array $model); /** + * @param RedirectedRequestBuilder $requestBuilder * @return \Bee_MVC_IHttpRequest */ - abstract public function restoreRequestObject(); + abstract public function restoreRequestObject(RedirectedRequestBuilder $requestBuilder); } \ No newline at end of file Modified: trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/Redirect/RedirectInfoStorage.php 2014-07-02 09:43:40 UTC (rev 161) @@ -44,11 +44,12 @@ } /** + * @param RedirectedRequestBuilder $requestBuilder * @return \Bee_MVC_IHttpRequest */ - public function restoreRequestObject() { + public function restoreRequestObject(RedirectedRequestBuilder $requestBuilder) { $request = new Bee_MVC_HttpRequest(); $request->addParameters($this->storedData); - return $request; + return $requestBuilder->massageConstructedRequest($request); } } \ No newline at end of file Modified: trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/Redirect/RedirectRequestStorage.php 2014-07-02 09:43:40 UTC (rev 161) @@ -49,13 +49,14 @@ } /** + * @param RedirectedRequestBuilder $requestBuilder * @return \Bee_MVC_IHttpRequest */ - public function restoreRequestObject() { + public function restoreRequestObject(RedirectedRequestBuilder $requestBuilder) { $_GET = $this->getParams; $_POST = $this->postParams; $_REQUEST = $this->requestParams; $_SERVER = $this->serverVars; - return new \Bee_MVC_HttpRequest(null, null, null, $this->headers); + return $requestBuilder->massageConstructedRequest(new \Bee_MVC_HttpRequest(null, null, null, $this->headers)); } } \ No newline at end of file Modified: trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php =================================================================== --- trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/Redirect/RedirectedRequestBuilder.php 2014-07-02 09:43:40 UTC (rev 161) @@ -1,7 +1,7 @@ <?php namespace Bee\MVC\Redirect; -use Bee\MVC\IRequestBuilder; +use Bee\MVC\DefaultRequestBuilder; /** * Class RedirectedRequestBuilder - restores a previously stored request from the session if applicable, or creates a @@ -9,7 +9,7 @@ * * @package Bee\MVC\Redirect */ -class RedirectedRequestBuilder implements IRequestBuilder { +class RedirectedRequestBuilder extends DefaultRequestBuilder { private $requestIdParamName = 'storedRequestId'; @@ -35,11 +35,11 @@ $id = $_GET[$this->requestIdParamName]; if(array_key_exists($id, $_SESSION) && ($storage = $_SESSION[$id]) instanceof AbstractRedirectStorage) { /** @var AbstractRedirectStorage $storage */ - return $storage->restoreRequestObject(); + return $storage->restoreRequestObject($this); } $this->throwNotFoundException($id); } - return new \Bee_MVC_HttpRequest(); + return parent::buildRequestObject(); } /** @@ -49,5 +49,4 @@ protected function throwNotFoundException($id) { throw new \Exception('Stored request with ID ' . $id . ' not found'); } - } \ No newline at end of file Added: trunk/framework/Bee/MVC/Request/DefaultAjaxDetectionStrategy.php =================================================================== --- trunk/framework/Bee/MVC/Request/DefaultAjaxDetectionStrategy.php (rev 0) +++ trunk/framework/Bee/MVC/Request/DefaultAjaxDetectionStrategy.php 2014-07-02 09:43:40 UTC (rev 161) @@ -0,0 +1,21 @@ +<?php + +namespace Bee\MVC\Request; +use Bee_MVC_IHttpRequest; + + +/** + * Class DefaultAjaxDetectionStrategy + * @package Bee\MVC\Request + */ +class DefaultAjaxDetectionStrategy implements IAjaxDetectionStrategy { + + /** + * Determine whether the given request represents an AJAX request or a regular GET / POSTback. + * @param Bee_MVC_IHttpRequest $request + * @return mixed + */ + public function isAjaxRequest(Bee_MVC_IHttpRequest $request) { + return $request->getHeader('X-Requested-With') == 'XMLHttpRequest'; + } +} \ No newline at end of file Added: trunk/framework/Bee/MVC/Request/IAjaxDetectionStrategy.php =================================================================== --- trunk/framework/Bee/MVC/Request/IAjaxDetectionStrategy.php (rev 0) +++ trunk/framework/Bee/MVC/Request/IAjaxDetectionStrategy.php 2014-07-02 09:43:40 UTC (rev 161) @@ -0,0 +1,18 @@ +<?php + +namespace Bee\MVC\Request; +use Bee_MVC_IHttpRequest; + +/** + * Interface IAjaxDetectionStrategy + * @package Bee\MVC\Request + */ +interface IAjaxDetectionStrategy { + + /** + * Determine whether the given request represents an AJAX request or a regular GET / POSTback. + * @param Bee_MVC_IHttpRequest $request + * @return mixed + */ + public function isAjaxRequest(Bee_MVC_IHttpRequest $request); +} \ No newline at end of file Modified: trunk/framework/Bee/MVC/SimpleMappingExceptionResolver.php =================================================================== --- trunk/framework/Bee/MVC/SimpleMappingExceptionResolver.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/SimpleMappingExceptionResolver.php 2014-07-02 09:43:40 UTC (rev 161) @@ -33,11 +33,6 @@ private $defaultErrorView; /** - * @var string - */ - private $ajaxViewNameSuffix = '.ajax'; - - /** * * @return array */ @@ -102,23 +97,6 @@ $viewName = $this->defaultErrorView; } - return $viewName ? new Bee_MVC_ModelAndView(array(self::MODEL_HANDLER_EXCEPTION_KEY => $ex), $this->modifyResolvedViewName($viewName, $request)) : false; + return $viewName ? new Bee_MVC_ModelAndView(array(self::MODEL_HANDLER_EXCEPTION_KEY => $ex), $viewName) : false; } - - /** - * @param $viewName - * @param Bee_MVC_IHttpRequest $request - * @return string - */ - protected function modifyResolvedViewName($viewName, Bee_MVC_IHttpRequest $request) { - return $this->isAjaxRequest($request) ? $viewName . $this->ajaxViewNameSuffix : $viewName; - } - - /** - * @param Bee_MVC_IHttpRequest $request - * @return bool - */ - protected function isAjaxRequest(Bee_MVC_IHttpRequest $request) { - return $request->getHeader('X-Requested-With') == 'XMLHttpRequest'; - } } Modified: trunk/framework/Bee/MVC/ViewResolver/Basic.php =================================================================== --- trunk/framework/Bee/MVC/ViewResolver/Basic.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/ViewResolver/Basic.php 2014-07-02 09:43:40 UTC (rev 161) @@ -18,20 +18,40 @@ /** * Basic implementation of the Bee_MVC_IViewResolver interface. Uses a Bee_IContext for view name resolution, looking up * beans of type Bee_MVC_IView by using the view name as bean name. - * + * * @author Benjamin Hartmann * @author Michael Plomer <mic...@it...> */ class Bee_MVC_ViewResolver_Basic implements Bee_MVC_IViewResolver { /** + * @var Logger + */ + protected $log; + + /** + * @return Logger + */ + protected function getLog() { + if (!$this->log) { + $this->log = Logger::getLogger(get_class($this)); + } + return $this->log; + } + + /** * Enter description here... * * @var Bee_IContext */ private $context; - + /** + * @var string + */ + private $ajaxViewNameSuffix = '.ajax'; + + /** * Enter description here... * * @param Bee_IContext $context @@ -40,7 +60,7 @@ public function setContext(Bee_IContext $context) { $this->context = $context; } - + /** * Enter description here... * @@ -49,10 +69,42 @@ public function getContext() { return $this->context; } - - public function resolveViewName($viewName) { + + /** + * @param String $viewName + * @param Bee_MVC_IHttpRequest $request + * @return Bee_MVC_IView|Object + */ + public function resolveViewName($viewName, Bee_MVC_IHttpRequest $request) { + $modifiedViewName = $this->modifyViewName($viewName, $request); + if ($modifiedViewName != $viewName) { + try { + return $this->getViewForName($viewName); + } catch (Bee_Context_BeansException $bex) { + if($this->getLog()->isDebugEnabled()) { + $this->getLog()->debug('Modified view name "' . $modifiedViewName . '" not found, trying base bean name "' . $viewName . '"', $bex); + } + } + } + return $this->getViewForName($viewName); + } + + /** + * @param string $viewName + * @return Bee_MVC_IView + */ + protected function getViewForName($viewName) { return $this->context->getBean($viewName, 'Bee_MVC_IView'); } -} -?> \ No newline at end of file + /** + * Modify the view name according to request specifics. By default, the suffix '.ajax' is appended to the view names + * for AJAX requests. Otherwise, the view name is left untouched. + * @param $viewName + * @param Bee_MVC_IHttpRequest $request + * @return string + */ + protected function modifyViewName($viewName, Bee_MVC_IHttpRequest $request) { + return $request->getAjax() ? $viewName . $this->ajaxViewNameSuffix : $viewName; + } +} \ No newline at end of file Modified: trunk/framework/Bee/MVC/ViewResolver/Loopback.php =================================================================== --- trunk/framework/Bee/MVC/ViewResolver/Loopback.php 2014-07-02 08:10:59 UTC (rev 160) +++ trunk/framework/Bee/MVC/ViewResolver/Loopback.php 2014-07-02 09:43:40 UTC (rev 161) @@ -1,18 +1,15 @@ <?php class Bee_MVC_ViewResolver_Loopback extends Bee_MVC_ViewResolver_Basic implements Bee_Context_Config_IContextAware{ - /** - * Callback that supplies the owning context to a bean instance. - * <p>Invoked after the population of normal bean properties - * but before an initialization callback such as - * {@link InitializingBean#afterPropertiesSet()} or a custom init-method. - * @param $context owning Context (never <code>null</code>). - * The bean can immediately call methods on the context. - */ + /** + * Callback that supplies the owning context to a bean instance. + * <p>Invoked after the population of normal bean properties + * but before an initialization callback such as + * {@link InitializingBean#afterPropertiesSet()} or a custom init-method. + * @param Bee_IContext $context owning context (never <code>null</code>). + * The bean can immediately call methods on the context. + */ public function setBeeContext(Bee_IContext $context) { $this->setContext($context); } - - } -?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |