定制 ondrejmirtes/composer-attribute-collector 二次开发

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

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

ondrejmirtes/composer-attribute-collector

Composer 安装命令:

composer require ondrejmirtes/composer-attribute-collector

包简介

A convenient and near zero-cost way to retrieve targets of PHP 8 attributes

README 文档

README

Release Code Coverage Downloads

composer-attribute-collector is a plugin for Composer. Its ambition is to provide a convenient way—and near zero cost—to retrieve targets of PHP 8 attributes. After the autoloader has been dumped, the plugin collects attribute targets and generates a static file. These targets can be retrieved through a convenient interface, without reflection. The plugin is useful when you need to discover attribute targets in a codebase—for known targets you can use reflection.

Features

  • Little configuration
  • No reflection in the generated file
  • No impact on performance
  • No dependency (except Composer of course)
  • A single interface to get attribute targets: classes, methods, and properties
  • Can cache discoveries to speed up consecutive runs.

Note

Currently, the plugin supports class, method, and property targets. You're welcome to contribute if you're interested in expending its support.

Usage

The following example demonstrates how targets and their attributes can be retrieved:

<?php

use olvlvl\ComposerAttributeCollector\Attributes;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\Mapping\Column;

require_once 'vendor/autoload.php';
require_once 'vendor/attributes.php'; // <-- the file created by the plugin

// Find the target classes of the AsMessageHandler attribute.
foreach (Attributes::findTargetClasses(AsMessageHandler::class) as $target) {
    // $target->attribute is an instance of the specified attribute
    // with the actual data.
    var_dump($target->attribute, $target->name);
}

// Find the target methods of the Route attribute.
foreach (Attributes::findTargetMethods(Route::class) as $target) {
    var_dump($target->attribute, $target->class, $target->name);
}

// Find the target properties of the Column attribute.
foreach (Attributes::findTargetProperties(Column::class) as $target) {
    var_dump($target->attribute, $target->class, $target->name);
}

// Find the target method-parameters of the UserInput attribute.
foreach (Attributes::findTargetMethodParameters(UserInput::class) as $target) {
    var_dump($target->attribute, $target->class, $target->method, $target->name);
}

// Filter target methods using a predicate.
// You can also filter target classes and properties.
$predicate = fn($attribute) => is_a($attribute, Route::class, true);
# or
$predicate = Attributes::predicateForAttributeInstanceOf(Route::class);

foreach (Attributes::filterTargetMethods($predicate) as $target) {
    var_dump($target->attribute, $target->class, $target->name);
}

// Find class, method, and property attributes for the ArticleController class.
$attributes = Attributes::forClass(ArticleController::class);

var_dump($attributes->classAttributes);
var_dump($attributes->methodsAttributes);
var_dump($attributes->propertyAttributes);

Getting started

Here are a few steps to get you started.

1. Configure the plugin

The plugin only inspects paths and files specified in the configuration with the include property. That is usually your "src" directory. Add this section to your composer.json file to enable the generation of the "attributes" file when the autoloader is dumped.

{
  "extra": {
    "composer-attribute-collector": {
      "include": [
        "src"
      ]
    }
  }
}

Check the Configuration options for more details.

2. Install the plugin

Use Composer to install the plugin. You will be asked if you trust the plugin and wish to activate it, select y to proceed.

composer require ondrejmirtes/composer-attribute-collector

You should see log messages similar to this:

Generating autoload files
Generating attributes file
Generated attributes file in 9.137 ms
Generated autoload files

Tip

See the Frequently Asked Questions section to automatically refresh the "attributes" file during development.

3. Autoload the "attributes" file

You can require the "attributes" file using require_once 'vendor/attributes.php'; but you might prefer to use Composer's autoloading feature:

{
  "autoloading": {
    "files": [
      "vendor/attributes.php"
    ]
  }
}

Configuration

Here are a few ways you can configure the plugin.

Including paths or files (root-only)

Use the include property to define the paths or files to inspect for attributes. Without this property, the "attributes" file will be empty.

The specified paths are relative to the composer.json file, and the {vendor} placeholder is replaced with the path to the vendor folder.

{
  "extra": {
    "composer-attribute-collector": {
      "include": [
        "path-or-file/to/include"
      ]
    }
  }
}

Excluding paths or files (root-only)

Use the exclude property to exclude paths or files from inspection. This is handy when files cause issues or have side effects.

The specified paths are relative to the composer.json file, and the {vendor} placeholder is replaced with the path to the vendor folder.

{
  "extra": {
    "composer-attribute-collector": {
      "exclude": [
        "path-or-file/to/exclude"
      ]
    }
  }
}

Cache discoveries between runs

The plugin is able to maintain a cache to reuse discoveries between runs. To enable the cache, set the environment variable COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE to 1, yes, or true. Cache items are persisted in the .composer-attribute-collector directory, you might want to add it to your .gitignore file.

COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=1 composer dump-autoload

Test drive with the Symfony Demo

You can try the plugin with a fresh installation of the Symfony Demo Application.

Tip

The demo application configured with the plugin is available on GitHub.

See the Getting started section to set up the plugin. If all went well, the file vendor/attributes.php should be available.

Now, you can try to get the controller methods tagged as routes. Create a PHP file with the following content and run it:

<?php

use olvlvl\ComposerAttributeCollector\Attributes;
use Symfony\Component\Routing\Annotation\Route;

require_once 'vendor/autoload.php';

$predicate = Attributes::predicateForAttributeInstanceOf(Route::class);
$targets = Attributes::filterTargetMethods($predicate);

foreach ($targets as $target) {
    echo "action: $target->class#$target->name, path: {$target->attribute->getPath()}\n";
}

You should see an output similar to the following excerpt:

action: App\Controller\BlogController#index, path: /
action: App\Controller\BlogController#index, path: /rss.xml
action: App\Controller\BlogController#index, path: /page/{page<[1-9]\d{0,8}>}
action: App\Controller\BlogController#postShow, path: /posts/{slug}
action: App\Controller\BlogController#commentNew, path: /comment/{postSlug}/new
action: App\Controller\BlogController#search, path: /search

Frequently Asked Questions

Do I need to generate an optimized autoloader?

You don't need to generate an optimized autoloader for this to work. The plugin uses code similar to Composer to find classes. Anything that works with Composer should work with the plugin.

Can I use the plugin during development?

Yes, you can use the plugin during development, but keep in mind the "attributes" file is only generated after the autoloader is dumped. If you modify attributes you will have to run composer dump-autoload to refresh the "attributes" file.

As a workaround you could have watchers on the directories that contain classes with attributes to run XDEBUG_MODE=off composer dump-autoload when you make changes. PhpStorm offers file watchers. You could also use spatie/file-system-watcher, it only requires PHP. If the plugin is too slow for your liking, try running the command with COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=yes, it will enable caching and speed up consecutive runs.

Continuous Integration

The project is continuously tested by GitHub actions.

Tests Static Analysis Code Style

Code of Conduct

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you're expected to uphold this code.

Contributing

See CONTRIBUTING for details.

ondrejmirtes/composer-attribute-collector 适用场景与选型建议

ondrejmirtes/composer-attribute-collector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.62M 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ondrejmirtes/composer-attribute-collector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ondrejmirtes/composer-attribute-collector 我们能提供哪些服务?
定制开发 / 二次开发

基于 ondrejmirtes/composer-attribute-collector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-05-24