rudra/annotation
Composer 安装命令:
composer require rudra/annotation
包简介
Class & Method Annotation Reader
README 文档
README
Annotations and attributes reader | API
Installation
composer require rudra/annotation
Modern metadata reader for PHP 8+ attributes with legacy annotation support.
Using
$annotation = new Annotation();
🎯 Recommended: PHP 8+ Attributes
$annotation->getAttributes(PageController::class); $annotation->getAttributes(PageController::class, "indexAction");
#[Routing(url:'')] #[Defaults(name:'user1', lastname:'sample', age:'0', address:['country' => 'Russia', 'state' => 'Tambov'], phone:'000-00000000')] #[assertResult(false)] #[Validate(name:'min:150', phone:'max:9')] #[Middleware('Middleware', params:['int1' => '123'])] #[Annotation("param1", param2:'param2', param3:['param1', 'param2' => 'param2'])] class PageController { #[Routing(url:'')] #[Defaults(name:'user1', lastname:'sample', age:'0', address:['country' => 'Russia', 'state' => 'Tambov'], phone:'000-00000000')] #[assertResult(false)] #[Validate(name:'min:150', phone:'max:9')] #[Middleware('Middleware', params:['int1' => '123'])] #[Annotation("param1", param2:'param2', param3:['param1', 'param2' => 'param2'])] public function indexAction() { // Your code } }
📜 Legacy: Annotations
Note: Annotations are supported for backward compatibility with legacy projects. For new projects, use PHP 8+ attributes.
$annotation->getAnnotations(PageController::class); $annotation->getAnnotations(PageController::class, "indexAction");
/** * @Routing(url = '') * @Defaults(name = 'user1', lastname = 'sample', age='0', address = {country : 'Russia'; state : 'Tambov'}, phone = '000-00000000') * @assertResult(false) * @Validate(name = 'min:150', phone = 'max:9') * @Middleware('Middleware', params = {int1 : '123'}) * @Annotation(param1, param2 = 'param2', param3={param1;param2:'param2'}) */ class PageController { /** * @Routing(url = '') * @Defaults(name = 'user1', lastname = 'sample', age='0', address = {country : 'Russia'; state : 'Tambov'}, phone = '000-00000000') * @assertResult(false) * @Validate(name = 'min:150', phone = 'max:9') * @Middleware('Middleware', params = {int1 : '123'}) * @Annotation(param1, param2 = 'param2', param3={param1;param2:'param2'}) */ public function indexAction() { // Your code } }
📊 Result in both cases:
[
'Routing' => [['url' => ""]],
'Defaults' => [
[
'name' => "user1",
'lastname' => "sample",
'age' => "0",
'address' => [
'country' => "Russia",
'state' => "Tambov",
],
'phone' => "000-00000000",
],
],
'assertResult' => [["false"]],
'Validate' => [
[
'name' => "min:150",
'phone' => "max:9",
],
],
'Middleware' => [
[
"'Middleware'",
'params' => [
'int1' => '123',
],
],
],
"Annotation" => [
[
"param1",
"param2" => "param2",
"param3" => [
"param1",
"param2" => "param2",
],
],
],
];
⚠️ Known Limitations
When using nested arrays (in curly braces
{}), ensure that the values do not contain the array assignment symbol (:). The parser uses simple splitting by this symbol and does not escape it inside quotes. String values (with=) are handled correctly even if they contain multiple=symbols.
✅ Works correctly:
/** * @Config(settings={theme:'dark'; lang:'ru'}) * @Routing(url='http://site.com?a=1&b=2') */
❌ Breaks array parsing:
/** * @Config(settings={url:'http://site.com:8080'}) */
Note: These limitations apply only to legacy annotations. PHP 8+ attributes do not have these restrictions. ✅ All cases work correctly:
#[Config(settings: ['theme' => 'dark', 'lang' => 'ru'])] #[Routing(url: 'http://site.com?a=1&b=2')] #[Config(settings: ['url' => 'http://site.com:8080'])] // ✅ Works!
License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0) — a free, open-source license that:
- Requires preservation of copyright and license notices,
- Allows commercial and non-commercial use,
- Requires that any modifications to the original files remain open under MPL-2.0,
- Permits combining with proprietary code in larger works.
📄 Full license text: LICENSE
🌐 Official MPL-2.0 page: https://mozilla.org/MPL/2.0/
统计信息
- 总下载量: 718
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MPL-2.0
- 更新时间: 2018-05-28