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

Class: Console_Application_Abstract

Source Location: /library/Console/Application/Abstract.php

Class Overview


Template method class for CLI commands.


Author(s):

Copyright:

  • Copyright (c) 2009 Sven Strittmatter

Implements interfaces:

Variables

Methods


Child classes:

Console_Application_Concrete
Template method class for CLI commands.
TestApplication
Template method class for CLI commands.
Console_Deamon_Abstract
Class which is running in a loop.
HelloWorldApplication
Template method class for CLI commands.

Class Details

[line 58]
Template method class for CLI commands.



Tags:

copyright:  Copyright (c) 2009 Sven Strittmatter
see:  HelloWorldApplication for an example implementation.
abstract:  
usedby:  HelloWorldApplication
usedby:  Console_Deamon_Abstract
uses:  Console_Exception
uses:  Console_Application_Interface


[ Top ]


Class Variables

$scriptname =

[line 67]

Name of the script. Appended with 'Application' its the Classname of your concrete implementation.



Tags:

access:  protected

Type:   string


[ Top ]



Class Methods


constructor __construct [line 104]

Console_Application_Abstract __construct( string $scriptname, [int $maxExecTime = 60], [string $memoryLimit = '128M'])

On construction time a sapiCheck is done for CLI sapi.

The php enviroment will be set and some default signal handlers are registered.

Throws a excpetion if the php enviroment ist not the command line.




Tags:

throws:  RuntimeException if sapi check fails
throws:  RuntimeException if registerig signal handlers fails
access:  public


Parameters:

string   $scriptname  
int   $maxExecTime   Optional;
string   $memoryLimit   Optional;

[ Top ]

method echoMsg [line 456]

void echoMsg( [string $msg = ''])

Echoes a string appended with PHP_EOL.

If you call it without parameter it makes newlines ;-)




Tags:

access:  public


Parameters:

string   $msg  

[ Top ]

method execute [line 204]

void execute( )

Template method. Try to run some method stubs in this order:

  1. Console_Application_Abstract::findConsoleParameters()
  2. Console_Application_Abstract::setUp()
  3. Console_Application_Abstract::run()
  4. Console_Application_Abstract::tearDown()

If Console_Application_Abstract::findConsoleParameters() returns true the script execution aborts imediately.

If in the templateed method structure any exception occures it is catched an the method Console_Application_Abstract::onError() is called. The exception object is passed to it.

After that procedure two additional tear down methods will be called:

  1. Console_Application_Abstract::setExecuted()
  2. Console_Application_Abstract::resetPhpEnviroment()




Tags:

final:  
access:  public



Implementation of:
Console_Application_Interface::execute()
Executes the application object
[ Top ]

method findConsoleParameters [line 284]

bool findConsoleParameters( )

This method listens by default to the console parameters -v and -h.

By default -v enables the verbose mode (see Console_Application_Abstract::isVerbose()).

By default -h calls Console_Application_Abstract::printHelpMessage() and returns with false.

To retrieve custom options overwite this mithed. Call the method an checks against false. Then check your options. If a required option is not set you should throw an Console_Exception_MissingParameter. This errors are handled in an default way to the console user (see Console_Exception_MissingParameter::onError()).

