olvlvl / composer-attribute-collector

A convenient and near zero-cost way to retrieve targets of PHP 8 attributes
Other
145 stars 3 forks source link

Plugin Loads Before Custom Autoload Logic, Causing Undefined Class Errors #30

Open abdokouta opened 1 month ago

abdokouta commented 1 month ago

Bug Report

Package: olvlvl/composer-attribute-collector

Issue Summary

The Plugin class from olvlvl/composer-attribute-collector loads before my custom autoload logic, which registers classes dynamically. As a result, classes that should be available during the autoload process are not found, leading to undefined class errors.

I have a custom command (php artisan autoload) that is responsible for loading additional classes after the post-autoload-dump event. However, since the Plugin class registers its autoload mappings first, my classes are not recognized when the composer-attribute-collector runs.

Steps to Reproduce

  1. Define a custom autoloading command (php artisan autoload) that dynamically loads classes after the post-autoload-dump event.

  2. Configure olvlvl/composer-attribute-collector with the following in composer.json:

    "extra": {
        "class": "olvlvl\\ComposerAttributeCollector\\Plugin",
        "composer-attribute-collector": {
            "include": [
                "tests"
            ],
            "exclude": [
                "tests/Acme/PSR4/IncompatibleSignature.php"
            ]
        }
    }
  3. Run composer dump-autoload or any related command.

  4. Observe that classes expected by my command are not loaded, resulting in class not found errors.

Expected Behavior

The Plugin class from olvlvl/composer-attribute-collector should allow custom autoload logic to run before it attempts to register classes, or provide a way to delay its execution until after my custom autoload process has completed.

Actual Behavior

The Plugin class is executed first, which results in classes being unavailable for the autoload process, leading to undefined class errors.

Environment

Possible Solution

Allow the Plugin class to be manually triggered after custom autoload logic is executed, or provide a configuration option to defer its execution until after custom autoload registration is complete.

Additional Context

I currently run a custom command (php artisan autoload) in the post-autoload-dump section of composer.json:

"scripts": {
    "post-autoload-dump": [
        "@php artisan autoload"
    ]
}

However, the Plugin class runs too early, preventing my autoload command from functioning properly.

olvlvl commented 1 month ago

Thanks for opening the issue. Could you try the following?

1. Comment the listener registration in olvlvl/composer-attribute-collector/src/Plugin.php:

    public static function getSubscribedEvents(): array
    {
        return [
            // 'post-autoload-dump' => 'onPostAutoloadDump',
        ];
    }

2. Add the plugin after your autoload registration:

"scripts": {
    "post-autoload-dump": [
        "@php artisan autoload",
        "olvlvl\ComposerAttributeCollector\Plugin::onPostAutoloadDump"
    ]
}