Source for file Router.class.php

Documentation is available at Router.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Router
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Router Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Router
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Router Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Router");
  34.  
  35. class Gumbo_Router implements Gumbo_Interface_Router {
  36.     
  37.     /** @var string $_url url to jump to */
  38.     private $_url;
  39.     /** @var bool $_active if the Router is active */
  40.     private $_active = false;
  41.     
  42.     /** @var int $_delay time, in seconds, to delay the jump */
  43.     private $_delay = GUMBO_ROUTER_DELAY;
  44.     /** @var bool $_flush flushes the buffer before routing */
  45.     private $_flush = GUMBO_ROUTER_FLUSH;
  46.     
  47.     
  48.     
  49.     /**
  50.      * Constructor
  51.      * @postcondition $this->activate()
  52.      * @param string $url 
  53.      * @param int $delay time, in seconds, before routing
  54.      * @param bool $flush flushes the buffer before routing
  55.      */
  56.     public function __construct ($url=null$delay=null$flush=null{
  57.         if (!is_null ($url)) $this->setUrl ($url)}
  58.         if (!is_null ($delay)) $this->setDelay ($delay)}
  59.         if (!is_null ($flush)) $this->setFlush ($flush)}
  60.         
  61.         $this->activate ();
  62.     }
  63.     
  64.     
  65.     
  66.     /** ACTION METHODS **/
  67.     /**
  68.      * Jumps to the supplied URL
  69.      * @precondition isActive ()
  70.      * @throws Gumbo_Exception
  71.      */
  72.     public function go ({
  73.         try {
  74.             if (!$this->isActive ()) {
  75.                 throw new Gumbo_Exception ("Router Inactive");
  76.             }
  77.             
  78.             // flushes the buffer
  79.             if ($this->getFlush ()) {
  80.                 flush ();
  81.             }
  82.             
  83.             // pause for delay
  84.             if ($this->getDelay ()) {
  85.                 sleep ($this->getDelay ());
  86.             }
  87.             
  88.             // check if headers have been sent
  89.             if (!headers_sent ()) {
  90.                 header ("Location: " $this->getUrl ());
  91.             else {
  92.                 // write javascript to the browser
  93.                 $txt "<script language=\"Javascript\" type=\"text/javascript\">";
  94.                 $txt .= "window.location=\"" $this->getUrl ("\";";
  95.                 $txt .= "</script>";
  96.                 echo $txt;
  97.             }
  98.             exit ();
  99.         catch (Gumbo_Exception $e{
  100.             $e->setFunction (__METHOD__);
  101.             gumbo_trigger ($e);
  102.         }
  103.     }
  104.     
  105.     /**
  106.      * Activates the Router
  107.      * @precondition getUrl()
  108.      * @postcondition isActive()
  109.      */
  110.     public function activate ({
  111.         if (!$this->getUrl ()) return}
  112.         $this->_active = true;
  113.     }
  114.     
  115.     /**
  116.      * Deactivates the Router
  117.      * @postcondition !isActive()
  118.      */
  119.     public function deactivate ({
  120.         $this->_active = false;
  121.     }
  122.     
  123.     
  124.     
  125.     /** MUTATOR METHODS **/
  126.     /**
  127.      * Sets the URL to jump to
  128.      * @precondition Valid::isUrl ()
  129.      * @param string $url 
  130.      * @throws Gumbo_Exception
  131.      * @uses Gumbo_Valid_Web
  132.      */
  133.     public function setUrl ($url{
  134.         try {
  135.             // verify precondition
  136.             if (!is_string ($url)) {
  137.                 throw new Gumbo_Exception ("Invalid Argument 'url:str' => {$url}:gettype ($url));
  138.             }
  139.             
  140.             gumbo_load ("Valid_Web");
  141.             $valid new Gumbo_Valid_Web ();
  142.             if (!$valid->isUrl ($url)) {
  143.                 throw new Gumbo_Exception ("Invalid URL: {$url}");
  144.             }
  145.             
  146.             $this->_url = $url;
  147.         catch (Gumbo_Exception $e{
  148.             $e->setFunction (__METHOD__);
  149.             gumbo_trigger ($e);
  150.         }
  151.     }
  152.     
  153.     /**
  154.      * Sets the delay time, in seconds, before routing
  155.      * @precondition $delay >= 0
  156.      * @precondition $delay <= 30
  157.      * @param int $delay 
  158.      * @throws Gumbo_Exception
  159.      */
  160.     public function setDelay ($delay{
  161.         try {
  162.             // verify precondition
  163.             if (!is_int ($delay&& !is_numeric ($delay)) {
  164.                 throw new Gumbo_Exception ("Invalid Argument 'delay:int' => {$delay}:gettype ($delay));
  165.             }
  166.             if ($delay || $delay 30{
  167.                 throw new Gumbo_Exception ("Out Of Range '0 <= delay <= 30' => {$delay}");
  168.             }
  169.             
  170.             $this->_delay = (int) $delay;
  171.         catch (Gumbo_Exception $e{
  172.             $e->setFunction (__METHOD__);
  173.             gumbo_trigger ($e);
  174.         }
  175.     }
  176.     
  177.     /**
  178.      * Sets the program to flush the buffer before routing
  179.      * @param bool $val 
  180.      * @throws Gumbo_Exception
  181.      */
  182.     public function setFlush ($val{
  183.         try {
  184.             // verify precondition
  185.             if (!is_bool ($val)) {
  186.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  187.             }
  188.             
  189.             $this->_flush = $val;
  190.         catch (Gumbo_Exception $e{
  191.             $e->setFunction (__METHOD__);
  192.             gumbo_trigger ($e);
  193.         }
  194.     }
  195.     
  196.     
  197.     
  198.     /** ACCESSOR METHODS **/
  199.     /**
  200.      * Returns the Url
  201.      * @return string 
  202.      */
  203.     public function getUrl ({
  204.         return $this->_url;
  205.     }
  206.     
  207.     /**
  208.      * Returns the delay time, in seconds
  209.      * @return int 
  210.      */
  211.     public function getDelay ({
  212.         return $this->_delay;
  213.     }
  214.     
  215.     /**
  216.      * Returns if the buffer should be flushed
  217.      * @return bool 
  218.      */
  219.     public function getFlush ({
  220.         return $this->_flush;
  221.     }
  222.     
  223.     /**
  224.      * Returns if the Router is active
  225.      * @return bool 
  226.      */
  227.     public function isActive ({
  228.         return $this->_active;
  229.     }
  230.     
  231. }
  232.  
  233. ?>