Example implementation. Full example @see HelloWorldApplication

  1.  protected function findConsoleParameters({
  2.   $options getopt('n:s:');
  3.  
  4.   if (!parent::findConsoleParameters()) {
  5.        return false;
  6.    }
  7.  
  8.    if (isset($options['n']&& !empty($options['n'])) {
  9.        $this->name $options['n'];
  10.    }
  11.  
  12.    if (isset($options['s']&& !empty($options['s'])) {
  13.        $this->sleep = (int) $options['s'];
  14.    }
  15.  
  16.    return true;
  17.  }

To brek the script if a user omitted a required console option you should implement it that way, because the onError() method handles Console_Exception_MissingParameter by default with nice user experience.

  1.  protected function findConsoleParameters({
  2.   $options getopt('n:');
  3.  
  4.   if (!parent::findConsoleParameters()) {
  5.        return false;
  6.    }
  7.  
  8.    if (isset($options['n']&& !empty($options['n'])) {
  9.        $this->name $options['n'];
  10.    else {
  11.       throw new Console_Exception_MissingParameter('n');
  12.    }
  13.  
  14.    return true;
  15.  }

For more details about getting options by getopt()




Tags:

return:  Must return false to abort script execution!
link:  http://de.php.net/manual/en/function.getopt.php
access:  protected


Overridden in child classes as:

Console_Application_Concrete::findConsoleParameters()
HelloWorldApplication::findConsoleParameters()

[ Top ]

method getHelpMessage [line 412]

string getHelpMessage( [string $author = ''], [string $email = 'foo@bar.de'])

Returns a help message. Its called by default object behavier, if you suply -h to the script. By default it returns a line break formatted help message like:

  This is a sample console application. It prints some hello world statements.

  Usage:  {$scriptName} [-h] [-v]
          {$scriptName} -v : Sets the script in verbose mode.
          {$scriptName} -h : prints this help message.

  For bug reporting or issues contact: foo@bar.de

You can customize the help message your wy. See an example implementation (@see HelloWorldApplication)

  1.  protected function getHelpMessage($author '...'$email '...'{
  2.       $message  "This is a sample console application. It prints some ";
  3.       $message .= "hello world statements." PHP_EOL PHP_EOL;
  4.       $message .= $this->getUsage(PHP_EOL;
  5.       $message .= "\t{$this->scriptname} -n : You can specify a name to ";
  6.       $message .= §be hellowed" . PHP_EOL;
  7.       $message .= "t{$this->scriptname-You can specify the seconds ";
  8.       $message .= §to sleep between hello and name."  PHP_EOL;
  9.       $message .= parent::getHelpMessage($author$email);
  10.  
  11.       throw new Console_Exception($message);
  12.  }




Tags:

access:  public


Overridden in child classes as:

HelloWorldApplication::getHelpMessage()

Parameters:

string   $author  
string   $email  

[ Top ]

method getUsage [line 443]

string getUsage( [string $options = ''])

Returns a usage string like:

 Usage: {$scriptName} {$options} [-h] [-v]

By example implementation (@see HelloWorldApplication) its:

 Usage:  helloworld.sh [-n] [-s] [-h] [-v]




Tags:

access:  public


Overridden in child classes as:

HelloWorldApplication::getUsage()

Parameters:

string   $options  

[ Top ]

method iniSet [line 172]

mixed iniSet( string $name, mixed $value)

This method wraps the php internal ini_set() that way, that it returns the old value.



Tags:

access:  protected


Parameters:

string   $name  
mixed   $value  

[ Top ]

method isExecuted [line 138]

bool isExecuted( )

Returns whether the application object is executed or not.



Tags:

access:  public



Implementation of:
Console_Application_Interface::isExecuted()
Returns whether the application object is executed or nit.
[ Top ]

method isPcntlEnabled [line 503]

void isPcntlEnabled( )



Tags:

access:  protected


Overridden in child classes as:

Console_Application_Concrete::isPcntlEnabled()

[ Top ]

method isVerbose [line 150]

bool isVerbose( )

Returns whether the application object is verbose or not.

This is set by the comandline option -v (in Console_Application_Abstract::findConsoleParameters()).




Tags:

access:  protected


[ Top ]

method onError [line 364]

void onError( mixed $errorData)

This method is called if in the three followed methods a exception was

throwed:

  • Console_Application_Abstract::findConsoleParameters()
  • Console_Application_Abstract::setUp()
  • Console_Application_Abstract::run()
  • Console_Application_Abstract::tearDown()

Exceptions of the (sub)type Console_Exception are handled by defaukt in a special way: <dl> <dt>Console_Exception_MissingParameter</dt> <dd>The exeption message and the return string of getUsage() will be echoed.</dd> <dt>Console_Exception</dt> <dd>The excpetion message will be echoed.</dd> </dl>




Tags:

todo:  php catchabel errors abfangen: error_handler setzen.
access:  protected


Overridden in child classes as:

Console_Application_Concrete::onError()

Parameters:

mixed   $errorData  

[ Top ]

method registerSignalHandlers [line 481]

void registerSignalHandlers( )

Registers some signal handlers:

<dl> <dd>SIGTERM</dd> <dt>Console_Application_Abstract::signalTerm()</dt> <dd>SIGINT</dd> <dt>Console_Application_Abstract::signalInt()</dt> <dd>SIGHUP</dd> <dt>Console_Application_Abstract::signalHangUp()</dt> </dl>

IS called on construction time. Overwrite this methid




Tags:

link:  http://de.php.net/manual/en/pcntl.setup.php
throws:  RuntimeException If pcntl_signal() doesnt exists
throws:  RuntimeException If pcntl_signal() failes.
access:  protected


[ Top ]

method run [line 324]

void run( )

You need to implement this method. Put your stuff to do here.

Return values are not evaluated.




Tags:

abstract:  
access:  protected


Overridden in child classes as:

Console_Application_Concrete::run()
TestApplication::run()
Console_Deamon_Abstract::run()
Template method from Console_Application_Abstract.
HelloWorldApplication::run()

[ Top ]

method setExecuted [line 159]

void setExecuted( )

Sets object executed.



Tags:

access:  protected


[ Top ]

method setPhpEnviroment [line 580]

void setPhpEnviroment( string $memoryLimit, int $maxExecutionTime)

Sets the the php enviroment by ini settings to a proper default.

These are:

  • memory_limit
  • max_execution_time
  • html_errors = false
  • implicit_flush = true

The old values are stored in Console_Application_Abstract::iniSet array. So they can be restored by Console_Application_Abstract::resetPhpEnviroment().

If you wanne make some custom ini settings in your script overwrite this method this way:




Tags:

access:  protected


Parameters:

string   $memoryLimit  
int   $maxExecutionTime  

[ Top ]

method setUp [line 311]

void setUp( )

Template method stub. Overwrite this method to hook the set up phase of your script. This method is called after Console_Application_Abstract::findConsoleParameters(), unless it returns false. And before Console_Application_Abstract::run().

By default this method does nothing!

Return values are not evaluated.




Tags:

access:  protected


Overridden in child classes as:

Console_Application_Concrete::setUp()

[ Top ]

method signalHangUp [line 544]

void signalHangUp( int $signal)

Signal handler registered by Console_Application_Abstract::registerSignalHandlers().

By default it echoes a message and calls Console_Application_Abstract::tearDown().

Overwrite this method to react on SIGHUP.




Tags:

access:  public


Overridden in child classes as:

Console_Application_Concrete::signalHangUp()

Parameters:

int   $signal  

[ Top ]

method signalInt [line 530]

void signalInt( int $signal)

Signal handler registered by Console_Application_Abstract::registerSignalHandlers().

By default it echoes a message and calls Console_Application_Abstract::tearDown().

Overwrite this method to react on SIGINT.




Tags:

access:  public


Overridden in child classes as:

Console_Application_Concrete::signalInt()
Console_Deamon_Abstract::signalInt()
Bind the signal SIG_INT to brek the loop.
HelloWorldApplication::signalInt()

Parameters:

int   $signal  

[ Top ]

method signalTerm [line 516]

void signalTerm( int $signal)

Signal handler registered by Console_Application_Abstract::registerSignalHandlers().

By default it echoes a message and calls Console_Application_Abstract::tearDown().

Overwrite this method to react on SIGTERM.




Tags:

access:  public


Overridden in child classes as:

Console_Application_Concrete::signalTerm()
Console_Deamon_Abstract::signalTerm()
Bind the signal SIG_TERM to brek the loop.

Parameters:

int   $signal  

[ Top ]

method tearDown [line 337]

void tearDown( )

Template method stub. Overwrite this method to hook the tear down phase of your script. This method is called after Console_Application_Abstract::run(), unless any exception was throwed.

And before Console_Application_Abstract::setExecuted().

By default this method does nothing!

Return values are not evaluated.




Tags:

access:  protected


Overridden in child classes as:

Console_Application_Concrete::tearDown()

[ Top ]


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