Adam M.
Adam M.

Reputation: 773

How to get package name in script?

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?

Upvotes: 4

Views: 1698

Answers (1)

Adam M.
Adam M.

Reputation: 773

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().

Upvotes: 8

Related Questions