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

Source for file Abstract.php

Documentation is available at Abstract.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.  * $Revision$
  15.  * 
  16.  * @category    Console
  17.  * @package     Console_Application
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * Console_Application_Abstract.
  23.  *
  24.  * @see Console_Application_Abstract
  25.  */
  26. require_once('Console/Application/Abstract.php');
  27. /**
  28.  * Console_Exception.
  29.  *
  30.  * @see Console_Exception
  31.  */
  32. require_once('Console/Exception.php');
  33.  
  34. /**
  35.  * Class which is running in a loop.
  36.  *
  37.  * @abstract
  38.  * @category   Console
  39.  * @package    Console_Deamon
  40.  * @uses       Console_Application_Abstract
  41.  * @uses       Console_Exception
  42.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  43.  */
  44.     /**
  45.      * Flag to signal to break the loop.
  46.      *
  47.      * @access private
  48.      * @var bool 
  49.      */
  50.     private $breakLoop;
  51.     /**
  52.      * Counts the executed loops.
  53.      * 
  54.      * @access private
  55.      * @var int 
  56.      */
  57.     private $counter 0;
  58.  
  59.     /**
  60.      * Template method from Console_Application_Abstract.
  61.      * This method runs the method doLoopCycle() which you must
  62.      * implement in your deamon class implementation.
  63.      *
  64.      * @see Console_Application_Abstract
  65.      *
  66.      * @final
  67.      * @access protected
  68.      */
  69.     final protected function run({
  70.         $this->onPreLoop();
  71.  
  72.         while (true{
  73.             if ($this->breakLoop{
  74.                 $this->onBreakLoop();
  75.                 break;
  76.             }
  77.  
  78.             $this->doLoopCycle();
  79.             $this->counter++;
  80.         }
  81.  
  82.         $this->onPostLoop();
  83.     }
  84.  
  85.     /**
  86.      * Overwrite this method to hook the pre loop phase.
  87.      * This method is called first before the loop. But after
  88.      * Console_Application_Abstract::setUp().
  89.      *
  90.      * @access protected
  91.      */
  92.     protected function onPreLoop({}
  93.     /**
  94.      * Overwrite this method to hook the moment the internal breakLoop flag
  95.      * is set true by the script.
  96.      *
  97.      * @access protected
  98.      */
  99.     protected function onBreakLoop({}
  100.     /**
  101.      * Overwrite this method to hook the phase after the loop. This Method
  102.      * is called imediately after self::onBreakLoop(). But before
  103.      * Console_Application_Abstract::tearDown().
  104.      *
  105.      * @access protected
  106.      */
  107.     protected function onPostLoop({}
  108.     /**
  109.      * You need to implement this method with your loop code.
  110.      * This method is called periodicly until self::breakLoop()
  111.      * is called.
  112.      *
  113.      * Important is, that after self::breakLoop() this method runs
  114.      * until it returns. If you want to break out from your loop
  115.      * method directly after setting the demaon to break the loop,
  116.      * you need to return:
  117.      *
  118.      * <code>
  119.      * protected function onPostLoop() {
  120.      *      // some work ...
  121.      *
  122.      *      if ($something) {
  123.      *          // We break out imediately.
  124.      *          // some other work will not be executed
  125.      *          self::breakLoop();
  126.      *          return;
  127.      *      }
  128.      *
  129.      *      if ($somethingElse) {
  130.      *          // We only break the loop.
  131.      *          // This method will not be executed anymore.
  132.      *          // But some other work will be executed the last time!
  133.      *          self::breakLoop();
  134.      *      }
  135.      *
  136.      *      // some other work ...
  137.      * }
  138.      * </code>
  139.      */
  140.     protected abstract function doLoopCycle();
  141.  
  142.     /**
  143.      * Signals the object to do no more loop cycles.
  144.      * After the actual call to self::doLoopCycle() has finished
  145.      * self::onBreakLoop() will be called.
  146.      */
  147.     protected function breakLoop({
  148.         $this->breakLoop true;
  149.     }
  150.  
  151.     /**
  152.      * Bind the signal SIG_TERM to brek the loop.
  153.      *
  154.      * @access public
  155.      * @param int $signal 
  156.      */
  157.     public function signalTerm($signal{
  158.         $this->breakLoop();
  159.     }
  160.  
  161.     /**
  162.      * Bind the signal SIG_INT to brek the loop.
  163.      *
  164.      * @access public
  165.      * @param int $signal 
  166.      */
  167.     public function signalInt($signal{
  168.         $this->breakLoop();
  169.     }
  170. }

Documentation generated on Mon, 17 Aug 2009 15:51:54 +0200 by phpDocumentor 1.4.2