Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Working with annotations using the doctrine annotation reader
{
"name": "working-annotation",
"license": "MIT",
"type": "project",
"description": "Using annotations",
"require":
{
"doctrine/common": "*"
},
"authors": [
{
"name": "Leonardo Rifeli",
"email": "leonardorifeli@gmail.com",
"homepage": "http://leonardorifeli.com",
"role": "Back-end Developer"
}
]
}
<?php
/**
* @PeopleAnnotation(description="Get all information about a people", type="class")
*/
class People {
/**
* @PeopleAnnotation(description="Use to people name", type="attribute")
*/
private $name;
/**
* @PeopleAnnotation(description="Use to people birth date", type="attribute")
*/
private $birthDate;
/**
* @PeopleAnnotation(description="Get people name", type="method")
*/
public function getName() {
return $this->name;
}
/**
* @PeopleAnnotation(description="Get people birth date", type="method")
*/
public function getBirthDate() {
return $this->birthDate;
}
}
<?php
/**
* @Annotation
*/
class PeopleAnnotation {
public $description;
public $type;
}
<?php
require_once 'vendor/autoload.php';
require_once 'PeopleAnnotation.php';
require_once 'People.php';
use Doctrine\Common\Annotations\AnnotationReader;
$annotationReader = new AnnotationReader();
$reflectionClass = new ReflectionClass('People');
$classAnnotations = $annotationReader->getClassAnnotations($reflectionClass);
echo "CLASS ANNOTATIONS:";
var_dump($classAnnotations);
echo '<hr/>';
$people = new People();
$reflectionObject = new ReflectionObject($people);
$objectAnnotations = $annotationReader->getClassAnnotations($reflectionObject);
echo "OBJECT ANNOTATIONS:";
var_dump($objectAnnotations);
echo '<hr/>';
$reflectionProperty = new ReflectionProperty('People', 'name');
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
echo "PROPERTY ANNOTATION NAME:";
var_dump($propertyAnnotations);
echo '<hr/>';
$reflectionProperty = new ReflectionProperty('People', 'birthDate');
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
echo "PROPERTY ANNOTATION BIRTH DATE";
var_dump($propertyAnnotations);
echo '<hr/>';
$reflectionMethod = new ReflectionMethod('People', 'getName');
$methodAnnotations = $annotationReader->getMethodAnnotations($reflectionMethod);
echo "Method ANNOTATIONS getName:";
var_dump($propertyAnnotations);
echo '<hr/>';
$reflectionMethod = new ReflectionMethod('People', 'getBirthDate');
$methodAnnotations = $annotationReader->getMethodAnnotations($reflectionMethod);
echo "Method ANNOTATIONS getBirthDate: ";
var_dump($propertyAnnotations);
CLASS ANNOTATIONS:
array (size=1)
0 =>
object(PeopleAnnotation)[11]
public 'description' => string 'Get all information about a people' (length=34)
public 'type' => string 'class' (length=5)
OBJECT ANNOTATIONS:
array (size=1)
0 =>
object(PeopleAnnotation)[15]
public 'description' => string 'Get all information about a people' (length=34)
public 'type' => string 'class' (length=5)
PROPERTY ANNOTATION NAME:
array (size=1)
0 =>
object(PeopleAnnotation)[18]
public 'description' => string 'Use to people name' (length=18)
public 'type' => string 'attribute' (length=9)
PROPERTY ANNOTATION BIRTH DATE
array (size=1)
0 =>
object(PeopleAnnotation)[19]
public 'description' => string 'Use to people birth date' (length=24)
public 'type' => string 'attribute' (length=9)
Method ANNOTATIONS getName:
array (size=1)
0 =>
object(PeopleAnnotation)[19]
public 'description' => string 'Use to people birth date' (length=24)
public 'type' => string 'attribute' (length=9)
Method ANNOTATIONS getBirthDate:
array (size=1)
0 =>
object(PeopleAnnotation)[19]
public 'description' => string 'Use to people birth date' (length=24)
public 'type' => string 'attribute' (length=9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment