音高 / 注释
Doctrine 注释和 PHP8 属性的注释抽象,用于(控制器)注释
v1.0.0
2021-05-18 11:47 UTC
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-09-20 17:02:56 UTC
README
此包为 Doctrine 注释和 PHP8 属性提供统一的 API,作为(控制器)注释。
使用方法
读取属性和注释
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 }
控制器请求属性
此包在 kernel.controller 事件上注册了一个 EventSubscriber,并将控制器注释存储在 Request::attributes 中,以便可以在其他事件中轻松访问。
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 } } }