博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
phalcon的CLI应用
阅读量:6856 次
发布时间:2019-06-26

本文共 2544 字,大约阅读时间需要 8 分钟。

CLI应用是命令行下执行的程序, 可以应用于定时任务,守护进程, 脚本, 公用命令等等.

最小的目录结构:

app/config/config.php
app/tasks/MainTask.php
app/cli.php <– main bootstrap file

创建bootstrap

use Phalcon\DI\FactoryDefault\CLI as CliDI;

use Phalcon\CLI\Console as ConsoleApp;
define('VERSION', '1.0.0');
//Using the CLI factory default services container
$di = new CliDI();
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__)));
/**
* Register the autoloader and tell it to register the tasks directory
*/
$loader = new \Phalcon\Loader();
$loader->registerDirs(
  array(
    APPLICATION_PATH . '/tasks'
  )
);
$loader->register();
// Load the configuration file (if any)
if(is_readable(APPLICATION_PATH . '/config/config.php')) {
  $config = include APPLICATION_PATH . '/config/config.php';
  $di->set('config', $config);
}
//Create a console application
$console = new ConsoleApp();
$console->setDI($di);
/**
* Process the console arguments
*/
$arguments = array();
$params = array();
foreach($argv as $k => $arg) {
  if($k == 1) {
    $arguments['task'] = $arg;
  } elseif($k == 2) {
    $arguments['action'] = $arg;
  } elseif($k >= 3) {
    $params[] = $arg;
  }
}
if(count($params) > 0) {
  $arguments['params'] = $params;
}
// define global constants for the current task and action
define('CURRENT_TASK', (isset($argv[1]) ? $argv[1] : null));
define('CURRENT_ACTION', (isset($argv[2]) ? $argv[2] : null));
try {
  // handle incoming arguments
  $console->handle($arguments);
}
catch (\Phalcon\Exception $e) {
  echo $e->getMessage();
  exit(255);
}

命令行下启用用例:

$ php app/cli.php

任务

cli应用至少要有一个默认的任务mainTask和一个默认的行为:mainAction.
class mainTask extends \Phalcon\CLI\Task  {
  public function mainAction() {
    echo "\nThis is the default task and the default action \n";
  }
  /**
  * @param array $params
  */
  public function testAction(array $params) {
    echo sprintf('hello %s', $params[0]) . PHP_EOL;
    echo sprintf('best regards, %s', $params[1]) . PHP_EOL;
  }
}
命令:
$ php app/cli.php main test world universe
hello world
best regards, universe

任务链:

要支持任务链首先定义DI:
$di->setShared('console', $console);
try {
  // handle incoming arguments
  $console->handle($arguments);
}
然后就可以使用控制台的任何任务了:
class MainTask extends \Phalcon\CLI\Task {
  public function mainAction() {
    echo "\nThis is the default task and the default action \n";
    $this->console->handle(array(
      'task' => 'main',
      'action' => 'test'
    ));
  }
  public function testAction() {
    echo '\nI will get printed too!\n';
  }
}

 

转载地址:http://dtyyl.baihongyu.com/

你可能感兴趣的文章
变频电源与变频器不同浅释
查看>>
利用HTML5将摄像头视频流转换成ascii码流,通过websocket实时传输给其它浏览器展示。...
查看>>
运维之道:16 张图片带你 1 小时学会 Ansible
查看>>
分享:IT管理员都喜欢用的Outlook超大附件系统
查看>>
objective-c设计模式之---单例
查看>>
golang读取json格式的天气预报
查看>>
Cisco ASA5500解决内网用公网IP不能访问DMZ区服务器的
查看>>
Windows7常用命令
查看>>
vue获取input输入框的数据
查看>>
Go标准库testing进行有序代码测试
查看>>
linux 常用软件安装整理
查看>>
每周总结20130829——Android异步任务
查看>>
编译原理--词法分析程序
查看>>
springMVC 中几种获取request和response的方式
查看>>
vector与ArrayList、hashmap与hashtable区别
查看>>
一个简单系统的设计之争
查看>>
如何配置Kettle集群运行环境
查看>>
Jmeter分布式测试
查看>>
Centos6.9系统部分基础优化(更新时间2018/04/19)
查看>>
一次浏览器http请求的过程
查看>>