【问题标题】:composer install pass command line argument or parameter to bin/consolecomposer install 将命令行参数或参数传递给 bin/console
【发布时间】:2015-12-22 22:14:56
【问题描述】:

我正在尝试使我的 symfony 3.0 应用程序能够与多个内核一起工作。 真正的目标:一个项目中的多个应用程序

一般情况下一切正常。我编辑了 bin/console 它的内容完全如下。它完全可以通过php bin/console --app=api得到我需要的结果 但是当我执行composer install bin/console 时自然会抛出异常,它不知道--app 参数。我想制作类似composer install --app=api 的东西,并且期望的行为会将参数传递给bin/console 我检查了文档,几乎互联网的每个像素都找不到解决方案。

#!/usr/bin/env php
<?php

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Debug\Debug;

// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);

set_time_limit(0);

/**
 * @var Composer\Autoload\ClassLoader $loader
 */
$loader = require __DIR__.'/../apps/autoload.php';

$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$app = $input->getParameterOption(array('--app', '-a'));
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';

if ($debug) {
    Debug::enable();
}

switch ($app) {
    case 'api':
        $kernel = new ApiKernel($env, $debug);
        break;
    case 'frontend':
        $kernel = new FrontendKernel($env, $debug);
        break;
    default:
        throw new \InvalidArgumentException("[--app|-a=<app>]  app: api|frontend");
        break;
}

$application = new Application($kernel);
$application->getDefinition()->addOptions([
    new InputOption('--app', '-a', InputOption::VALUE_REQUIRED, 'The application to operate in.'),
]);
$application->run($input);

【问题讨论】:

    标签: php symfony composer-php


    【解决方案1】:

    您可以使用composer install --no-scripts来防止安装后自动运行app/console

    或者您可以从composer.json 中的scripts 部分中删除bin/console 命令,这可能更有意义。见https://github.com/symfony/symfony-standard/blob/master/composer.json#L33-L34

    或者您可以使用环境变量而不是参数。

    【讨论】:

    • 哇,我错过了无脚本标志。我可以将 bin/console 相关的脚本放到 capistrano 中。没什么大不了。谢谢@Ryan。
    • 我为作曲家放置了一个包装器,并命名为 multicomposer。我将$app = $input-&gt;getParameterOption(array('--app', '-a')); 添加到$app = $input-&gt;getParameterOption(array('--app', '-a'), getenv('SYMFONY_APP')); 和multicomposer sh gist.github.com/feyyazesat/f8a2dbd0b3553b0ef6b4的内容
    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2012-08-13
    • 2015-06-09
    • 2017-10-22
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多