Example
[ class tree: Example ] [ index: Example ] [ all elements ]

Source for file HelloWorldApplication.php

Documentation is available at HelloWorldApplication.php

  1. <?php
  2. /**
  3.  * Console Library
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revisio$
  15.  *
  16.  * @category    Console
  17.  * @package     Example
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * @see Console_Application_Abstract
  23.  */
  24. require_once('Console/Application/Abstract.php');
  25. /**
  26.  * @see Console_Exception
  27.  */
  28. require_once('Console/Exception.php');
  29.  
  30. /*
  31.  * Neccessary for the signal handler
  32.  */
  33. declare(ticks 1);
  34.  
  35. /**
  36.  * Template method class for CLI commands.
  37.  *
  38.  * @see HelloWorldApplication for an example implementation.
  39.  *
  40.  * @abstract
  41.  * @category   Console
  42.  * @package    Example
  43.  * @uses       Console_Application_Abstract
  44.  * @uses       Console_Exception
  45.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  46.  */
  47.     /**
  48.      * @access protected
  49.      * @var string 
  50.      */
  51.     protected $name  = 'world';
  52.     /**
  53.      * @access protected
  54.      * @var int 
  55.      */
  56.     protected $sleep = 0;
  57.  
  58.     /**
  59.      * @see Console_Application_Abstract::run()
  60.      */
  61.     protected function run({
  62.         // if this constant is defined, then this script runs in an unit test.
  63.         // there we wont echo some messages to cli.
  64.         $quiet defined('TESTS_CONSOLE_APPLICATION_LOCATION');
  65.  
  66.         if (!$quiet{
  67.             echo "Hello";
  68.             sleep($this->sleep);
  69.             echo " {$this->name}!\n";
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * @see Console_Application_Abstract::signalInt()
  75.      */
  76.     public function signalInt($signal) {
  77.         throw new Console_Exception('bye bye');
  78.     }
  79.  
  80.     /**
  81.      * @see Console_Application_Abstract::findConsoleParameters()
  82.      */
  83.     protected function findConsoleParameters() {
  84.         $options = getopt('n:s:');
  85.  
  86.         if (!parent::findConsoleParameters()) {
  87.             return false;
  88.         }
  89.  
  90.         if (isset($options['n']) && !empty($options['n'])) {
  91.             $this->name = $options['n'];
  92.         }
  93.  
  94.         if (isset($options['s']) && !empty($options['s'])) {
  95.             $this->sleep = (int) $options['s'];
  96.         }
  97.  
  98.         return true;
  99.     }
  100.  
  101.     /**
  102.      * @see Console_Application_Abstract::getUsage()
  103.      */
  104.     public function getUsage($options = null) {
  105.         return parent::getUsage('[-n name] [-s seconds]');
  106.     }
  107.  
  108.     /**
  109.      * @see Console_Application_Abstract::getHelpMessage()
  110.      */
  111.     public function getHelpMessage($author = 'Sven Strittmatter', $email = 'ich@weltraumschaf.de') {
  112.         $message  = "This is a sample console application. It prints some hello world statements." . PHP_EOL . PHP_EOL;
  113.         $message .= $this->getUsage() . PHP_EOL;
  114.         $message .= "\t{$this->scriptname} -n : You can specify a name to be hellowedPHP_EOL;
  115.         $message .= "\t{$this->scriptname} -s : You can specify the seconds to sleep between hello and name."  PHP_EOL;
  116.         $message .= parent::getHelpMessage($author, $email);
  117.  
  118.         throw new Console_Exception($message);
  119.     }

Documentation generated on Mon, 17 Aug 2009 15:52:39 +0200 by phpDocumentor 1.4.2