Nette\Annotations
Rozšiřuje schopnosti Reflection API o podporu phpDoc/JavaDoc anotací.
Anotovat lze třídy, metody a vlastnosti tříd. Zapisují se přímo do zdrojového kódu:
/**
* @title("Administration")
* @secured(role = "admin", level = 2)
* @persistent(grid, paginator)
*/
class AnnotatedClass {
/** @persistent */
public $id;
/** @secured(role = "guest") */
public function renderLogin()
{}
}
Protože interní třídy PHP nelze rozšířit o nové metody (narozdíl
třeba od tříd odvozených od Nette\Object), je podpora anotací
umístěna do třídy Nette\Annotations:
$reflection = new ReflectionClass('AnnotatedClass');
// existuje anotace 'title'?
echo Annotations::has($reflection, 'title'); // TRUE
// anotace jsou case-sensitive
echo Annotations::has($reflection, 'TITLE'); // FALSE
// hodnota anotace
echo Annotations::get($reflection, 'title'); // string "Administration"
$value = Annotations::get($reflection, 'secured');
// -> object(stdClass) {
// "role" => string "admin"
// "level" => int(2)
// }
$value = Annotations::get($reflection, 'persistent');
// -> array('grid', 'paginator')
Obdobným způsobem lze zjišťovat anotace u metod nebo vlastností:
$reflection = new ReflectionProperty('AnnotatedClass', 'id');
echo Annotations::get($reflection, 'persistent'); // TRUE
$reflection = new ReflectionMethod('AnnotatedClass', 'renderLogin');
echo Annotations::get($reflection, 'secured')->role; // "guest"
Nette\Annotations podporuje i vícenásobné anotace:
/**
* @title(value="Administration", lang=en)
* @title(value="Administrace", lang=cs)
* @title(value="Administrácia", lang=sk)
* @renderable
*/
class AnnotatedClass { ... }
$reflection = new ReflectionClass('AnnotatedClass');
$value = Annotations::getAll($reflection);
// -> array of all 4 annotations
$value = Annotations::getAll($reflection, 'title');
// -> array of 3 @title annotations
