Skip to content

Instantly share code, notes, and snippets.

@Swop
Last active May 10, 2020 21:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swop/5990316 to your computer and use it in GitHub Desktop.
Save Swop/5990316 to your computer and use it in GitHub Desktop.
Annotation reader in Symfony
<?php
namespace Foo\Annotations;
/**
*@Annotation
*/
class AnnotatedDescription
{
public $value;
public $type;
public $desc;
}
User Doctrine AnnotationReader to use custom annotations in a class.
(credits: Abu Ashraf Masnun - http://www.masnun.com/2012/08/12/using-annotations-in-php-with-doctrine-annotation-reader.html)
{
"require":
{
"doctrine/common": "*"
}
}
<?php
namespace Foo\Model;
use Foo\Annotations\AnnotatedDescription;
/**
* @AnnotatedDescription("The class demonstrates the use of annotations")
*/
class FooBar
{
/**
* @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();
}
}
<?php
use Doctrine\Common\Annotations\AnnotationReader;
use Foo\Model\FooBar;
$annotationReader = new AnnotationReader();
//Get class annotation
$reflectionClass = new ReflectionClass('FooBar');
$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 Foobar();
$reflectionObject = new ReflectionObject($annotationDemoObject);
$objectAnnotations = $annotationReader->getClassAnnotations($reflectionObject);
echo "========= OBJECT ANNOTATIONS =========" . PHP_EOL;
var_dump($objectAnnotations);
//Property Annotations
$reflectionProperty = new ReflectionProperty('FooBar', 'property');
$propertyAnnotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
echo "========= PROPERTY ANNOTATIONS =========" . PHP_EOL;
var_dump($propertyAnnotations);
// Method Annotations
$reflectionMethod = new ReflectionMethod('FooBar', 'getProperty');
$methodAnnotations = $annotationReader->getMethodAnnotations($reflectionMethod);
echo "========= Method ANNOTATIONS =========" . PHP_EOL;
var_dump($propertyAnnotations);
@onnyprima
Copy link

onnyprima commented Nov 25, 2019

how read spesific annotations name? example im only want to read @Assert annotation n skip all

@ismail1432
Copy link

how read spesific annotations name? example im only want to read @Assert annotation n skip all

Just do this :

$reader = new AnnotationReader();          
$reflector = new \ReflectionClass(YourClass::class);        
 // Get Annotation on the Class       
$infos = $reader->getClassAnnotation($reflector, Assert::class);

// Get annotation on property
$properties = $reflector->getProperties();
foreach ($properties as $property) {                              
$infos[] = $reader->getPropertyAnnotation($property,Assert::class)                                                   
}                                                        

@Hamza-sudo
Copy link

How can i read any annotation in my project without tell the class name or methode name ?

@ismail1432
Copy link

I don’t think it’s possible from PHP reflection API.

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