如何在编译器安装后运行Symfony控制台命令?
问题描述:
我的 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"
],
我想运行一个自定义控制台命令, code> src / MyBundle / Command / MyCommand.php 。
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?
答
您可以看到后安装钩子对于Sensio DistributionBundle是如何工作的。
You can see how the postinstall hook work for the Sensio DistributionBundle.
例如,这是如何调用Acme演示包的 Hello World
命令:
As example, this is how you can call the Hello World
command of the Acme Demo bundle:
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 bundle版本的 ScriptHandler
p>
I extend the ScriptHandler
class of the sensio-distribution bundle of version:
sensio/distribution-bundle (v3.0.18)
希望这个帮助