我的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中运行?
答案 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)
希望这个帮助