2009-07-22 132 views
1

如何使用Zend编写示例控制台应用程序?如何使用Zend编写示例控制台应用程序?

/Zend/Console/Getopt.php 

我只是想传递一个值作为-v并将获得版本信息。

输入作为

prjectfolder/console/version.php -v 

输出:

Version 1.xxxxx 

在Zend公司与简单的PHP与发送的lib包括方法如何我的代码这一点。

回答

9

这是我如何处理应用程序的CLI接口的一个小例子。它包括我的Bootstrap和Zend Autoloader。一个更好的解决办法是改变引导的CLI操作(不需要调度和这样的东西),但我是一个懒惰的家伙:-)

<?php 
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/')); 
define('APPLICATION_ENVIRONMENT', 'development'); 

/** 
* Setup for includes 
*/ 
set_include_path(
    APPLICATION_PATH . '/../library' . PATH_SEPARATOR . 
    APPLICATION_PATH . '/../application/models' . PATH_SEPARATOR . 
    APPLICATION_PATH . '/../application/extends'. PATH_SEPARATOR . 
    get_include_path()); 


/** 
* Zend Autoloader 
*/ 
require_once 'Zend/Loader/Autoloader.php'; 

$autoloader = Zend_Loader_Autoloader::getInstance(); 

/** 
* Register my Namespaces for the Autoloader 
*/ 
$autoloader->registerNamespace('My_'); 
$autoloader->registerNamespace('Db_'); 


/** 
* Include my complete Bootstrap 
* @todo change when time is left 
*/ 
require '../application/bootstrap.php'; 

/** 
* Setup the CLI Commands 
* ../application/cli.php --add 
* ../application/cli.php --scan 
* .. 
*/ 
try { 
    $opts = new Zend_Console_Getopt(
     array(
      'help'  => 'Displays usage information.', 
      'add'  => 'Add the Feeds to the Pipe', 
      'scan'  => 'Scan the Feeds in the Pipe', 
      'que'  => 'Process the Pipe', 
     ) 
    ); 

    $opts->parse(); 

} catch (Zend_Console_Getopt_Exception $e) { 
    exit($e->getMessage() ."\n\n". $e->getUsageMessage()); 
} 

if(isset($opts->help)) { 
    echo $opts->getUsageMessage(); 
    exit; 
} 

/** 
* Action : add 
*/ 
if(isset($opts->add)) { 
    // do something 
} 

/** 
* Action : scan 
*/ 
if(isset($opts->scan)) { 
    // do something 
} 

/** 
* Action : que 
*/ 
if(isset($opts->que)) { 
    // do something 
} 
-1

你可以在ZF docs找到所有你需要的细节。

+0

的是新Zend技术。所以不知道如何使用。安慰。 – coderex 2009-07-22 05:56:10

相关问题