Skip to content

Commit

Permalink
Add support for reading annotations for method invocations #66
Browse files Browse the repository at this point in the history
  • Loading branch information
lisachenko committed Jan 20, 2014
1 parent c9a80c3 commit a35f257
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/Go/Aop/Framework/AbstractMethodInvocation.php
Expand Up @@ -8,10 +8,9 @@

namespace Go\Aop\Framework;

use ReflectionMethod;

use Go\Aop\Intercept\MethodInvocation;
use Go\Aop\Intercept\MethodInterceptor;
use Go\Aop\Support\AnnotatedReflectionMethod;
use ReflectionMethod;

/**
* Abstract method invocation implementation
Expand Down Expand Up @@ -54,7 +53,7 @@ public function __construct($className, $methodName, array $advices)
{
parent::__construct($advices);
$this->className = $className;
$this->reflectionMethod = $method = new ReflectionMethod($this->className, $methodName);
$this->reflectionMethod = $method = new AnnotatedReflectionMethod($this->className, $methodName);

// Give an access to call protected method
if ($method->isProtected()) {
Expand All @@ -68,7 +67,7 @@ public function __construct($className, $methodName, array $advices)
* <p>This method is a friendly implementation of the
* {@link Joinpoint::getStaticPart()} method (same result).
*
* @return ReflectionMethod the method being called.
* @return AnnotatedReflectionMethod the method being called.
*/
public function getMethod()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Go/Aop/Intercept/MethodInvocation.php
Expand Up @@ -8,6 +8,7 @@

namespace Go\Aop\Intercept;

use Go\Aop\Support\AnnotatedReflectionMethod;
use ReflectionMethod;

/**
Expand All @@ -28,7 +29,7 @@ interface MethodInvocation extends Invocation
* <p>This method is a friendly implementation of the
* {@link Joinpoint::getStaticPart()} method (same result).
*
* @return ReflectionMethod the method being called.
* @return ReflectionMethod|AnnotatedReflectionMethod the method being called.
*/
public function getMethod();

Expand Down
56 changes: 56 additions & 0 deletions src/Go/Aop/Support/AnnotatedReflectionMethod.php
@@ -0,0 +1,56 @@
<?php
/**
* Go! OOP&AOP PHP framework
*
* @copyright Copyright 2014, Lissachenko Alexander <lisachenko.it@gmail.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/

namespace Go\Aop\Support;

use Doctrine\Common\Annotations\Reader;
use ReflectionMethod;

/**
* Extended version of ReflectionMethod with annotation support
*/
class AnnotatedReflectionMethod extends ReflectionMethod
{
/**
* Annotation reader
*
* @var Reader
*/
private static $annotationReader = null;

/**
* Gets a method annotation.
*
* @param string $annotationName The name of the annotation.
* @return mixed The Annotation or NULL, if the requested annotation does not exist.
*/
public function getAnnotation($annotationName)
{
return self::$annotationReader->getMethodAnnotation($this, $annotationName);
}

/**
* Gets the annotations applied to a method.
*
* @return array An array of Annotations.
*/
public function getAnnotations()
{
return self::$annotationReader->getMethodAnnotations($this);
}

/**
* Injects an annotation reader
*
* @param Reader $reader
*/
public static function injectAnnotationReader(Reader $reader)
{
self::$annotationReader = $reader;
}
}
6 changes: 5 additions & 1 deletion src/Go/Core/GoAspectContainer.php
Expand Up @@ -16,6 +16,7 @@
use Go\Aop\Pointcut\PointcutLexer;
use Go\Aop\Pointcut\PointcutGrammar;
use Go\Aop\Pointcut\PointcutParser;
use Go\Aop\Support\AnnotatedReflectionMethod;
use Go\Instrument\RawAnnotationReader;

use Doctrine\Common\Annotations\AnnotationReader;
Expand Down Expand Up @@ -73,7 +74,10 @@ public function __construct()

// TODO: use cached annotation reader
$this->share('aspect.annotation.reader', function () {
return new AnnotationReader();
$reader = new AnnotationReader();
// Direct injection for AnnotatedReflectionMethod
AnnotatedReflectionMethod::injectAnnotationReader($reader);
return $reader;
});
$this->share('aspect.annotation.raw.reader', function () {
return new RawAnnotationReader();
Expand Down

0 comments on commit a35f257

Please sign in to comment.