Created
August 12, 2012 15:54
-
-
Save masnun/3332445 to your computer and use it in GitHub Desktop.
Demonstration of Doctrine Annotation Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
*@Annotation | |
*/ | |
class AnnotatedDescription | |
{ | |
public $value; | |
public $type; | |
public $desc; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": | |
{ | |
"doctrine/common": "*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where shall we store the annotation objects?
In a container? That can be injected in ... EntityMangager(s) or Repositories?