Source for file Server.class.php

Documentation is available at Server.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 Debug
  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.  * Debug Server Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Debug
  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 Debug Server Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Singleton");
  34. gumbo_load ("Interface_Output");
  35. gumbo_load ("Factory");
  36. gumbo_load ("Debug_Server_Blank");
  37.  
  38.     
  39.     /** @var Gumbo_Interface_Debug_Server $_instance */
  40.     private static $_instance null;
  41.     
  42.     /** @var Gumbo_Interface_Debug_Server_Report $_reports */
  43.     private $_reports = false;
  44.     
  45.     
  46.     
  47.     /**
  48.      * Constructor
  49.      */
  50.     private function __construct ({}
  51.     
  52.     /**
  53.      * Singleton Method
  54.      * @return Gumbo_Interface_Debug_Server 
  55.      */
  56.     public static function instance ({
  57.         if (self::$_instance == null{
  58.             self::$_instance new Gumbo_Debug_Server ();
  59.         }
  60.         return self::$_instance;
  61.     }
  62.     
  63.     
  64.     
  65.     /** ACTION METHODS **/
  66.     /**
  67.      * Returns a Debug Server Report
  68.      * @param string $name name of Class or key string
  69.      * @param mixed $args additional arguments
  70.      * @return Gumbo_Interface_Debug_Server_Report 
  71.      * @throws Gumbo_Exception
  72.      * @uses Gumbo_Debug_Server_Blank
  73.      */
  74.     public function factory ($name=null$args=null{
  75.         try {
  76.             // prepend Gumbo_Filter_ if class does not exist
  77.             if (is_string ($name&& !class_exists ($namefalse)) {
  78.                 $name "Gumbo_Debug_Server_" ucfirst (str_replace ("Gumbo_Debug_Server_"""$name));
  79.             }
  80.             
  81.             $args array ();
  82.             $args [$name;
  83.             foreach (func_get_args (as $key=>$val{
  84.                 if ($key == 0continue}
  85.                 $args [$val;
  86.             }
  87.             
  88.             $obj call_user_func_array (array ("parent""factory")$args);
  89.             
  90.             if (!($obj instanceof Gumbo_Interface_Debug_Server_Report)) {
  91.                 throw new Gumbo_Exception ("Invalid Debug Server Report: " get_class ($obj));
  92.             }
  93.             
  94.             return $obj;
  95.         catch (Gumbo_Exception $e{
  96.             $e->setFunction (__METHOD__);
  97.             gumbo_trigger ($e);
  98.         }
  99.         return new Gumbo_Debug_Server_Blank ();
  100.     }
  101.     
  102.     /**
  103.      * Activates a Server Report
  104.      * @param string $report Debug Server Report Name
  105.      * @param arr $args additional arguments
  106.      * @throws Gumbo_Exception
  107.      * @uses Gumbo_Debug_Server_Blank
  108.      */
  109.     public function activate ($report$args=null{
  110.         try {
  111.             // verify precondition
  112.             if (!is_string ($report)) {
  113.                 throw new Gumbo_Exception ("Invalid Argument 'report:str' => {$report}:gettype ($report));
  114.             }
  115.             if (!$this->exists ($report)) {
  116.                 throw new Gumbo_Exception ("Debug Server Report Not Found: {$report}");
  117.             }
  118.             if ($this->isActive ($report)) {
  119.                 throw new Gumbo_Exception ("Debug Server Report Active: {$report}");
  120.             }
  121.             
  122.             $args func_get_args ();
  123.             $obj call_user_func_array (array ($this"factory")$args);
  124.             if (!($obj instanceof Gumbo_Debug_Server_Blank)) {
  125.                 $this->_reports [$report$obj;
  126.             }
  127.         catch (Gumbo_Exception $e{
  128.             $e->setFunction (__METHOD__);
  129.             gumbo_trigger ($e);
  130.         }
  131.     }
  132.     
  133.     /**
  134.      * Deactivates a Server Report
  135.      * @param string $report Debug Server Report Name
  136.      * @throws Gumbo_Exception
  137.      */
  138.     public function deactivate ($report{
  139.         try {
  140.             // verify precondition
  141.             if (!is_string ($report)) {
  142.                 throw new Gumbo_Exception ("Invalid Argument 'report:str' => {$report}:gettype ($report));
  143.             }
  144.             if (!$this->isActive ($report)) {
  145.                 throw new Gumbo_Exception ("Debug Server Report Not Active: {$report}");
  146.             }
  147.             
  148.             unset ($this->_reports [$report]);
  149.         catch (Gumbo_Exception $e{
  150.             $e->setFunction (__METHOD__);
  151.             gumbo_trigger ($e);
  152.         }
  153.     }
  154.     
  155.     /**
  156.      * Resets the Reports
  157.      * @postcondition !isActive()
  158.      */
  159.     public function reset ({
  160.         $this->_reports = array ();
  161.     }
  162.     
  163.     /**
  164.      * Displays to the browser
  165.      */
  166.     public function display ({
  167.         echo $this->fetch ();
  168.     }
  169.     
  170.     /**
  171.      * Returns the formatted text to the browser
  172.      * @param bool $reload reloads the output
  173.      * @return string 
  174.      */
  175.     public function fetch ($reload=false{
  176.         $txt false;
  177.         
  178.         foreach ($this->getReports (as $key=>$report{
  179.             $txt .= $report->run ();
  180.         }
  181.         
  182.         return $txt;
  183.     }
  184.     
  185.     
  186.     
  187.     
  188.     /** ACCESSOR METHODS **/
  189.     /**
  190.      * Returns a single Report
  191.      * @param string $report 
  192.      * @return Gumbo_Interface_Debug_Server_Report 
  193.      * @throws Gumbo_Exception
  194.      */
  195.     public function getReport ($report{
  196.         try {
  197.             // verify precondition
  198.             if (!is_string ($report)) {
  199.                 throw new Gumbo_Exception ("Invalid Argument 'report:str' => {$report}:gettype ($report));
  200.             }
  201.             if (!$this->isActive ($report)) {
  202.                 throw new Gumbo_Exception ("Debug Server Report Not Active: {$report}");
  203.             }
  204.             
  205.             return $this->_reports [$report];
  206.         catch (Gumbo_Exception $e{
  207.             $e->setFunction (__METHOD__);
  208.             gumbo_trigger ($e);
  209.         }
  210.         return null;
  211.     }
  212.     
  213.     /**
  214.      * Returns the Debug Server Reports
  215.      * @return Gumbo_Interface_Debug_Server_Report[] 
  216.      */
  217.     public function getReports ({
  218.         return $this->_reports;
  219.     }
  220.     
  221.     /**
  222.      * Returns if a particular Report is active
  223.      * @param string $report 
  224.      * @return bool 
  225.      * @throws Gumbo_Exception
  226.      */
  227.     public function isActive ($report{
  228.         try {
  229.             // verify precondition
  230.             if (!is_string ($report)) {
  231.                 throw new Gumbo_Exception ("Invalid Argument 'report:str' => {$report}:gettype ($report));
  232.             }
  233.             
  234.             return isset ($this->_reports [$report]);
  235.         catch (Gumbo_Exception $e{
  236.             $e->setFunction (__METHOD__);
  237.             gumbo_trigger ($e);
  238.         }
  239.         return false;
  240.     }
  241.     
  242.     /**
  243.      * Returns if the Report exists
  244.      * @param string $report 
  245.      * @return bool 
  246.      * @throws Gumbo_Exception
  247.      */
  248.     public function exists ($report{
  249.         try {
  250.             // verify precondition
  251.             if (!is_string ($report)) {
  252.                 throw new Gumbo_Exception ("Invalid Argument 'report:str' => {$report}:gettype ($report));
  253.             }
  254.             
  255.             $report "Gumbo_Debug_Server_Report_" str_replace ("Gumbo_Debug_Server_Report_"""$report);
  256.             return gumbo_load ($report);
  257.         catch (Gumbo_Exception $e{
  258.             $e->setFunction (__METHOD__);
  259.             gumbo_trigger ($e);
  260.         }
  261.         return false;
  262.     }
  263.     
  264.     
  265.     
  266.     /**
  267.      * Returns the object as a string
  268.      * @return string 
  269.      */
  270.     public function __toString ({
  271.         return $this->fetch ();
  272.     }
  273.     
  274. }
  275.  
  276. ?>