PHP code example of quellabs / annotation-reader

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);

/**
 * @Route("/api/users")
 * @InterceptWith("AuthValidator")
 * @InterceptWith("LoggingInterceptor")
 * @Cache(ttl=3600)
 */
public function getUsers() { /* ... */ }

$annotations = $reader->getMethodAnnotations(MyClass::class, 'getUsers');
$array = $annotations->toArray();

// Result structure:
// [
//     'App\Annotations\Route' => Route("/api/users"),
//     'App\Annotations\InterceptWith' => InterceptWith("AuthValidator"),
//     0 => InterceptWith("LoggingInterceptor"),  // Second InterceptWith uses numeric key
//     'App\Annotations\Cache' => Cache(ttl=3600)
// ]

// Access patterns:
$route = $array[Route::class];                    // First (and only) Route
$firstInterceptor = $array[InterceptWith::class]; // First InterceptWith
$secondInterceptor = $array[0];                   // Second InterceptWith
$cache = $array[Cache::class];                    // Cache annotation

$annotations = $reader->getMethodAnnotations(MyClass::class, 'getUsers');
$array = $annotations->toIndexedArray();

// Result structure:
// [
//     0 => Route("/api/users"),
//     1 => InterceptWith("AuthValidator"),
//     2 => InterceptWith("LoggingInterceptor"),
//     3 => Cache(ttl=3600)
// ]

// Access patterns:
$firstAnnotation = $array[0];   // Route
$secondAnnotation = $array[1];  // First InterceptWith
$thirdAnnotation = $array[2];   // Second InterceptWith

// Iterate through all annotations
foreach ($array as $index => $annotation) {
    echo "Annotation {$index}: " . get_class($annotation) . "\n";
}

$annotations = $reader->getMethodAnnotations(MyClass::class, 'getUsers');
$array = $annotations->toGroupedArray();

// Result structure:
// [
//     'App\Annotations\Route' => [
//         0 => Route("/api/users")
//     ],
//     'App\Annotations\InterceptWith' => [
//         0 => InterceptWith("AuthValidator"),
//         1 => InterceptWith("LoggingInterceptor")
//     ],
//     'App\Annotations\Cache' => [
//         0 => Cache(ttl=3600)
//     ]
// ]

// Access patterns:
$routes = $array[Route::class];           // Array of Route annotations
$interceptors = $array[InterceptWith::class]; // Array of InterceptWith annotations

// Process all interceptors
foreach ($array[InterceptWith::class] as $interceptor) {
    // Handle each interceptor
}

// Check if specific annotation type exists
if (isset($array[Cache::class])) {
    $cacheAnnotations = $array[Cache::class];
}

// Get count of specific annotation type
$interceptorCount = count($array[InterceptWith::class] ?? []);

/**
 * @Table(name="products")
 * @Entity
 * @Cache(ttl=3600, enabled=true)
 */
class Product {
    /**
     * @Column(type="integer", primary=true, autoincrement=true)
     */
    private $id;
    
    /**
     * @Column(type="string", length=255)
     * @Validate("

use App\Models\User;
use App\Services\ValidationService;
use App\Events\UserCreated;

/**
 * @Entity(repository=UserRepository::class)
 * @EventListener(event=UserCreated::class)
 */
class UserService {
    /**
     * @Inject(service=ValidationService::class)
     * @Cache(driver=RedisDriver::class)
     */
    private $validator;
    
    /**
     * @Transform(transformer=UserTransformer::class)
     * @Authorize(policy=UserPolicy::class)
     */
    public function getUser(int $id): User {
        // Method implementation
    }
}

/**
 * @ComplexAnnotation(
 *     type=User::class,
 *     name="user_service",
 *     priority=10,
 *     enabled=true,
 *     tags={"user", "service"}
 * )
 */
class UserService {
    // Class implementation
}