Problem/Motivation

When subscribing to events of the Scaffold plugin from other composer packages, the dispatched events are not received by listeners, because the event dispatcher is initialised as a new instance rather than re-using an existing instance coming from composer. This makes it "forget" all the listeners collected by composer.

Current code:

$dispatcher = new EventDispatcher($this->composer, $this->io);
$dispatcher->dispatch(self::PRE_DRUPAL_SCAFFOLD_CMD);

Fixed code:

$dispatcher = $this->composer->getEventDispatcher();
$dispatcher->dispatch(self::PRE_DRUPAL_SCAFFOLD_CMD);

Steps to reproduce

1. Create a plugin class with the following code

<?php

namespace YourNamespace\Composer\Plugin\YourPlugin;

use Composer\Composer;
use Composer\EventDispatcher\Event;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;

/**
 * Composer plugin for handling drupal scaffold.
 *
 * @internal
 */
class Plugin implements PluginInterface, EventSubscriberInterface {
  
  /**
   * {@inheritdoc}
   */
  public function activate(Composer $composer, IOInterface $io) {
  }

  /**
   * {@inheritdoc}
   */
  public function deactivate(Composer $composer, IOInterface $io) {
  }

  /**
   * {@inheritdoc}
   */
  public function uninstall(Composer $composer, IOInterface $io) {
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      'pre-drupal-scaffold-cmd' => 'preDrupalScaffoldCmd',
      'post-drupal-scaffold-cmd' => 'postDrupalScaffoldCmd',
    ];
  }

  public static function preDrupalScaffoldCmd(Event $event) {
    print 'preDrupalScaffoldCmd' . PHP_EOL;
  }

  public static function postDrupalScaffoldCmd(Event $event) {
    print 'postDrupalScaffoldCmd' . PHP_EOL;
  }

}

2. Create a package with the following `composer.json` and place the file with Plugin class into `src` directory and provide references:


{
    "name": "yournamespace/yourplugin",
    "type": "composer-plugin",
    "require": {
        "php": ">=8.2",
        "composer-plugin-api": "^2"
    },
    "autoload": {
        "psr-4": {
            "YourNamespace\\Composer\\Plugin\\YourPlugin\\": "src"
        }
    },
    "extra": {
        "class": "YourNamespace\\Composer\\Plugin\\YourPlugin\\Plugin"
    }
}

3. Create a consumer website package:
composer create-project drupal-composer/drupal-project:10.x-dev some-dir --no-interaction

4. Add your custom package as a dependency to the newly created project.

5. Run `composer drupal:scaffold`

6. Observe that plugin's listeners were ignored.

Proposed resolution

Use the event dispatcher from provided by composer:

$dispatcher = $this->composer->getEventDispatcher();

Remaining tasks

1. Update event dispatcher.
2. Add tests. Note that there are currently no tests for this functionality, so initial tests would need to be added as well.

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

None

CommentFileSizeAuthor
#6 scaffold-events-failed.patch5.27 KBtannguyenhn

Issue fork drupal-3438034

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alex.skrypnyk created an issue. See original summary.

alex.skrypnyk’s picture

adwivedi008 made their first commit to this issue’s fork.

adwivedi008’s picture

Status: Active » Needs review

Hi @alex.skrypnyk
Thanks for raising the issue and proposing solution

Implemented the proposed solution and created MR
It needs review so moving the issue to needs review

tannguyenhn’s picture

StatusFileSize
new5.27 KB

tannguyenhn’s picture

Hi @alex

I have provided the patch to proof the bug and MR to fix the bug.

alex.skrypnyk changed the visibility of the branch 3438034-composer-scaffold-plugin to hidden.

alex.skrypnyk’s picture

Assigned: Unassigned » alex.skrypnyk
Status: Needs review » Reviewed & tested by the community

Looks like MR has passed.

The code change in the scaffold plugin works for me.

Waiting for the core team to check the code.

Also wondering if this can be cherry-picked into 10.x branch as this plugin is not specific to a Drupal version.

alexpott’s picture

Assigning issue credit.

alex.skrypnyk’s picture

Issue summary: View changes
alexpott’s picture

Title: Composer Scaffold plugin event listeners do not receive event dispatch" is mostly correct but could be clearer. Here’s a revised version for better clarity: "Event listeners in the Composer Scaffold plugin do not receive event dispatches » Fix Composer Scaffold plugin event listeners

The title doesn't make sense to me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Left a comment on the MR... just need to move some test code around so we're running the tests in ComposerHookTest twice.

alex.skrypnyk’s picture

thanks for review @alexpott
we are working on the update to the MR.

alex.skrypnyk’s picture

Status: Needs work » Needs review
greg.1.anderson’s picture

Patch looks good to me -- thanks for finding this and submitting a test. It definitely should have been like this from the beginning.

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Yep this looks correct to me too, thanks for adding the test coverage - confirmed that without the change that the new test fails because the listener is not discovered.

longwave’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed f35f0419c7 to 11.x and 2fc0e9b818 to 10.3.x and 46c3c5d946 to 10.2.x. Thanks!

  • alexpott committed 46c3c5d9 on 10.2.x
    Issue #3438034 by tannguyenhn, adwivedi008, alex.skrypnyk, alexpott: Fix...

  • alexpott committed 2fc0e9b8 on 10.3.x
    Issue #3438034 by tannguyenhn, adwivedi008, alex.skrypnyk, alexpott: Fix...

  • alexpott committed f35f0419 on 11.x
    Issue #3438034 by tannguyenhn, adwivedi008, alex.skrypnyk, alexpott: Fix...

  • alexpott committed c3630f3e on 10.2.x
    Issue #3438034 follow-up by alexpott: Fix Composer Scaffold plugin event...

  • alexpott committed 0a3a3854 on 10.3.x
    Issue #3438034 follow-up by alexpott: Fix Composer Scaffold plugin event...

  • alexpott committed f0ac25fe on 11.x
    Issue #3438034 follow-up by alexpott: Fix Composer Scaffold plugin event...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.