如何修复作曲家post-install-cmd脚本的错误?

时间:2013-10-02 09:08:53

标签: php composer-php autoloader

我想使用composer脚本进行一些安装后的操作,例如将文件从bootstrap vendor文件夹复制到我的Web应用程序公用文件夹。我有PHP世界和Web应用程序开发的宝贝经验。

我正按照this tutorial

尝试学习这样做

这是我的目录结构* enter image description here

这是我的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"
        ]
    }
}

这是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
        }
    }

执行 composer install 后出现此错误 enter image description here

install.sh此时为空

如何修复此错误,尤其是什么是自动加载?,我不知道要搜索哪些关键字请建议我阅读。

4 个答案:

答案 0 :(得分:5)

以防有人再次遇到这个问题。这是勺子饲料样品。 :)

在这样的特定场景中

enter image description here

我们必须将我们的composer.json设置为ff。设置

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

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

然后Installer.php的内容是

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

然后执行php composer.phar update将毫无征兆地工作。

答案 1 :(得分:1)

post-package-install值是相对于composer.json文件的位置。不要在这里使用绝对位置。

此外,Composer的安装脚本需要PHP,而不是sh!

答案 2 :(得分:1)

Kongthap,我遇到了同样的问题,这就是我修复它的方法:

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

Dphp是我的lib的根名称空间。

希望它有所帮助。

答案 3 :(得分:0)

有时问题可能是全局包已过时,您可以在运行composer global update之前运行命令composer install来修复它