Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
abdokouta opened this issue Sep 16, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@abdokouta
Copy link

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

  • PHP version: 8.2
  • Composer version: 2.7.9
  • olvlvl/composer-attribute-collector version: @latest

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
Copy link
Owner

olvlvl commented Sep 20, 2024

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"
    ]
}

@olvlvl olvlvl added the enhancement New feature or request label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants