korim / param-reader
方法参数注解/属性读取器
1.1.0
2024-05-13 18:14 UTC
Requires
- php: ^7.2 || ^8.0
- doctrine/annotations: ^1.13 || ^2.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.4
- phpunit/phpunit: ^8.5.24
Suggests
- koriym/attributes: doctrine/annotation Reader interface in order to read both doctrine/annotation and PHP 8 attributes.
This package is auto-updated.
Last update: 2024-09-13 19:23:00 UTC
README
这是一个用于从方法参数获取属性或注解的元数据读取器。
尽管 doctine/annotation 不能注解方法参数,但这个读取器将具有与方法参数同名属性注解视为方法参数元数据。
这在需要为注入准备元数据时特别有用。
安装
composer require koriym/param-reader
入门
$reader = new PramReader(); $user = $reader->getParametrAnnotation(new ReflectionParameter([Consumer::class, '__construct'], 'name'), User::class); assert($user instanceof User); $users = $reader->getParametrAnnotations(new ReflectionParameter([Consumer::class, '__construct'], 'name')); assert($users[0] instanceof User); assert($users[1] instanceof Foo);
以下两个代码提供了相同的信息。
class Consumer { private $name; public function __construct(#[User, Foo] string $name) { $this->name = $name; } }
class Consumer { /** * @User * @Foo */ private $name; public function __construct(string $name) { $this->name = $name; } }