eZ Publish  [trunk]
debug.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing ezpRestDebug class
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 final class ezpRestDebug
00011 {
00012     private static $instance;
00013 
00014     /**
00015      * @var eZINI
00016      */
00017     private $restINI;
00018 
00019     /**
00020      * eZDebug instance
00021      * @var eZDebug
00022      */
00023     private $eZDebug;
00024 
00025     /**
00026      * @var ezcDebug
00027      */
00028     private $debug;
00029 
00030     /**
00031      * @var bool
00032      */
00033     private static $isDebugEnabled;
00034 
00035     /**
00036      * Private constructor
00037      */
00038     private function __construct()
00039     {
00040         $this->restINI = eZINI::instance( 'rest.ini' );
00041         $this->eZDebug = eZDebug::instance();
00042         $this->debug = ezcDebug::getInstance();
00043         $this->debug->setOutputFormatter( new ezpRestDebugPHPFormatter() );
00044     }
00045 
00046     /**
00047      * Singleton. Get instance of the class
00048      */
00049     public static function getInstance()
00050     {
00051         if ( !self::$instance instanceof ezpRestDebugHandler)
00052         {
00053             self::$instance = new self();
00054         }
00055 
00056         return self::$instance;
00057     }
00058 
00059     /**
00060      * Checks if debug is enabled (locally in rest.ini and globally in site.ini)
00061      * @return bool
00062      */
00063     public static function isDebugEnabled()
00064     {
00065         if( self::$isDebugEnabled === null )
00066         {
00067             $isEnabled = false;
00068             $globalDebugEnabled = eZINI::instance()->variable( 'DebugSettings', 'DebugOutput' ) === 'enabled';
00069             $localDebugEnabled = eZINI::instance( 'rest.ini' )->variable( 'DebugSettings', 'Debug' ) === 'enabled';
00070 
00071             if( $globalDebugEnabled && $localDebugEnabled )
00072                 $isEnabled = true;
00073 
00074             self::$isDebugEnabled = $isEnabled;
00075         }
00076         return self::$isDebugEnabled;
00077     }
00078 
00079     /**
00080      * Returns debug report
00081      */
00082     public function getReport()
00083     {
00084         $report = array();
00085 
00086         $report['restDebug'] = $this->debug->generateOutput();
00087 
00088         $reportEZDebug = $this->eZDebug->printReportInternal( false );
00089         $report['eZDebug'] = explode( "\n", $reportEZDebug );
00090 
00091 
00092         return $report;
00093     }
00094 
00095     /**
00096      * Initializes/updates debug settings, system wide
00097      */
00098     public function updateDebugSettings()
00099     {
00100         $ini = eZINI::instance();
00101         $debugSettings = array();
00102         $debugSettings['debug-enabled'] = ( $ini->variable( 'DebugSettings', 'DebugOutput' ) == 'enabled' and
00103                                             $this->restINI->variable( 'DebugSettings', 'Debug' ) == 'enabled' );
00104         $debugSettings['debug-by-ip'] = $ini->variable( 'DebugSettings', 'DebugByIP' ) == 'enabled';
00105         $debugSettings['debug-ip-list'] = $ini->variable( 'DebugSettings', 'DebugIPList' );
00106 
00107         $logList = $ini->variable( 'DebugSettings', 'AlwaysLog' );
00108         $logMap = array( 'notice' => eZDebug::LEVEL_NOTICE,
00109                          'warning' => eZDebug::LEVEL_WARNING,
00110                          'error' => eZDebug::LEVEL_ERROR,
00111                          'debug' => eZDebug::LEVEL_DEBUG,
00112                          'strict' => eZDebug::LEVEL_STRICT );
00113         $debugSettings['always-log'] = array();
00114         foreach ( $logMap as $name => $level )
00115         {
00116             $debugSettings['always-log'][$level] = in_array( $name, $logList );
00117         }
00118         eZDebug::updateSettings( $debugSettings );
00119     }
00120 
00121     /**
00122      * Generic safe way to access ezcDebug public methods
00123      * @param $method
00124      * @param $arguments
00125      */
00126     public function __call( $method, $arguments )
00127     {
00128         if( self::isDebugEnabled() )
00129         {
00130             if ( method_exists( $this->debug, $method ) )
00131                 return call_user_func_array( array( $this->debug, $method ), $arguments );
00132             else
00133                 throw new ezcBasePropertyNotFoundException( 'ezcDebug::'.$method.'()' );
00134         }
00135     }
00136 }
00137 ?>