Created
April 5, 2013 20:01
-
-
Save ocean90/5322171 to your computer and use it in GitHub Desktop.
#WordPressPlugin: Place the plugin into mu-plugins and log your PHP debug infos into the Chrome console. See http://www.chromephp.com/ for more infos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: Chrome Logger | |
| * Description: Logs PHP debug info into Chrome console | |
| * Author: Dominik Schilling | |
| */ | |
| /** | |
| * Server Side Chrome PHP debugger class | |
| * Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0 | |
| * Copyright 2012 Craig Campbell <iamcraigcampbell@gmail.com> | |
| */ | |
| class ChromePhp{const VERSION='3.0.1';const HEADER_NAME='X-ChromePhp-Data';const BACKTRACE_LEVEL='backtrace_level';const LOG='log';const WARN='warn';const ERROR='error';const GROUP='group';const INFO='info';const GROUP_END='groupEnd';const GROUP_COLLAPSED='groupCollapsed';protected $_php_version;protected $_timestamp;protected $_json=array('version'=>self::VERSION,'columns'=>array('label','log','backtrace','type'),'rows'=>array());protected $_backtraces=array();protected $_error_triggered=false;protected $_settings=array(self::BACKTRACE_LEVEL=>1);protected static $_instance;protected $_processed=array();private function __construct(){$this->_php_version=phpversion();$this->_timestamp=$this->_php_version>=5.1?$_SERVER['REQUEST_TIME']:time();$this->_json['request_uri']=$_SERVER['REQUEST_URI'];}public static function getInstance(){if(self::$_instance===null){self::$_instance=new self();}return self::$_instance;}public static function log(){$args=func_get_args();$severity=count($args)==3?array_pop($args):'';if($severity==self::LOG){$severity='';}return self::_log($args+array('type'=>$severity));}public static function warn(){return self::_log(func_get_args()+array('type'=>self::WARN));}public static function error(){return self::_log(func_get_args()+array('type'=>self::ERROR));}public static function group(){return self::_log(func_get_args()+array('type'=>self::GROUP));}public static function info(){return self::_log(func_get_args()+array('type'=>self::INFO));}public static function groupCollapsed(){return self::_log(func_get_args()+array('type'=>self::GROUP_COLLAPSED));}public static function groupEnd(){return self::_log(func_get_args()+array('type'=>self::GROUP_END));}protected static function _log(array $args){$type=$args['type'];unset($args['type']);if(count($args)==0&&$type!=self::GROUP_END){return;}$label=null;$value=isset($args[0])?$args[0]:'';$logger=self::getInstance();if(count($args)==2){$label=$args[0];$value=$args[1];}$logger->_processed=array();$value=$logger->_convert($value);$backtrace=debug_backtrace(false);$level=$logger->getSetting(self::BACKTRACE_LEVEL);$backtrace_message='unknown';if(isset($backtrace[$level]['file'])&&isset($backtrace[$level]['line'])){$backtrace_message=$backtrace[$level]['file'].' : '.$backtrace[$level]['line'];}$logger->_addRow($label,$value,$backtrace_message,$type);}protected function _convert($object){if(!is_object($object)){return $object;}$this->_processed[]=$object;$object_as_array=array();$object_as_array['___class_name']=get_class($object);$object_vars=get_object_vars($object);foreach($object_vars as $key=>$value){if($value===$object||in_array($value,$this->_processed,true)){$value='recursion - parent object ['.get_class($value).']';}$object_as_array[$key]=$this->_convert($value);}$reflection=new ReflectionClass($object);foreach($reflection->getProperties()as $property){if(array_key_exists($property->getName(),$object_vars)){continue;}$type=$this->_getPropertyKey($property);if($this->_php_version>=5.3){$property->setAccessible(true);}try{$value=$property->getValue($object);}catch(ReflectionException $e){$value='only PHP 5.3 can access private/protected properties';}if($value===$object||in_array($value,$this->_processed,true)){$value='recursion - parent object ['.get_class($value).']';}$object_as_array[$type]=$this->_convert($value);}return $object_as_array;}protected function _getPropertyKey(ReflectionProperty $property){$static=$property->isStatic()?' static':'';if($property->isPublic()){return 'public'.$static.' '.$property->getName();}if($property->isProtected()){return 'protected'.$static.' '.$property->getName();}if($property->isPrivate()){return 'private'.$static.' '.$property->getName();}}protected function _addRow($label,$log,$backtrace,$type){if(in_array($backtrace,$this->_backtraces)){$backtrace=null;}if($backtrace!==null){$this->_backtraces[]=$backtrace;}$row=array($label,$log,$backtrace,$type);$this->_json['rows'][]=$row;$this->_writeHeader($this->_json);}protected function _writeHeader($data){header(self::HEADER_NAME.': '.$this->_encode($data));}protected function _encode($data){return base64_encode(utf8_encode(json_encode($data)));}public function addSetting($key,$value){$this->_settings[$key]=$value;}public function addSettings(array $settings){foreach($settings as $key=>$value){$this->addSetting($key,$value);}}public function getSetting($key){if(!isset($this->_settings[$key])){return null;}return $this->_settings[$key];}} | |
| /** | |
| * Wrapper for ChromePhp::log() | |
| */ | |
| function __l() { | |
| $args = func_get_args(); | |
| if ( 1 == count( $args ) ) | |
| ChromePhp::log( $args[0] ); | |
| else | |
| ChromePhp::log( $args[1], $args[1] ); | |
| }; | |
| /** | |
| * Wrapper for ChromePhp::warn() | |
| */ | |
| function __w( $value ) { | |
| ChromePhp::warn( $value ); | |
| } | |
| /** | |
| * Wrapper for ChromePhp::error() | |
| */ | |
| function __e( $value ) { | |
| ChromePhp::error( $value ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Es gibt dazu ein plugin auf github alternativ habe ich es in Debug Objects drin. vielleicht hilft dir das oder du kannst es verbessern.