12

I would like to use composer script to do some post installation such as copying files from bootstrap vendor folder to my web application public folder. I have a baby experience with PHP world and web application development.

I'm trying to learn doing this by following this tutorial

This is my directory structure* enter image description here

This is my composer.json

{
    "name": "Composer Script",
    "description": "An example to demonstrate the use of Composer scripts",
    "version": "1.0.0",
    "require": {
        "twitter/bootstrap": ">=3.0"
    },

    "scripts": {
        "post-install-cmd": [
            "ComposerScript\\Installer::postInstall"
        ],
        "post-package-install": [
            "/var/www/test/composer-script/install.sh"
        ]
    }
}

This is ComposerScript\Installer.php

class Installer

    {
        public static function postInstall(Event $event)
        {
            $composer = $event->getComposer();
            // do stuff
        }

        public static function postPackageInstall(Event $event)
        {
            $installedPackage = $event->getOperation()->getPackage();
            // do stuff
        }

        public static function warmCache(Event $event)
        {
            // make cache toasty
        }
    }

After execute composer install I got this error enter image description here

install.sh is empty at this moment

How to fix this error, and especially what is autoload?, I don't event know what keywords to search for please suggest me some reading.

robsch
  • 9,358
  • 9
  • 63
  • 104
Artisan
  • 4,042
  • 13
  • 37
  • 61

4 Answers4

7

Just in case someone stumbled again to this problem. Here is a spoon feed sample. :)

In a given scenario like this

enter image description here

We have to set on our composer.json to the ff. settings

"autoload": {
    "psr-0": {
        "ComposerScript\\Installer" : ""
    }
},

"scripts": {
    "post-package-update": [
        "ComposerScript\\Installer::postPackageUpdate"
    ]
}

Then the content of Installer.php is

namespace ComposerScript;

use Composer\Script\Event;

class Installer
{
    public static function postUpdate(Event $event)
    {
    $composer = $event->getComposer();
    // do stuff
    }

    public static function postPackageUpdate(Event $event)
    {
    $packageName = $event->getOperation()
        ->getPackage()
        ->getName();
    echo "$packageName\n";
    // do stuff
    }

    public static function warmCache(Event $event)
    {
    // make cache toasty
    }
}

Then executing php composer.phar update will work without warning.

notmii
  • 413
  • 4
  • 9
  • 1
    Thanks, this worked. However, I would love to know how to modify this if the script I am calling is in a second folder within ComposerScript – myol May 01 '15 at 13:51
1

The post-package-install value is relative to the location of the composer.json file. Do not use absolute location here.

Also, Composer's install scripts expect PHP, not sh!

Juan Treminio
  • 2,176
  • 1
  • 18
  • 27
1

Kongthap, I ran into the same problem and this is how I fixed it:

"require": {...},
"autoload": {
    "psr-0": {
        "Dphp": "src/"
    }
},
"scripts": {...}

Dphp is the root namespace of my lib.

Hope it helps.

btnguyen
  • 51
  • 4
0

Sometimes the problem can be global packages are outdated, you can fix it by running the command composer global update before running composer install

Agu Dondo
  • 12,638
  • 7
  • 57
  • 68