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

时间:2022-04-29 20:47:38

How can I write a sample console application with Zend?

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

/Zend/Console/Getopt.php

I just want to pass a value as -v and will get the version information.

我只想将值传递给-v并获取版本信息。

Input as

prjectfolder/console/version.php -v

Output:

Version 1.xxxxx

How can I code this in Zend with simple PHP with send lib includes methods.

如何使用简单的PHP在Zend中对此进行编码,其中send lib包含方法。

3 个解决方案

#1


This is a small example of how I am handling the CLI Interface for an Application. It is including my Bootstrap and the Zend Autoloader. A better solution is to change the Bootstrap for CLI Operations (no need for Dispatching and such stuff) but I am a lazy guy :-)

这是我如何处理应用程序的CLI接口的一个小例子。它包括我的Bootstrap和Zend Autoloader。更好的解决方案是更改用于CLI操作的Bootstrap(不需要调度等等),但我是一个懒惰的人:-)

<?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
}

#2


I suggest using Symfony Console Component instead http://dev.umpirsky.com/building-cli-apps-with-symfony-console-component/

我建议使用Symfony Console Component而不是http://dev.umpirsky.com/building-cli-apps-with-symfony-console-component/

#3


You can find all the details you need in ZF docs.

您可以在ZF文档中找到所需的所有详细信息。

#1


This is a small example of how I am handling the CLI Interface for an Application. It is including my Bootstrap and the Zend Autoloader. A better solution is to change the Bootstrap for CLI Operations (no need for Dispatching and such stuff) but I am a lazy guy :-)

这是我如何处理应用程序的CLI接口的一个小例子。它包括我的Bootstrap和Zend Autoloader。更好的解决方案是更改用于CLI操作的Bootstrap(不需要调度等等),但我是一个懒惰的人:-)

<?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
}

#2


I suggest using Symfony Console Component instead http://dev.umpirsky.com/building-cli-apps-with-symfony-console-component/

我建议使用Symfony Console Component而不是http://dev.umpirsky.com/building-cli-apps-with-symfony-console-component/

#3


You can find all the details you need in ZF docs.

您可以在ZF文档中找到所需的所有详细信息。