Skip to content

Commit

Permalink
Merge pull request #388 from RunOpenCode/feature/annotated-class-prop…
Browse files Browse the repository at this point in the history
…erty-access

[Feature] Implementation of annotation getters for the ClassField joinpoint (#387)
  • Loading branch information
lisachenko committed Jan 22, 2018
2 parents aed36ca + 902eb90 commit 8d52f89
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Aop/Framework/ClassFieldAccess.php
Expand Up @@ -13,6 +13,7 @@

use Go\Aop\AspectException;
use Go\Aop\Intercept\FieldAccess;
use Go\Aop\Support\AnnotatedReflectionProperty;
use ReflectionProperty;

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ public function __construct(string $className, string $fieldName, array $advices
{
parent::__construct($advices);

$this->reflectionProperty = $reflectionProperty = new ReflectionProperty($className, $fieldName);
$this->reflectionProperty = $reflectionProperty = new AnnotatedReflectionProperty($className, $fieldName);
// Give an access to protected field
if ($reflectionProperty->isProtected()) {
$reflectionProperty->setAccessible(true);
Expand All @@ -84,6 +85,8 @@ public function getAccessType(): int

/**
* Gets the field being accessed.
*
* @return ReflectionProperty|AnnotatedReflectionProperty the property which is being accessed.
*/
public function getField(): ReflectionProperty
{
Expand Down
4 changes: 2 additions & 2 deletions src/Aop/Support/AnnotatedReflectionMethod.php
Expand Up @@ -18,7 +18,7 @@
/**
* Extended version of ReflectionMethod with annotation support
*/
class AnnotatedReflectionMethod extends ReflectionMethod
class AnnotatedReflectionMethod extends ReflectionMethod implements AnnotationAccess
{
/**
* Annotation reader
Expand All @@ -28,7 +28,7 @@ class AnnotatedReflectionMethod extends ReflectionMethod
private static $annotationReader;

/**
* Gets a method annotation.
* Gets method annotation.
*
* @param string $annotationName The name of the annotation.
* @return mixed The Annotation or NULL, if the requested annotation does not exist.
Expand Down
60 changes: 60 additions & 0 deletions src/Aop/Support/AnnotatedReflectionProperty.php
@@ -0,0 +1,60 @@
<?php
declare(strict_types = 1);
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Aop\Support;

use Doctrine\Common\Annotations\Reader;
use Go\Core\AspectKernel;
use ReflectionProperty;

/**
* Extended version of ReflectionProperty with annotation support
*/
class AnnotatedReflectionProperty extends ReflectionProperty implements AnnotationAccess
{
/**
* Annotation reader
*
* @var Reader
*/
private static $annotationReader;

/**
* Gets property 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(string $annotationName)
{
return self::getReader()->getPropertyAnnotation($this, $annotationName);
}

/**
* Gets the annotations applied to a property.
*/
public function getAnnotations(): array
{
return self::getReader()->getPropertyAnnotations($this);
}

/**
* Returns an annotation reader
*/
private static function getReader(): Reader
{
if (!self::$annotationReader) {
self::$annotationReader = AspectKernel::getInstance()->getContainer()->get('aspect.annotation.reader');
}

return self::$annotationReader;
}
}
31 changes: 31 additions & 0 deletions src/Aop/Support/AnnotationAccess.php
@@ -0,0 +1,31 @@
<?php
declare(strict_types = 1);
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Aop\Support;

/**
* Provides access to annotations from reflection class/property/method.
*/
interface AnnotationAccess
{
/**
* Gets 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(string $annotationName);

/**
* Gets the annotations.
*/
public function getAnnotations(): array;
}
7 changes: 7 additions & 0 deletions tests/Go/Aop/Framework/AbstractMethodInvocationTest.php
Expand Up @@ -3,6 +3,8 @@

namespace Go\Aop\Framework;

use Go\Aop\Support\AnnotationAccess;

class AbstractMethodInvocationTest extends \PHPUnit_Framework_TestCase
{
/**
Expand All @@ -28,4 +30,9 @@ public function testStaticPartEqualsToReflectionMethod()
{
$this->assertInstanceOf('ReflectionMethod', $this->invocation->getStaticPart());
}

public function testProvidesAccessToAnnotations()
{
$this->assertInstanceOf(AnnotationAccess::class, $this->invocation->getMethod());
}
}
35 changes: 35 additions & 0 deletions tests/Go/Aop/Framework/ClassFieldAccessTest.php
@@ -0,0 +1,35 @@
<?php
declare(strict_types = 1);

namespace Go\Aop\Framework;

use Go\Aop\Support\AnnotationAccess;

class ClassFieldAccessTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ClassFieldAccess
*/
protected $classField;

public function setUp()
{
$this->classField = new ClassFieldAccess(__CLASS__, 'classField', []);
}

public function testClassFiledReturnsProperty()
{
$this->assertEquals(__CLASS__, $this->classField->getField()->class);
$this->assertEquals('classField', $this->classField->getField()->name);
}

public function testStaticPartEqualsToReflectionMethod()
{
$this->assertInstanceOf('ReflectionProperty', $this->classField->getStaticPart());
}

public function testProvidesAccessToAnnotations()
{
$this->assertInstanceOf(AnnotationAccess::class, $this->classField->getField());
}
}

0 comments on commit 8d52f89

Please sign in to comment.