CodeIgniter CLI (command line mode) 修改

未分類
有些時候可能會使用 命令列的方式去 執行一些 cron 的 工作
用 web 模式可能會 timeout, 把 execute time 設定成很長 也不是辦法

所以要把 CodeIgniter 改一下
以便在 CodeIgniter 的 架構下 跑 CLI


首先下載 MY_URI.php

如果連結壞了 可以抓底下的 code

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* MY_URI Class
*
* Parses URIs and determines routing
*
* @package CodeIgniter
* @subpackage Libraries
* @category URI
* @author ExpressionEngine Dev Team
* @modified Philip Sturgeon
* @link http://codeigniter.com/user_guide/libraries/uri.html
*/
class MY_URI extends CI_URI {

/**
* Get the URI String, with added support for command line
*
* @access private
* @return string
*/
function _fetch_uri_string()
{
if (strtoupper($this->config->item('uri_protocol')) == 'AUTO')
{
// If the URL has a question mark then it's simplest to just
// build the URI string from the zero index of the $_GET array.
// This avoids having to deal with $_SERVER variables, which
// can be unreliable in some environments
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
{
$this->uri_string = key($_GET);
return;
}

// Is there a PATH_INFO variable?
// Note: some servers seem to have trouble with getenv() so we'll test it two ways
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
$this->uri_string = $path;
return;
}

// No PATH_INFO?... What about QUERY_STRING?
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
if (trim($path, '/') != '')
{
$this->uri_string = $path;
return;
}

// No QUERY_STRING?... Maybe the ORIG_PATH_INFO variable exists?
$path = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO');
if (trim($path, '/') != '' && $path != "/".SELF)
{
// remove path and script information so we have good URI data
$this->uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $path);
return;
}

// Has arguments and no server name, must be command line
if(isset($_SERVER['argv']) && !isset($_SERVER['SERVER_NAME']))
{
$this->uri_string = $this->_parse_cli_args();
return;
}

// We've exhausted all our options...
$this->uri_string = '';
}
elseif(strtoupper($this->config->item('uri_protocol')) == 'CLI')
{
$this->uri_string = $this->_parse_cli_args();
}
else
{
$uri = strtoupper($this->config->item('uri_protocol'));

if ($uri == 'REQUEST_URI')
{
$this->uri_string = $this->_parse_request_uri();
return;
}

$this->uri_string = (isset($_SERVER[$uri])) ? $_SERVER[$uri] : @getenv($uri);
}

// If the URI contains only a slash we'll kill it
if ($this->uri_string == '/')
{
$this->uri_string = '';
}
}

// Convert arguments into a
function _parse_cli_args() {

// Get all arguments from command line
$args = $_SERVER['argv'];

// Remove the first, its the file name
unset($args[0]);

return '/'.implode('/', $args);
}

}
// END MY_URI Class

/* End of file URI.php */
/* Location: ./application/libraries/MY_URI.php */



然後把檔案貼到 application/libraries/ 裡面

然後 到 config.php 加入以下內容
$config['uri_protocol']    = "AUTO"; // Works for web and command line
$config['uri_protocol'] = "CLI"; // Command line only
$config['uri_protocol'] = isset($_SERVER['REQUEST_URI']) ? 'PATH_INFO' : 'CLI'; // working on web with a specific uri type and command line at the same time, change path info to any of the normal CI uri types.

就設定完成了

命令列的執行方法如下
php index.php controller method param1 etc