在 Symfony 控制台中执行 "composer install"
Doing a "composer install" inside a Symfony console
在 jQuery 成功后,我必须在工作目录中启动 composer 安装(我正在 Silex 下开发 git 面板)。
有人告诉我,使用 Symfony 控制台可以很好地完成它,因为它可以保留一些选项。
但是我真的不知道怎么称呼它。
我创建了一个扩展 Command 的 class,我想我必须在 execute 方法下实现它...
class ComposerCommand extends Command
{
protected function configure()
{
$this
->setName('composer:install')
->setDescription('Composer install')
->addArgument()
->addOption()
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
}
}
我试过这个:
<?php
namespace App\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class TestCommand extends Command {
protected function configure() {
$this->setName("test")
->setDescription("Sample description for our command named test")
->setDefinition(array(
new InputOption('flag', 'f', InputOption::VALUE_NONE, 'Raise a flag'),
new InputArgument('activities', InputArgument::IS_ARRAY, 'Space-separated activities to perform', null),
))
->setHelp(<<<EOT
The <info>test</info> command does things and stuff
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
try {
\Phar::mapPhar('composer.phar');
include 'phar://composer.phar/demarrage.php';
} catch (\Exception $e) {
throw new \Exception($e);
}
}
}
还是不行。
好的,我发现了一些有趣的东西。
namespace App\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class ComposerCommand extends Command {
/**
* Configures the console Application
*
*/
protected function configure() {
$this->setName("composer:install")
->setDescription("Composer install inside a symfony console.")
->addOption('path', 'p', InputOption::VALUE_REQUIRED)
->setHelp(<<<EOT
The <info>composer:install</info> command makes "composer install"
EOT
);
}
/**
* Executes the console Application
*
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) {
try {
$path = $input->getOption('path');
$process = new Process('php composer.phar install --no-interaction');
$process->setWorkingDirectory($path);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$output->writeln($process->getOutput());
$output->writeln($process->getErrorOutput());
} catch (\Exception $e) {
throw new \Exception($e);
}
}
}
在 jQuery 成功后,我必须在工作目录中启动 composer 安装(我正在 Silex 下开发 git 面板)。 有人告诉我,使用 Symfony 控制台可以很好地完成它,因为它可以保留一些选项。 但是我真的不知道怎么称呼它。
我创建了一个扩展 Command 的 class,我想我必须在 execute 方法下实现它...
class ComposerCommand extends Command
{
protected function configure()
{
$this
->setName('composer:install')
->setDescription('Composer install')
->addArgument()
->addOption()
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
}
}
我试过这个:
<?php
namespace App\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class TestCommand extends Command {
protected function configure() {
$this->setName("test")
->setDescription("Sample description for our command named test")
->setDefinition(array(
new InputOption('flag', 'f', InputOption::VALUE_NONE, 'Raise a flag'),
new InputArgument('activities', InputArgument::IS_ARRAY, 'Space-separated activities to perform', null),
))
->setHelp(<<<EOT
The <info>test</info> command does things and stuff
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
try {
\Phar::mapPhar('composer.phar');
include 'phar://composer.phar/demarrage.php';
} catch (\Exception $e) {
throw new \Exception($e);
}
}
}
还是不行。
好的,我发现了一些有趣的东西。
namespace App\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class ComposerCommand extends Command {
/**
* Configures the console Application
*
*/
protected function configure() {
$this->setName("composer:install")
->setDescription("Composer install inside a symfony console.")
->addOption('path', 'p', InputOption::VALUE_REQUIRED)
->setHelp(<<<EOT
The <info>composer:install</info> command makes "composer install"
EOT
);
}
/**
* Executes the console Application
*
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) {
try {
$path = $input->getOption('path');
$process = new Process('php composer.phar install --no-interaction');
$process->setWorkingDirectory($path);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$output->writeln($process->getOutput());
$output->writeln($process->getErrorOutput());
} catch (\Exception $e) {
throw new \Exception($e);
}
}
}