Skip to content

Instantly share code, notes, and snippets.

@masnun
Created August 12, 2012 15:54
Show Gist options
  • Save masnun/3332445 to your computer and use it in GitHub Desktop.
Save masnun/3332445 to your computer and use it in GitHub Desktop.
Demonstration of Doctrine Annotation Reader
<?php
/**
*@Annotation
*/
class AnnotatedDescription
{
public $value;
public $type;
public $desc;
}
{
"require":
{
"doctrine/common": "*"
}
}
<?php
// Composer autoloader - used for autoloading doctrine components
require_once 'vendor/autoload.php';
// Contains blueprints of the different annotations types as classes
require_once 'annotations.php';
//The example class to demonstrate annotations
/**
* @AnnotatedDescription("The class demonstrates the use of annotations")
*/
class AnnotationDemo
{
/**
* @AnnotatedDescription("The property is made private for a subtle reason")
*/
private $property = "I am a private property!";
/**
* @AnnotatedDescription(desc ="The property is made private for a subtle reason", type="getter")
*/
public function getProperty()
{
return $this->getProperty();
}
}
// Lets parse the annotations
use Doctrine\Common\Annotations\AnnotationReader;
$annotationReader = new AnnotationReader();
//Get class annotation
$reflectionClass = new ReflectionClass('AnnotationDemo');
$classAnnotations = $annotationReader->getClassAnnotations($reflectionClass);
echo "========= CLASS ANNOTATIONS =========" . PHP_EOL;
var_dump($classAnnotations);
// You can also pass ReflectionObject to the same method to read annotations in runtime
$annotationDemoObject = new AnnotationDemo();
$reflectionObject = new ReflectionObject($annotationDemoObject);
$objectAnnotations = $annotationReader->getClassAnnotations($reflectionObject);
echo "========= OBJECT ANNOTATIONS =========" . PHP_EOL;
var_dump($objectAnnotations);
//Property Annotations
$reflectionProperty = new ReflectionProperty('AnnotationDemo', 'property');
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
echo "========= PROPERTY ANNOTATIONS =========" . PHP_EOL;
var_dump($propertyAnnotations);
// Method Annotations
$reflectionMethod = new ReflectionMethod('AnnotationDemo', 'getProperty');
$methodAnnotations = $annotationReader->getMethodAnnotations($reflectionMethod);
echo "========= Method ANNOTATIONS =========" . PHP_EOL;
var_dump($propertyAnnotations);
@edisonnica
Copy link

last line should be
var_dump($methodAnnotations);

@FabianKoestring
Copy link

This is how to get single property annotation

$annotationReader = new AnnotationReader();
$reflectionProperty = new ReflectionProperty('AnnotationDemo', 'property');
$propertyAnnotation = $annotationReader->getPropertyAnnotation($reflectionProperty, 'Doctrine\ORM\Mapping\Column');

echo "=========   PROPERTY ANNOTATION =========" . PHP_EOL;
var_dump($propertyAnnotation);

@DarkMukke
Copy link

DarkMukke commented Sep 28, 2016

Created a wrapper (for singletons)

<?php

use Doctrine\Common\Annotations\SimpleAnnotationReader as DocReader;

/**
 * Helper AnnotationReader
 */
class AnnotationReader
{

    /**
     * @param $class
     * @return null|\StdClass
     */
    public static function getClass($class)
    {
        $reader = new DocReader;
        $reflector = new \ReflectionClass($class);
        return $reader->getClassAnnotations($reflector);
    }

    /**
     * @param $class
     * @param $property
     * @return array
     */
    public static function getProperty($class, $property)
    {
        $reader = new DocReader;
        $reflector = new \ReflectionProperty($class, $property);
        return $reader->getPropertyAnnotations($reflector);
    }

    /**
     * @param $class
     * @param $method
     * @return array
     */
    public static function getMethod($class, $method)
    {
        $reader = new DocReader;
        $reflector = new \ReflectionMethod($class, $method);
        return $reader->getMethodAnnotations($reflector);
    }

}

@sweiguny
Copy link

Where shall we store the annotation objects?
In a container? That can be injected in ... EntityMangager(s) or Repositories?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment