4
votes

I'm doing a short post package install/update script to copy some files from the vendor directory into my public one.

Following the example of the composer site however when I execute it I get an error:

Fatal error: Call to undefined method Composer\DependencyResolver\Operation\UpdateOperation::getPackage() in S:\Projects\composer-scripts\FileCopy.php on line 17

The code is:

namespace composer-scipts;

use Composer\Script\Event;

class FileCopy
{
    public static function postPackageInstall( Event $event )
    {
        $packageName = $event->getOperation()->getPackage()->getName();

        echo "$packageName\n";
    }

    public static function postPackageUpdate( Event $event )
    {
        $packageName = $event->getOperation()->getPackage()->getName();

        echo "$packageName\n";
    }
}

Can anyone please advise?

1
@Ant not sure what you mean? It is a script as per getcomposer.org/doc/articles/scripts.md - Adam M.
Look in the Defining scripts# section to see what I mean. - Ant
Ok, the snippet from the composer.json is: "psr-4": { "composer-scripts": "vendor/wip/composer-scripts" }, "scripts": { "post-package-install": [ "FileCopy::postPackageInstall" ], "post-package-update": [ "FileCopy::postPackageUpdate" ] } - Adam M.
Ok, changing to getInitialPackage() seems to have done the trick, thanks for the help! - Adam M.

1 Answers

8
votes

Following further testing I have identified the issue, which is essentially due to two different interfaces having the same/a similar method but with different signatures. Thusly I have ended up with:

public static function postPackageInstall( Event $event )
{
    $packageName = $event->getOperation()->getPackage()->getName();

    if( $packageName == 'twbs/bootstrap' )
    {
        self::copyFiles();
    }
}

public static function postPackageUpdate( Event $event )
{
    $packageName = $event->getOperation()->getInitialPackage()->getName();

    if( $packageName == 'twbs/bootstrap' )
    {
        self::copyFiles();
    }
}

So, postPackageInstall uses getPackage() where-as postPackageUpdate uses getInitialPackage().