zoukankan      html  css  js  c++  java
  • phalcon的CLI应用

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

    最小的目录结构:
    app/config/config.php
    app/tasks/MainTask.php
    app/cli.php <– main bootstrap file


    创建bootstrap

    use PhalconDIFactoryDefaultCLI as CliDI;
    use PhalconCLIConsole 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 PhalconLoader();
    $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 (PhalconException $e) {
      echo $e->getMessage();
      exit(255);
    }

    命令行下启用用例:
    $ php app/cli.php

    任务
    cli应用至少要有一个默认的任务mainTask和一个默认的行为:mainAction.
    class mainTask extends PhalconCLITask  {
      public function mainAction() {
        echo " This is the default task and the default action ";
      }
      /**
      * @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 PhalconCLITask {
      public function mainAction() {
        echo " This is the default task and the default action ";
        $this->console->handle(array(
          'task' => 'main',
          'action' => 'test'
        ));
      }
      public function testAction() {
        echo ' I will get printed too! ';
      }
    }

  • 相关阅读:
    [置顶] 【玩转cocos2d-x之二十】从CCObject看cocos2d-x的内存管理机制
    android 随手记 读写文件的几种方式
    (队列的应用5.3.2)POJ 2259 Team Queue(队列数组的使用)
    iPhone调用ffmpeg2.0.2解码h264视频的示例代码
    android 随手记 仿微信的popwindow
    [LeetCode] Remove Nth Node From End of List
    [置顶] Zend Optimizer 和 Zend Debugger 同时安装
    uva 10721
    android实现六边形等不规则布局
    WPF中的TextBox隐藏边框
  • 原文地址:https://www.cnblogs.com/xiaoleiel/p/8295254.html
Copyright © 2011-2022 走看看