当我调用php console.php say:hello MyName
时,下面的独立命令应用程序本身运行良好
问题在底部。
/var/www/html/local/test-app/composer.json
{
"name": "my/test-package",
"description": "......",
"type": "library",
"version": "1.0.0",
"autoload": {
"psr-0": {"": ""}
},
"require": {
"symfony/console": "2.*"
},
"minimum-stability": "stable"
}
/var/www/html/local/test-app/Command/HelloCommand.php
<?php
namespace Command;
use Symfony'Component'Console'Command'Command;
use Symfony'Component'Console'Input'InputArgument;
use Symfony'Component'Console'Input'InputInterface;
use Symfony'Component'Console'Output'OutputInterface;
class HelloCommand extends Command
{
protected function configure()
{
$this
->setName('say:hello')
->setDescription('Say hello to someone')
->addArgument(
'name',
InputArgument::OPTIONAL,
'Who do you want to say hello?'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$hello = $name ? 'Hello '.$name : 'Hello';
$output->writeln($hello);
}
}
/var/www/html/local/test-app/console.php
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
use Command'HelloCommand;
use Symfony'Component'Console'Application;
$application = new Application();
$application->add(new HelloCommand());
$application->run();
问题
当我在主项目的composer.json中包含独立包时,composer会安装所有内容,但当我调用php console.php say:hello MyName
时,我会收到Could not open input file: console.php
错误。显然,我必须在bin
目录中创建一个符号链接,比如behat、phing、phpspec、条令等,但如何创建呢?
我的主要项目的composer json
"require": {
"my/test-package": "dev-master"
},
我检查过:
- 控制台组件
- 如何在项目中逐步使用单个组件
- Symfony2组件作为独立软件包
- https://github.com/exu/symfony2-console-standalone
- 使用Symfony控制台的命令行PHP
等等…
Composer将在供应商目录中安装依赖项。您可以使用console.php文件的完整路径:
php vendor/my/test-package/console.php say:hello MyName
看到完整的路径而不仅仅是vendor/bin/可能会让人感到惊讶,但这是一种有效的方法,正如Wouter J针对PHPUnit所示。它有一个优点:它将调用正确的命令,而不是下次(例如,另一个包已经创建了vendor/bin/console.php文件(。
如果您想使用composer为二进制文件定位特定目录,请使用
{
....
"config": {
"bin-dir": "bin"
},
....
}
这样,您就可以在项目的根目录中为其中的所有二进制文件创建一个bin
目录(因为它会自动为您创建动态链接(。
不要忘记将此目录(bin
(添加到.gitignore文件中。
更多信息:https://getcomposer.org/doc/articles/vendor-binaries.md#can-供应商二进制文件安装在供应商bin之外的其他地方-
已解决。我举了一个完整的例子,这样其他人都可以从中受益
单机SYMFONY2控制台命令应用程序
/var/www/html/local/test-app/composer.json
在下面创建composer.json文件后,运行composer install
。
{
"name": "my/test-package",
"description": "Write something here",
"type": "library",
"version": "1.0.0",
"autoload": {
"psr-0": {
"Command": "src/"
}
},
"require": {
"symfony/console": "2.*"
},
"minimum-stability": "stable",
"bin": [
"bin/console"
]
}
/var/www/html/local/test-app/bin/console
请确保使其可执行,以便首先运行:chmod +x bin/console
。
#!/usr/bin/env php
<?php
set_time_limit(0);
(@include_once __DIR__ . '/../vendor/autoload.php') || @include_once __DIR__ . '/../../../autoload.php';
use Command'HelloCommand;
use Symfony'Component'Console'Application;
$app = new Application('My CLI Application', '1.0.0');
$app->add(new HelloCommand());
$app->run();
/var/www/html/local/test-app/src/Command/HelloCommand.php
<?php
namespace Command;
use Symfony'Component'Console'Command'Command;
use Symfony'Component'Console'Input'InputArgument;
use Symfony'Component'Console'Input'InputInterface;
use Symfony'Component'Console'Output'OutputInterface;
class HelloCommand extends Command
{
protected function configure()
{
$this
->setName('say:hello')
->setDescription('Say hello to someone')
->addArgument(
'name',
InputArgument::OPTIONAL,
'Who do you want to say hello?'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$hello = $name ? 'Hello '.$name : 'Hello';
$output->writeln($hello);
}
}
测试
test-app$ bin/console say:hello MyName
Hello MyName
独立控制台命令应用程序现在已经准备好了,但您也希望在主项目中使用它,因此,请执行以下操作。
您的主要项目
在composer.json文件中包含新包之后,运行composer update my/test-package
。
/var/www/html/local/my-main-app/composer.json
{
"name": "my/main-app",
"type": "project",
"description": "Main",
"autoload": {
"psr-0": { "": "src/" }
},
"repositories": [
{
"type": "git",
"url": "git@github.com:my/test-package.git"
}
],
"require": {
.......
.......
"my/test-package": "dev-master"
},
"minimum-stability": "stable"
}
若您查看bin
目录,应该会看到名为console
的文件,这意味着您可以在主应用程序中使用该命令。
测试
my-main-app$ bin/console say:hello MyName
Hello MyName