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);
@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