1. Go to this page and download the library: Download quellabs/annotation-reader library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
quellabs / annotation-reader example snippets
use Quellabs\AnnotationReader\AnnotationsReader;
use Quellabs\AnnotationReader\Configuration;
// Create configuration
$config = new Configuration();
$config->setAnnotationCachePath(__DIR__ . '/cache');
$config->setUseAnnotationCache(true);
// Create annotation reader
$reader = new AnnotationsReader($config);
// Get annotations for a class
$classAnnotations = $reader->getClassAnnotations(MyClass::class);
// Get annotations for a class, filtered by a specific annotation
$classAnnotations = $reader->getClassAnnotations(MyClass::class, SomeAnnotation::class);
// Get annotations for a property
$propertyAnnotations = $reader->getPropertyAnnotations(MyClass::class, 'propertyName');
// Get annotations for a property, filtered by a specific annotation
$propertyAnnotations = $reader->getPropertyAnnotations(MyClass::class, 'propertyName', SomeAnnotation::class);
// Get annotations for a method
$methodAnnotations = $reader->getMethodAnnotations(MyClass::class, 'methodName');
// Get annotations for a method, filtered by a specific annotation
$methodAnnotations = $reader->getMethodAnnotations(MyClass::class, 'methodName', SomeAnnotation::class);
$annotations = $reader->getMethodAnnotations(MyClass::class, 'myMethod');
// Array access by class name - returns first annotation of that type
$interceptor = $annotations[InterceptWith::class];
// Array access by numeric index - returns annotation at that position
$firstAnnotation = $annotations[0];
$secondAnnotation = $annotations[1];
// Iterate through all individual annotations
foreach ($annotations as $annotation) {
// Each iteration gives you a single annotation object
}
// Get all annotations of a specific type (returns AnnotationCollection)
$allInterceptors = $annotations->all(InterceptWith::class);
// Check if multiple annotations of same type exist
if ($annotations->hasMultiple(InterceptWith::class)) {
// Process multiple interceptors
}
// Collection methods
$count = count($annotations);
$isEmpty = $annotations->isEmpty();
$firstAnnotation = $annotations->first();
$lastAnnotation = $annotations->last();
// Filtering returns a new AnnotationCollection
$filtered = $annotations->filter(function($annotation) {
return $annotation->isActive();
});
// Chaining operations
$activeInterceptors = $annotations
->all(InterceptWith::class)
->filter(fn($interceptor) => $interceptor->isActive());
/**
* @InterceptWith("AuthValidator")
* @InterceptWith("LoggingInterceptor")
* @Route("/api/users")
*/
public function getUsers() { /* ... */ }
$annotations = $reader->getMethodAnnotations(MyClass::class, 'getUsers');
// Get first InterceptWith annotation
$firstInterceptor = $annotations[InterceptWith::class];
// Get all InterceptWith annotations as a collection
$allInterceptors = $annotations->all(InterceptWith::class);
// Check for multiple InterceptWith annotations
if ($annotations->hasMultiple(InterceptWith::class)) {
foreach ($allInterceptors as $interceptor) {
// Process each interceptor
}
}
// Get single Route annotation
$route = $annotations[Route::class];
// Iterate through all annotations (individual objects)
foreach ($annotations as $annotation) {
// Gets: AuthValidator, LoggingInterceptor, Route
}
// Filter by specific annotation class
$interceptors = $reader->getMethodAnnotations(MyClass::class, 'myMethod', InterceptWith::class);
// Or filter with custom logic
$activeAnnotations = $annotations->filter(fn($annotation) => $annotation->isActive());
// All results are AnnotationCollection with consistent access
$first = $interceptors[0]; // First annotation
$count = count($interceptors); // Total count
foreach ($interceptors as $annotation) {
// Iterate individual annotations
}
// Chain operations
$result = $annotations
->filter(fn($a) => $a->isActive())
->all(InterceptWith::class);