美文网首页
thinkphp5下command

thinkphp5下command

作者: Mracale | 来源:发表于2021-07-23 10:21 被阅读0次
    php think查看可用命令
    image
    [root@root root] # php think clear 
    Clear Successed
    [root@root root] # php think clear config
    Clear Successed
    [root@root root] # php think clear view
    Clear Successed
    [root@root root] # php think clear cache
    Clear Successed
    [root@root root] # php think clear db
    Clear Successed
    [root@root root] # php think clear log
    Clear Successed
    [root@root root] # php think command
    
    
    applicationcommand.php文件返回数组里新增一条:
    
    return [ 
        'app\admin\command\Test', #测试
    ]; 
    
    
    application\admin\command下test文件:
    <?php
    namespace app\admin\command;
    
    use think\console\Command;
    use think\Console;
    use think\console\Output;
    use think\console\Input;
    use think\console\Input\Argument;
    use think\console\Input\Option;
    /*
     * 带参数脚本兼容linux定时和手动执行
    */
    class Test extends Command
    {
    
        //定义任务名和描述
        protected function configure(){
            //设置参数
            $this->addArgument('id',Argument::REQUIRED,'ID必填'); //必传参数
            $this->addArgument('name', Argument::OPTIONAL,'NAME选填');//可选参数
            //选项定义
            $this->addOption('type', 't', Option::VALUE_REQUIRED,'TYPE必填',0); //选项值必填
            $this->addOption('status', 's', Option::VALUE_OPTIONAL,'STATUS选填',1); //选项值选填
    
            $this->setName('test')->setDescription("php think test 测试"); //选项定义 
        }
    
        //调用该类时,会自动运行execute方法
        protected function execute(Input $input, Output $output){
            set_time_limit(0);
            @ob_end_clean();
            @ob_implicit_flush(1);
            ini_set('memory_limit','100M');
            # echo substr_replace('18858281234','****',3,4);
            header("Content-type: text/html; charset=utf-8");
            date_default_timezone_set('Asia/Shanghai');
    
                $excel_id = Cache::get('excel_id');
                if(!empty($excel_id)){ //如果缓存有值,用定时脚本执行
                      $id = $excel_id;
                 }else{//如果使用命令行手动传参执行
                    $args = $input->getArguments();//获取传参数组
                    print_r($args);                  
                    $options = $input->getOptions(); //获取选项数组
                    print_r($options);
                    $output->writeln("参数接收为:".$args['id'].'--'.$args['name']);
                    //接收用全称name,不能简写shortcut
                    $output->writeln("选项接收为:".$options['type'].'--'.$options['status']);
                }
    
            $page = 0;  
           while(true){
               $page++;
               $res = $model->where(['id'=>$id])->paginate(100, false, [ 'page' => $page, 'query' => $query ]); //获取订单数据
               $res = json_decode($res,true);
               if( empty($res['data']['body'])){//分页获取数据为空,则说明执行完毕
                   exit("已同步完成\n\r");//退出
               }
    
              //数据不为空,循环遍历插入本地数据库
               foreach($res['data']['body'] as $k=>$v){
                   $data['product_id'] = $v['product_id'];
                   $data['order_status'] = $status;
                   Db::table(Config('database.prefix').'table')->insert($data);
    
               }
               echo "同步第{$page}页完成\n\r"; //输出
           } 
        }
     }
    
    
    不带参数调用
    php think command
    
    
    参数Arguments调用(只传值,用空格隔开;必传在前,选填在后)
    php think command  args1 argsN...
    
    
    选项Options调用(简称传参,全称接收;全称name和value中间有空格,简称空格可有可无)
    php think  command   --name1  "value1"  --nameN  "valueN"
    php think  command  -n1  "value1"  -nN  "valueN" 
    
    
    参数Arguments和选项Options混合调用(参数和选项传递顺序不分)
    php think  command   args1 argsN...  --name1  "value1"  --nameN  "valueN"  
    php think  command   --name1  "value1"  --nameN  "valueN"  args1 argsN...
    
    php think  command  args1 argsN...  -n1  "value1"  -nN  "valueN"  args1 argsN...
    php think  command  -n1  "value1"  -nN  "valueN"  args1 argsN...
    
    
    arguments和options混合调用demo:
    #四种调用结果相同 
    [root@root root]# php think test 1 admin --type "2" --status "3"
    [root@root root]# php think test --type "2" --status "3"  1  admin 
    [root@root root]# php think test 1 admin -t"2" -s"3"
    [root@root root]# php think test -t"2" -s"3" 1 admin
    Array
    (
        [command] => test
        [id] => 1
        [name] => admin
    )
    Array
    (
        [type] => 2
        [status] => 3
        [help] => 
        [version] => 
        [quiet] => 
        [verbose] => 
        [ansi] => 
        [no-ansi] => 
        [no-interaction] => 
    )
    参数接收为:1--admin
    选项接收为:2--3
    [root@root root]# 
    
    
    代码调用
     use think\Console;#引入Console
    
     $output = Console::call('command');#无参数调用
     $output = Console::call('command',[args1,args2,argsN...]);#带参数调用命令 
     $output = Console::call('test',['1','admin','-t2','-s3']);#带参数调用(options和arguments不区分顺序)
    return $output->fetch();    #获取输出信息 
    
    

    备注

    如果提示:Could not open input file: think
    说明缺少 think 文件
    

    相关文章

      网友评论

          本文标题:thinkphp5下command

          本文链接:https://www.haomeiwen.com/subject/quvumltx.html