在作曲家安装后如何运行 Symfony 控制台命令?

How do I run a Symfony Console command after composer install?(在作曲家安装后如何运行 Symfony 控制台命令?)
本文介绍了在作曲家安装后如何运行 Symfony 控制台命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My composer.json contains the following declaration:

    "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 中运行?

I want to run a custom console command that I have in src/MyBundle/Command/MyCommand.php. How do I add this to the scripts to run in composer?

推荐答案

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

You can see how the postinstall hook work for the Sensio DistributionBundle.

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

As example, this is how you can call the Hello World command of the Acme Demo bundle:

ScriptHandler

<?php

namespace AcmeDemoBundleComposer;

use ComposerScriptCommandEvent;

class ScriptHandler extends SensioBundleDistributionBundleComposerScriptHandler {


    /**
     * 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 文件本身中管理额外的参数.

You can manage extra param in the json file itself.

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 bundle的ScriptHandler类:

I extend the ScriptHandler class of the sensio-distribution bundle of version:

sensio/distribution-bundle (v3.0.18)

希望有帮助

这篇关于在作曲家安装后如何运行 Symfony 控制台命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)