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

Php 如何在composer安装后运行Symfony控制台命令?,php,symfony,composer-php,Php,Symfony,Composer Php,Mycomposer.json包含以下声明: "post-install-cmd": [ "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle

My
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中运行的脚本中?

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

例如,以下是如何调用Acme演示包的
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']);
    }

}
测试

我扩展了sensio发行包版本的
ScriptHandler
类:

sensio/distribution-bundle (v3.0.18)

希望这些帮助

由于某些原因,我无法调用“self::getConsoleDir”,因为它未定义。即使我在使用Sensio\Bundle\DistributionBundle\Composer\ScriptHandler包含ScriptHandler之后扩展了ScriptHandler;您好@juuga您使用的是哪个版本的
sensio/distribution bundle
?我用这些信息更新我的答案,检查
composer.json
并查看您的版本(我有“sensio/distribution bundle”:“~3.0”)。顺便说一句,您可以查看您的版本的ScriptHandler类的源代码,并了解如何执行clearCache命令(我从中复制)。希望这有帮助。你是对的,我有一个早期版本。通过检查文件的正确实现,我现在可以正常工作了。谢谢嗨@juuga你真是太棒了!Sensio捆绑包中的Strage行为而不是兼容性。。。我将查看捆绑包的文档,以了解有关..的更多详细信息。。当然,帮助器类不是接口。。
sensio/distribution-bundle (v3.0.18)