Symfony的Console组件的简单使用。

Symfony的Console组件的简单使用。

本文目的是多Symfony的Console组件进行简单的使用。
达到这样的效果:

  • 输入

    php console test
  • 输出

    hello console.

准备工作

我们这里使用composer来进行Console组件的安装 composer安装教程

  1. 进入项目路径:

    cd ~/web/project/
  2. 安装Console组件:

    composer require symfony/console @stable
  3. 创建自己的代码目录:

    mkdir -p src/Mycmd # 创建自己的代码目录
  4. 注册命名空间:
    编辑 composer.json 文件如下,然后在命令行输入composer dump-autoload

    {
       "require": {
           "symfony/console": "@stable"
       },
       "autoload": {
           "psr-4":{
               "Mycmd\\": "src/Mycmd"
           }
       }
    }

编写命令文件

  1. 创建要执行的命令文件:
    src/Mycmd 路径下创建 TestCmd.php 文件,并写入:

    <?php
    
    namespace Mycmd;
    
    use Symfony\Component\Console\Command\Command;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Output\OutputInterface;
    
    /**
     * 
     */
    class TestCmd extends Command
    {
        public function __construct($msg)
        {
            $this->msg = $msg;
            parent::__construct();
        }
    
        protected function configure()
        {
            $this->setName('test');
        }
    
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $output->writeln("<comment>".$this->msg."</comment>");
        }
    }
    
  2. 在项目根目录下,创建Console组件的入口文件 console 并写入:

    #!/usr/bin/env php
    <?php
    
    require __DIR__.'/vendor/autoload.php';
    
    use Mycmd\TestCmd;
    use Symfony\Component\Console\Application;
    
    $application = new Application();
    $application->add(new TestCmd("hello console"));
    $application->run();

使用Console组件

在命令行中输入:

php console test

这时会看见

hello console

代码分析

首先说说我们自己的命令文件: TestCmd.php

<?php

namespace Mycmd;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * 
 */
class TestCmd extends Command
{
    public function __construct($msg)
    {
        $this->msg = $msg;
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('test');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln("<comment>".$this->msg."</comment>");
    }
}

代码中包含一个类 TestCmd 并继承了 Command 基类。

configure 方法中设置了命令的名称 test,即 php console test 命令中的最后一个单词

execute 方法中定义了该命令的执行过程,即输出 $this->msg

再看看入口文件 console

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

require __DIR__.'/vendor/autoload.php';

use Mycmd\TestCmd;
use Symfony\Component\Console\Application;

$application = new Application();
$application->add(new TestCmd("hello console"));
$application->run();

这里的 $application->add() 方法将我们定义的 TestCmd 添加到了命令行中。

Symfony官方文档:
http://symfony.com/doc/current/components/console/introduction.html

日期 2016-5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值