pitch / annotation
Annotation abstraction for Doctrine Annotations and PHP8 attributes as (Controller) Annotations
Installs: 372
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.4
Requires (Dev)
- doctrine/annotations: ^1.12
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
- symfony/framework-bundle: ^5
- symfony/http-kernel: ^5.0.6
This package is auto-updated.
Last update: 2024-12-20 17:32:51 UTC
README
This bundle provides a unified API for Doctrine Annotations and PHP8 Attributes as (Controller) Annotations.
Usage
Read attributes and annotations
namespace App; use Attribute; use Doctrine\Common\Annotation\Reader as DoctrineReader; use Pitch\Annotation\Annotation; use Pitch\Annotation\Reader as PitchReader; /** * @Annotation */ #[Attribute] class MyAnnotation implements Annotation { public string $value; } /** * @MyAnnotation('foo') */ class MyClass { #[MyAnnotation('bar')] public function myMethod() {} } $pitchReader = new PitchReader(new DoctrineReader()); $reflection = new ReflectionMethod(MyClass::class, 'myMethod'); foreach($pitchReader->getAnnotations($reflection)->all() as $annotation) { echo $annotation->value; // outputs: foobar }
Controller request attributes
This bundle registers an EventSubscriber
on the kernel.controller
event
and stores the controller annotations on Request::attributes
,
so that they can easily be accessed on other events.
namespace App\Annotation; #[Attribute] class MyAnnotation { public function __construct( public string $value, ) {} }
namespace App\Controller; use Symfony\Component\HttpFoundation\Request; use App\Annotation\MyAnnotation; class MyController { #[MyAnnotation("foo")] #[MyAnnotation("bar")] public function __invoke(Request $request) { foreach ($request->attributes->get('_' . MyAnnotation::class) as $a) { echo $a->value; // outputs: foobar } } }