定制 dentelis/php7-attribute-reader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dentelis/php7-attribute-reader

Composer 安装命令:

composer require dentelis/php7-attribute-reader

包简介

Library for reading php8 attributes from legacy php7 code

README 文档

README

A library that brings PHP 8 attribute syntax to PHP 7.2+. The API mirrors PHP 8's native ReflectionAttribute as closely as possible, making future migration trivial.

PHP Version License

Installation

composer require dentelis/php7-attribute-reader

Quick Start

use AttributeReader\ReflectionMethodWithAttributes;

class UserController
{
    #[Route('/api/users', 'GET')]
    #[Auth('admin')]
    public function getUsers() {}
}

$method = new ReflectionMethodWithAttributes(UserController::class, 'getUsers');
$attributes = $method->getAttributes();

foreach ($attributes as $attr) {
    echo $attr->getName();          // FQCN, e.g. "App\Attributes\Route"
    print_r($attr->getArguments()); // ['/api/users', 'GET']
    $instance = $attr->newInstance(); // Route object
}

Comparison with PHP 8

// PHP 8 native:
$method = new ReflectionMethod(UserController::class, 'getUsers');
$attrs = $method->getAttributes(Route::class);
$route = $attrs[0]->newInstance();

// This library (PHP 7.2+):
$method = new ReflectionMethodWithAttributes(UserController::class, 'getUsers');
$attrs = $method->getAttributes(Route::class);
$route = $attrs[0]->newInstance();

Migration to PHP 8: replace new ReflectionMethodWithAttributes(...) with new ReflectionMethod(...).

Reflection Classes

The library provides drop-in wrappers for all four PHP reflection types. Each wrapper exposes getAttributes() with the same signature as PHP 8, and delegates all other method calls to the underlying reflection object.

ReflectionMethodWithAttributes

use AttributeReader\ReflectionMethodWithAttributes;

$method = new ReflectionMethodWithAttributes(UserController::class, 'getUsers');

// All attributes
$attrs = $method->getAttributes();

// Filter by exact class
$attrs = $method->getAttributes(Route::class);

// Filter by parent class (includes subclasses)
$attrs = $method->getAttributes(BaseAttribute::class, Attribute::IS_INSTANCEOF);

// Delegates to ReflectionMethod
echo $method->getName();
echo $method->getDeclaringClass()->getName();

ReflectionClassWithAttributes

use AttributeReader\ReflectionClassWithAttributes;

#[Controller('/api')]
#[Auth('admin')]
class UserController {}

$class = new ReflectionClassWithAttributes(UserController::class);
// or pass an object:
$class = new ReflectionClassWithAttributes(new UserController());

$attrs = $class->getAttributes();
$attrs = $class->getAttributes(Controller::class);

// Delegates to ReflectionClass
echo $class->getName();
$class->getMethods();

ReflectionPropertyWithAttributes

use AttributeReader\ReflectionPropertyWithAttributes;

class User {
    #[Column('email', unique: true)]
    public $email;
}

$prop = new ReflectionPropertyWithAttributes(User::class, 'email');
$attrs = $prop->getAttributes();
$attrs = $prop->getAttributes(Column::class);

// Delegates to ReflectionProperty
echo $prop->getName();
$prop->isPublic();

ReflectionFunctionWithAttributes

use AttributeReader\ReflectionFunctionWithAttributes;

#[Route('/handler')]
function myHandler() {}

$func = new ReflectionFunctionWithAttributes('myHandler');
$attrs = $func->getAttributes();
$attrs = $func->getAttributes(Route::class);

// Delegates to ReflectionFunction
echo $func->getName();

getAttributes() Signature

All four wrapper classes share the same getAttributes() signature:

getAttributes(?string $name = null, int $flags = 0): Attribute[]

Parameters:

  • $name — filter by FQCN (exact match by default)
  • $flags0 (default) or Attribute::IS_INSTANCEOF (includes subclasses)

Attribute — Returned Object

$attr->getName(): string;        // Fully qualified class name
$attr->getArguments(): array;    // Parsed arguments (positional and/or named)
$attr->newInstance(): object;    // Instantiates the attribute class
$attr->isRepeated(): bool;       // True if the same attribute appears more than once

Constants:

  • Attribute::IS_INSTANCEOF = 2 — matches ReflectionAttribute::IS_INSTANCEOF

Features

Filtering

$method = new ReflectionMethodWithAttributes(Controller::class, 'index');

// All attributes
$all = $method->getAttributes();

// By exact class
$routes = $method->getAttributes(Route::class);

// By parent class (includes subclasses)
$all = $method->getAttributes(BaseAttribute::class, Attribute::IS_INSTANCEOF);

Named Arguments

#[Route(path: '/users', method: 'POST')]
#[Cache(ttl: 3600, enabled: true)]
#[Route('/api/users', method: 'POST')]  // mixed positional and named

Multiple Attributes

// Separate lines
#[Route('/admin')]
#[Auth('admin')]
public function dashboard() {}

// Comma-separated
#[Route('/admin'), Auth('admin')]
public function dashboard() {}

Repeated Attributes

#[Middleware('auth')]
#[Middleware('logging')]
public function index() {}

$method = new ReflectionMethodWithAttributes(Controller::class, 'index');
$attrs = $method->getAttributes();
$attrs[0]->isRepeated(); // true

Expressions in Arguments

Arithmetic, bitwise, and string concatenation with correct operator precedence:

#[Cache(ttl: 60 * 60 * 24)]            // 86400
#[Cache(ttl: (2 + 3) * 10)]            // 50
#[Route(path: '/api' . '/users')]       // '/api/users'
#[Config(flags: 1 | 2 | 4)]            // 7
#[Cache(ttl: 2 ** 10)]                 // 1024

Supported operators: +, -, *, /, %, **, ., |, &, ^, ~, <<, >>

Constants in Arguments

Class constants, aliased imports, and global constants:

use App\Config\Limits;
use App\Config\Limits as L;

#[Cache(ttl: Limits::DEFAULT_TTL)]         // resolved via use statement
#[Cache(ttl: L::DEFAULT_TTL)]              // aliases work too
#[Cache(ttl: Limits::DEFAULT_TTL * 2)]     // expressions with constants
#[Cache(ttl: PHP_INT_SIZE)]                // global PHP constants
#[Config(name: Limits::APP_NAME . '-prod')]// concatenation with constants

Attribute Name Resolution

Attribute names are automatically resolved to FQCNs using the file's use statements and namespace — no explicit mapping required.

// In the source file:
use App\Attributes\Route;

#[Route('/users')]  // Resolved to "App\Attributes\Route"

Alternative: Static AttributeReader API

If you prefer not to use wrapper classes, AttributeReader exposes static methods that accept standard PHP reflection objects directly:

use AttributeReader\AttributeReader;

// Method attributes
AttributeReader::getMethodAttributes(ReflectionMethod $method, ?string $name = null, int $flags = 0): array;

// Class attributes
AttributeReader::getClassAttributes(ReflectionClass $class, ?string $name = null, int $flags = 0): array;

// Property attributes
AttributeReader::getPropertyAttributes(ReflectionProperty $property, ?string $name = null, int $flags = 0): array;

// Function attributes
AttributeReader::getFunctionAttributes(ReflectionFunction $function, ?string $name = null, int $flags = 0): array;

Example:

$method = new ReflectionMethod(UserController::class, 'getUsers');
$attrs = AttributeReader::getMethodAttributes($method, Route::class);
$route = $attrs[0]->newInstance();

Testing

composer test

Requirements

  • PHP 7.2 or higher

License

MIT

Credits

Developed by Dim Entelis

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固