Symfony Console 集合到thinkphp
composer require symfony/console @stable
mkdir -p bin/cmd
- 注册命名空间,编辑 composer.json 文件如下,然后在命令行输入composer dump-autoload
{
"require": {
"symfony/console": "@stable"
},
"autoload": {
"psr-4":{
"cmd\\": "bin/cmd"
}
}
}
- 编写命令文件:
- 在 bin/cmd 路径下创建 DemoCmd.php 文件,并写入:
<?php
namespace cmd;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DemoCmd extends Command
{
public function __construct($msg)
{
$this -> msg = $msg;
parent::__construct();
}
protected function configure()
{
$this-> setName('demo');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output -> writeln("<comment>".$this->msg."</comment>");
}
}
- 在项目根目录下,创建Console组件的入口文件 console 并写入:
<?php
require __DIR__.'/vendor/autoload.php';
use cmd\DemoCmd;
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new DemoCmd("111"));
$application->run();