如何在composer安装后运行Symfony控制台命令?

时间:2015-03-16 13:45:03

标签: php symfony composer-php

我的composer.json包含以下声明:

    "post-install-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
    ],

我想在src/MyBundle/Command/MyCommand.php中运行自定义控制台命令。如何将其添加到脚本中以在composer中运行?

1 个答案:

答案 0 :(得分:23)

您可以看到postinstall挂钩如何为Sensio DistributionBundle工作。

例如,这是您可以调用Acme Demo包的Hello World命令的方法:

<强> ScriptHandler

<?php

namespace Acme\DemoBundle\Composer;

use Composer\Script\CommandEvent;

class ScriptHandler extends \Sensio\Bundle\DistributionBundle\Composer\ScriptHandler {


    /**
     * Call the demo command of the Acme Demo Bundle.
     *
     * @param $event CommandEvent A instance
     */
    public static function helloWorld(CommandEvent $event)
    {
        $options = self::getOptions($event);
        $consoleDir = self::getConsoleDir($event, 'hello world');

        if (null === $consoleDir) {
            return;
        }

//        $extraParam = '';
//        if (!$options['who']) {
//            $extraParam = ' --who';
//        }

        static::executeCommand($event, $consoleDir, 'acme:hello', $options['process-timeout']);
    }

}

您可以在json文件中管理额外的参数。

<强> composer.json

"post-install-cmd": [
    "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
    "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles",
    "Acme\\DemoBundle\\Composer\\ScriptHandler::helloWorld"
],

<强>测试

我扩展了版本的sensio-distribution包的ScriptHandler类:

sensio/distribution-bundle (v3.0.18)

希望这个帮助