[Beeframework-svn] SF.net SVN: beeframework:[164] trunk/framework/Bee
Brought to you by:
b_hartmann,
m_plomer
From: <m_p...@us...> - 2014-07-02 12:46:26
|
Revision: 164 http://sourceforge.net/p/beeframework/code/164 Author: m_plomer Date: 2014-07-02 12:46:18 +0000 (Wed, 02 Jul 2014) Log Message: ----------- - MVC MethodNameResolver: annotation-based - ajax flag introduced Modified Paths: -------------- trunk/framework/Bee/Annotations/Utils.php trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AnnotationBased.php trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AntPath.php trunk/framework/Bee/MVC/Controller/Multiaction/RequestHandler.php Modified: trunk/framework/Bee/Annotations/Utils.php =================================================================== --- trunk/framework/Bee/Annotations/Utils.php 2014-07-02 10:50:58 UTC (rev 163) +++ trunk/framework/Bee/Annotations/Utils.php 2014-07-02 12:46:18 UTC (rev 164) @@ -25,16 +25,16 @@ class Bee_Annotations_Utils { - /** - * Get a single {@link Annotation} of <code>annotationType</code> from the - * supplied {@link Method}, traversing its super methods if no annotation - * can be found on the given method itself. - * <p>Annotations on methods are not inherited by default, so we need to handle - * this explicitly. Tge - * @param method the method to look for annotations on - * @param annotationType the annotation class to look for - * @return the annotation found, or <code>null</code> if none found - */ + /** + * Get a single {@link Annotation} of <code>annotationType</code> from the + * supplied {@link Method}, traversing its super methods if no annotation + * can be found on the given method itself. + * <p>Annotations on methods are not inherited by default, so we need to handle + * this explicitly. Tge + * @param ReflectionMethod $method + * @param $annotationClassName + * @return the annotation found, or <code>null</code> if none found + */ public static function findAnnotation(ReflectionMethod $method, $annotationClassName) { $annotatedMethod = self::getReflectionAnnotatedMethodIfNecessary($method); $annotation = $annotatedMethod->getAnnotation($annotationClassName); Modified: trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AnnotationBased.php =================================================================== --- trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AnnotationBased.php 2014-07-02 10:50:58 UTC (rev 163) +++ trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AnnotationBased.php 2014-07-02 12:46:18 UTC (rev 164) @@ -22,6 +22,8 @@ * * Handler methods in a delegate handler class must conform to the rules for handler methods for the multi action controller and be * annotated with <code>Bee_MVC_Controller_Multiaction_RequestHandler</code> annotations. + * + * todo: replace addendum annotation handling with Doctrine Annotations * * @see Bee_MVC_Controller_MultiAction * @see Bee_MVC_Controller_Multiaction_IMethodNameResolver @@ -32,37 +34,68 @@ class Bee_MVC_Controller_Multiaction_MethodNameResolver_AnnotationBased extends Bee_MVC_Controller_Multiaction_MethodNameResolver_Abstract { const DEFAULT_HTTP_METHOD_KEY = 'DEFAULT'; + const AJAX_TYPE_TRUE_KEY = '_TRUE'; + const AJAX_TYPE_FALSE_KEY = '_FALSE'; + const AJAX_TYPE_ANY_KEY = '_ANY'; const CACHE_KEY_PREFIX = 'BeeMethodNameResolverAnnotationCache_'; + /** + * @var Logger + */ + protected $log; + + /** + * @return Logger + */ + protected function getLog() { + if (!$this->log) { + $this->log = Logger::getLogger(get_class($this)); + } + return $this->log; + } + /** * @var Bee_MVC_Controller_Multiaction_MethodNameResolver_AntPath[] */ private $methodResolvers = false; + /** + * @param Bee_MVC_IHttpRequest $request + * @return string + */ public function getHandlerMethodName(Bee_MVC_IHttpRequest $request) { $this->init(); $httpMethod = strtoupper($request->getMethod()); - $handlerMethod = $this->getHandlerMethodInternal($httpMethod, $request); + $ajaxKeyPart = $this->getAjaxTypeKey($request->getAjax()); + return $this->selectHandlerMethod(array( + $httpMethod . $ajaxKeyPart, + $httpMethod . self::AJAX_TYPE_ANY_KEY, + self::DEFAULT_HTTP_METHOD_KEY . $ajaxKeyPart, + self::DEFAULT_HTTP_METHOD_KEY . self::AJAX_TYPE_ANY_KEY + ), $request); + } - if(is_null($handlerMethod)) { - $handlerMethod = $this->getHandlerMethodInternal(self::DEFAULT_HTTP_METHOD_KEY, $request); + /** + * @param array $possibleMethodKeys + * @param Bee_MVC_IHttpRequest $request + * @return string + */ + private function selectHandlerMethod(array $possibleMethodKeys, Bee_MVC_IHttpRequest $request) { + $result = null; + foreach($possibleMethodKeys as $methodKey) { + if(array_key_exists($methodKey, $this->methodResolvers)) { + $result = $this->methodResolvers[$methodKey]->getHandlerMethodName($request); + if(!is_null($result)) { + return $result; + } + } } - - return $handlerMethod; + return $result; } - - private function getHandlerMethodInternal($httpMethod, Bee_MVC_IHttpRequest $request) { - if(array_key_exists($httpMethod, $this->methodResolvers)) { - $resolver = $this->methodResolvers[$httpMethod]; - return $resolver->getHandlerMethodName($request); - } - return null; - } - protected function init() { if(!$this->methodResolvers) { @@ -73,12 +106,14 @@ try { $this->methodResolvers = Bee_Cache_Manager::retrieve(self::CACHE_KEY_PREFIX . $delegateClassName); } catch (Exception $e) { + $this->getLog()->debug('No cached method name resolvers for delegate "' . $delegateClassName . '" found, annotation parsing required'); } } if(!$this->methodResolvers) { $classReflector = new ReflectionAnnotatedClass($delegateClassName); + /** @var \Addendum\ReflectionAnnotatedMethod[] $methods */ $methods = $classReflector->getMethods(ReflectionMethod::IS_PUBLIC); $mappings = array(); @@ -87,13 +122,17 @@ if($this->getController()->isHandlerMethod($method)) { // is possible handler method, check for annotations + /** @var Bee_MVC_Controller_Multiaction_RequestHandler[] $annotations */ $annotations = $method->getAllAnnotations('Bee_MVC_Controller_Multiaction_RequestHandler'); foreach($annotations as $annotation) { $httpMethod = strtoupper($annotation->httpMethod); + $ajax = filter_var($annotation->ajax, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + $ajax = $this->getAjaxTypeKey($ajax); if(!Bee_Utils_Strings::hasText($httpMethod)) { $httpMethod = self::DEFAULT_HTTP_METHOD_KEY; } + $httpMethod .= $httpMethod . $ajax; if(!array_key_exists($httpMethod, $mappings)) { $mappings[$httpMethod] = array(); } @@ -102,9 +141,7 @@ $pathPattern = str_replace('*\/', '*/', $annotation->pathPattern); $mappings[$httpMethod][$pathPattern] = $method->getName(); } - } - } foreach($mappings as $method => $mapping) { @@ -119,5 +156,15 @@ } } } -} -?> \ No newline at end of file + + /** + * @param bool|null $ajax + * @return string + */ + protected function getAjaxTypeKey($ajax) { + if(is_null($ajax)) { + return self::AJAX_TYPE_ANY_KEY; + } + return $ajax ? self::AJAX_TYPE_TRUE_KEY : self::AJAX_TYPE_FALSE_KEY; + } +} \ No newline at end of file Modified: trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AntPath.php =================================================================== --- trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AntPath.php 2014-07-02 10:50:58 UTC (rev 163) +++ trunk/framework/Bee/MVC/Controller/Multiaction/MethodNameResolver/AntPath.php 2014-07-02 12:46:18 UTC (rev 164) @@ -77,4 +77,3 @@ return $handlerMethodName; } } -?> \ No newline at end of file Modified: trunk/framework/Bee/MVC/Controller/Multiaction/RequestHandler.php =================================================================== --- trunk/framework/Bee/MVC/Controller/Multiaction/RequestHandler.php 2014-07-02 10:50:58 UTC (rev 163) +++ trunk/framework/Bee/MVC/Controller/Multiaction/RequestHandler.php 2014-07-02 12:46:18 UTC (rev 164) @@ -28,5 +28,5 @@ class Bee_MVC_Controller_Multiaction_RequestHandler extends Annotation { public $httpMethod; public $pathPattern; + public $ajax; } -?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |