Horde_Cli::init PHP Method

init() public static method

CLI scripts shouldn't timeout, so try to set the time limit to none. Also initialize a few variables in $_SERVER that aren't present from the CLI.
public static init ( ) : Horde_Cli
return Horde_Cli A Horde_Cli instance.
    public static function init()
    {
        /* Run constructor now because it requires $_SERVER['SERVER_NAME'] to
         * be empty if called with a CGI SAPI. */
        $cli = new static();
        @set_time_limit(0);
        ob_implicit_flush(true);
        ini_set('html_errors', false);
        set_exception_handler(array($cli, 'fatal'));
        if (!isset($_SERVER['HTTP_HOST'])) {
            $_SERVER['HTTP_HOST'] = '127.0.0.1';
        }
        if (!isset($_SERVER['SERVER_NAME'])) {
            $_SERVER['SERVER_NAME'] = '127.0.0.1';
        }
        if (!isset($_SERVER['SERVER_PORT'])) {
            $_SERVER['SERVER_PORT'] = '';
        }
        if (!isset($_SERVER['REMOTE_ADDR'])) {
            $_SERVER['REMOTE_ADDR'] = '';
        }
        $_SERVER['PHP_SELF'] = isset($argv) ? $argv[0] : '';
        if (!defined('STDIN')) {
            define('STDIN', fopen('php://stdin', 'r'));
        }
        if (!defined('STDOUT')) {
            define('STDOUT', fopen('php://stdout', 'r'));
        }
        if (!defined('STDERR')) {
            define('STDERR', fopen('php://stderr', 'r'));
        }
        return $cli;
    }

Usage Example

Example #1
0
 /**
  * The main entry point for the application.
  *
  * @param array $parameters A list of named configuration parameters.
  * <pre>
  * 'parser'   - (array)     Parser configuration parameters.
  *   'class'  - (string)    The class name of the parser to use.
  * 'output'   - (Horde_Cli) The output handler.
  * </pre>
  */
 public static function main(array $parameters = array())
 {
     $modular = self::_prepareModular($parameters);
     if (empty($parameters['output'])) {
         if (!class_exists('Horde_Cli')) {
             throw new Horde_Kolab_Cli_Exception('The Horde_Cli package seems to be missing (Class Horde_Cli is missing)!');
         }
         $cli = Horde_Cli::init();
     } else {
         $cli = $parameters['output'];
     }
     $parser = $modular->createParser();
     list($options, $arguments) = $parser->parseArgs();
     if (count($arguments) == 0) {
         $parser->printHelp();
     } else {
         try {
             if (!empty($options['config'])) {
                 if (!file_exists($options['config'])) {
                     throw new Horde_Kolab_Cli_Exception(sprintf('The specified config file %s does not exist!', $options['config']));
                 }
                 global $conf;
                 include $options['config'];
                 foreach ($conf as $key => $value) {
                     $options->ensureValue($key, $value);
                 }
             }
             if (empty($options['host'])) {
                 $options['host'] = 'localhost';
             }
             if (empty($options['driver'])) {
                 $options['driver'] = 'horde';
             }
             $world = array();
             foreach ($modular->getModules() as $module) {
                 $modular->getProvider()->getModule($module)->handleArguments($options, $arguments, $world);
             }
             if (!empty($options['timed']) && class_exists('Horde_Support_Timer')) {
                 $timer = new Horde_Support_Timer();
                 $timer->push();
             } else {
                 $timer = false;
             }
             $modular->getProvider()->getModule(Horde_String::ucfirst($arguments[0]))->run($cli, $options, $arguments, $world);
             if (!empty($options['timed'])) {
                 if ($timer) {
                     $cli->message(floor($timer->pop() * 1000) . ' ms');
                 } else {
                     $cli->message('The class Horde_Support_Timer seems to be missing!');
                 }
             }
         } catch (Horde_Cli_Modular_Exception $e) {
             $parser->printHelp();
         }
     }
 }
All Usage Examples Of Horde_Cli::init