web/lib/Zend/Wildfire/Plugin/FirePhp.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Wildfire
       
    17  * @subpackage Plugin
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: FirePhp.php 23066 2010-10-09 23:29:20Z cadorn $
       
    21  */
       
    22 
       
    23 /** Zend_Controller_Request_Abstract */
       
    24 require_once('Zend/Controller/Request/Abstract.php');
       
    25 
       
    26 /** Zend_Controller_Response_Abstract */
       
    27 require_once('Zend/Controller/Response/Abstract.php');
       
    28 
       
    29 /** Zend_Wildfire_Channel_HttpHeaders */
       
    30 require_once 'Zend/Wildfire/Channel/HttpHeaders.php';
       
    31 
       
    32 /** Zend_Wildfire_Protocol_JsonStream */
       
    33 require_once 'Zend/Wildfire/Protocol/JsonStream.php';
       
    34 
       
    35 /** Zend_Wildfire_Plugin_Interface */
       
    36 require_once 'Zend/Wildfire/Plugin/Interface.php';
       
    37 
       
    38 /**
       
    39  * Primary class for communicating with the FirePHP Firefox Extension.
       
    40  *
       
    41  * @category   Zend
       
    42  * @package    Zend_Wildfire
       
    43  * @subpackage Plugin
       
    44  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    46  */
       
    47 class Zend_Wildfire_Plugin_FirePhp implements Zend_Wildfire_Plugin_Interface
       
    48 {
       
    49     /**
       
    50      * Plain log style.
       
    51      */
       
    52     const LOG = 'LOG';
       
    53 
       
    54     /**
       
    55      * Information style.
       
    56      */
       
    57     const INFO = 'INFO';
       
    58 
       
    59     /**
       
    60      * Warning style.
       
    61      */
       
    62     const WARN = 'WARN';
       
    63 
       
    64     /**
       
    65      * Error style that increments Firebug's error counter.
       
    66      */
       
    67     const ERROR = 'ERROR';
       
    68 
       
    69     /**
       
    70      * Trace style showing message and expandable full stack trace.
       
    71      */
       
    72     const TRACE = 'TRACE';
       
    73 
       
    74     /**
       
    75      * Exception style showing message and expandable full stack trace.
       
    76      * Also increments Firebug's error counter.
       
    77      */
       
    78     const EXCEPTION = 'EXCEPTION';
       
    79 
       
    80     /**
       
    81      * Table style showing summary line and expandable table
       
    82      */
       
    83     const TABLE = 'TABLE';
       
    84 
       
    85     /**
       
    86      * Dump variable to Server panel in Firebug Request Inspector
       
    87      */
       
    88     const DUMP = 'DUMP';
       
    89 
       
    90     /**
       
    91      * Start a group in the Firebug Console
       
    92      */
       
    93     const GROUP_START = 'GROUP_START';
       
    94 
       
    95     /**
       
    96      * End a group in the Firebug Console
       
    97      */
       
    98     const GROUP_END = 'GROUP_END';
       
    99 
       
   100     /**
       
   101      * The plugin URI for this plugin
       
   102      */
       
   103     const PLUGIN_URI = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
       
   104 
       
   105     /**
       
   106      * The protocol URI for this plugin
       
   107      */
       
   108     const PROTOCOL_URI = Zend_Wildfire_Protocol_JsonStream::PROTOCOL_URI;
       
   109 
       
   110     /**
       
   111      * The structure URI for the Dump structure
       
   112      */
       
   113     const STRUCTURE_URI_DUMP = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1';
       
   114 
       
   115     /**
       
   116      * The structure URI for the Firebug Console structure
       
   117      */
       
   118     const STRUCTURE_URI_FIREBUGCONSOLE = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
       
   119 
       
   120     /**
       
   121      * Singleton instance
       
   122      * @var Zend_Wildfire_Plugin_FirePhp
       
   123      */
       
   124     protected static $_instance = null;
       
   125 
       
   126     /**
       
   127      * Flag indicating whether FirePHP should send messages to the user-agent.
       
   128      * @var boolean
       
   129      */
       
   130     protected $_enabled = true;
       
   131 
       
   132     /**
       
   133      * The channel via which to send the encoded messages.
       
   134      * @var Zend_Wildfire_Channel_Interface
       
   135      */
       
   136     protected $_channel = null;
       
   137 
       
   138     /**
       
   139      * Messages that are buffered to be sent when protocol flushes
       
   140      * @var array
       
   141      */
       
   142     protected $_messages = array();
       
   143 
       
   144     /**
       
   145      * Options for the object
       
   146      * @var array
       
   147      */
       
   148     protected $_options = array(
       
   149         'traceOffset' => 1, /* The offset in the trace which identifies the source of the message */
       
   150         'maxTraceDepth' => 99, /* Maximum depth for stack traces */
       
   151         'maxObjectDepth' => 10, /* The maximum depth to traverse objects when encoding */
       
   152         'maxArrayDepth' => 20, /* The maximum depth to traverse nested arrays when encoding */
       
   153         'includeLineNumbers' => true /* Whether to include line and file info for each message */
       
   154     );
       
   155 
       
   156     /**
       
   157      * Filters used to exclude object members when encoding
       
   158      * @var array
       
   159      */
       
   160     protected $_objectFilters = array();
       
   161 
       
   162     /**
       
   163      * A stack of objects used during encoding to detect recursion
       
   164      * @var array
       
   165      */
       
   166     protected $_objectStack = array();
       
   167 
       
   168     /**
       
   169      * Create singleton instance.
       
   170      *
       
   171      * @param string $class OPTIONAL Subclass of Zend_Wildfire_Plugin_FirePhp
       
   172      * @return Zend_Wildfire_Plugin_FirePhp Returns the singleton Zend_Wildfire_Plugin_FirePhp instance
       
   173      * @throws Zend_Wildfire_Exception
       
   174      */
       
   175     public static function init($class = null)
       
   176     {
       
   177         if (self::$_instance !== null) {
       
   178             require_once 'Zend/Wildfire/Exception.php';
       
   179             throw new Zend_Wildfire_Exception('Singleton instance of Zend_Wildfire_Plugin_FirePhp already exists!');
       
   180         }
       
   181         if ($class !== null) {
       
   182             if (!is_string($class)) {
       
   183                 require_once 'Zend/Wildfire/Exception.php';
       
   184                 throw new Zend_Wildfire_Exception('Third argument is not a class string');
       
   185             }
       
   186 
       
   187             if (!class_exists($class)) {
       
   188                 require_once 'Zend/Loader.php';
       
   189                 Zend_Loader::loadClass($class);
       
   190             }
       
   191             self::$_instance = new $class();
       
   192             if (!self::$_instance instanceof Zend_Wildfire_Plugin_FirePhp) {
       
   193                 self::$_instance = null;
       
   194                 require_once 'Zend/Wildfire/Exception.php';
       
   195                 throw new Zend_Wildfire_Exception('Invalid class to third argument. Must be subclass of Zend_Wildfire_Plugin_FirePhp.');
       
   196             }
       
   197         } else {
       
   198             self::$_instance = new self();
       
   199         }
       
   200 
       
   201         return self::$_instance;
       
   202     }
       
   203 
       
   204     /**
       
   205      * Constructor
       
   206      * @return void
       
   207      */
       
   208     protected function __construct()
       
   209     {
       
   210         $this->_channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
       
   211         $this->_channel->getProtocol(self::PROTOCOL_URI)->registerPlugin($this);
       
   212     }
       
   213 
       
   214     /**
       
   215      * Get or create singleton instance
       
   216      *
       
   217      * @param $skipCreate boolean True if an instance should not be created
       
   218      * @return Zend_Wildfire_Plugin_FirePhp
       
   219      */
       
   220     public static function getInstance($skipCreate=false)
       
   221     {
       
   222         if (self::$_instance===null && $skipCreate!==true) {
       
   223             return self::init();
       
   224         }
       
   225         return self::$_instance;
       
   226     }
       
   227 
       
   228     /**
       
   229      * Destroys the singleton instance
       
   230      *
       
   231      * Primarily used for testing.
       
   232      *
       
   233      * @return void
       
   234      */
       
   235     public static function destroyInstance()
       
   236     {
       
   237         self::$_instance = null;
       
   238     }
       
   239 
       
   240     /**
       
   241      * Enable or disable sending of messages to user-agent.
       
   242      * If disabled all headers to be sent will be removed.
       
   243      *
       
   244      * @param boolean $enabled Set to TRUE to enable sending of messages.
       
   245      * @return boolean The previous value.
       
   246      */
       
   247     public function setEnabled($enabled)
       
   248     {
       
   249         $previous = $this->_enabled;
       
   250         $this->_enabled = $enabled;
       
   251         if (!$this->_enabled) {
       
   252             $this->_messages = array();
       
   253             $this->_channel->getProtocol(self::PROTOCOL_URI)->clearMessages($this);
       
   254         }
       
   255         return $previous;
       
   256     }
       
   257 
       
   258     /**
       
   259      * Determine if logging to user-agent is enabled.
       
   260      *
       
   261      * @return boolean Returns TRUE if logging is enabled.
       
   262      */
       
   263     public function getEnabled()
       
   264     {
       
   265         return $this->_enabled;
       
   266     }
       
   267 
       
   268     /**
       
   269      * Set a single option
       
   270      *
       
   271      * @param  string $key The name of the option
       
   272      * @param  mixed $value The value of the option
       
   273      * @return mixed The previous value of the option
       
   274      */
       
   275     public function setOption($key, $value)
       
   276     {
       
   277       if (!array_key_exists($key,$this->_options)) {
       
   278         throw new Zend_Wildfire_Exception('Option with name "'.$key.'" does not exist!');
       
   279       }
       
   280       $previous = $this->_options[$key];
       
   281       $this->_options[$key] = $value;
       
   282       return $previous;
       
   283     }
       
   284 
       
   285     /**
       
   286      * Retrieve a single option
       
   287      *
       
   288      * @param  string $key The name of the option
       
   289      * @return mixed The value of the option
       
   290      */
       
   291     public function getOption($key)
       
   292     {
       
   293       if (!array_key_exists($key,$this->_options)) {
       
   294         throw new Zend_Wildfire_Exception('Option with name "'.$key.'" does not exist!');
       
   295       }
       
   296       return $this->_options[$key];
       
   297     }
       
   298 
       
   299     /**
       
   300      * Retrieve all options
       
   301      *
       
   302      * @return array All options
       
   303      */
       
   304     public function getOptions()
       
   305     {
       
   306       return $this->_options;
       
   307     }
       
   308 
       
   309     /**
       
   310      * Specify a filter to be used when encoding an object
       
   311      *
       
   312      * Filters are used to exclude object members.
       
   313      *
       
   314      * @param string $Class The class name of the object
       
   315      * @param array $Filter An array of members to exclude
       
   316      * @return void
       
   317      */
       
   318     public function setObjectFilter($class, $filter) {
       
   319       $this->_objectFilters[$class] = $filter;
       
   320     }
       
   321 
       
   322     /**
       
   323      * Starts a group in the Firebug Console
       
   324      *
       
   325      * @param string $title The title of the group
       
   326      * @return TRUE if the group instruction was added to the response headers or buffered.
       
   327      */
       
   328     public static function group($title)
       
   329     {
       
   330         return self::send(null, $title, self::GROUP_START);
       
   331     }
       
   332 
       
   333     /**
       
   334      * Ends a group in the Firebug Console
       
   335      *
       
   336      * @return TRUE if the group instruction was added to the response headers or buffered.
       
   337      */
       
   338     public static function groupEnd()
       
   339     {
       
   340         return self::send(null, null, self::GROUP_END);
       
   341     }
       
   342 
       
   343     /**
       
   344      * Logs variables to the Firebug Console
       
   345      * via HTTP response headers and the FirePHP Firefox Extension.
       
   346      *
       
   347      * @param  mixed  $var   The variable to log.
       
   348      * @param  string  $label OPTIONAL Label to prepend to the log event.
       
   349      * @param  string  $style  OPTIONAL Style of the log event.
       
   350      * @param  array  $options OPTIONAL Options to change how messages are processed and sent
       
   351      * @return boolean Returns TRUE if the variable was added to the response headers or buffered.
       
   352      * @throws Zend_Wildfire_Exception
       
   353      */
       
   354     public static function send($var, $label=null, $style=null, $options=array())
       
   355     {
       
   356         $firephp = self::getInstance();
       
   357 
       
   358         if (!$firephp->getEnabled()) {
       
   359             return false;
       
   360         }
       
   361 
       
   362         if ($var instanceof Zend_Wildfire_Plugin_FirePhp_Message) {
       
   363 
       
   364             if ($var->getBuffered()) {
       
   365                 if (!in_array($var, self::$_instance->_messages)) {
       
   366                     self::$_instance->_messages[] = $var;
       
   367                 }
       
   368                 return true;
       
   369             }
       
   370 
       
   371             if ($var->getDestroy()) {
       
   372                 return false;
       
   373             }
       
   374 
       
   375             $style = $var->getStyle();
       
   376             $label = $var->getLabel();
       
   377             $options = $var->getOptions();
       
   378             $var = $var->getMessage();
       
   379         }
       
   380 
       
   381         if (!self::$_instance->_channel->isReady()) {
       
   382             return false;
       
   383         }
       
   384 
       
   385         foreach ($options as $name => $value) {
       
   386             if ($value===null) {
       
   387                 unset($options[$name]);
       
   388             }
       
   389         }
       
   390         $options = array_merge($firephp->getOptions(), $options);
       
   391 
       
   392         $trace = null;
       
   393 
       
   394         $skipFinalEncode = false;
       
   395 
       
   396         $meta = array();
       
   397         $meta['Type'] = $style;
       
   398 
       
   399         if ($var instanceof Exception) {
       
   400 
       
   401             $eTrace = $var->getTrace();
       
   402             $eTrace = array_splice($eTrace, 0, $options['maxTraceDepth']);
       
   403 
       
   404             $var = array('Class'=>get_class($var),
       
   405                          'Message'=>$var->getMessage(),
       
   406                          'File'=>$var->getFile(),
       
   407                          'Line'=>$var->getLine(),
       
   408                          'Type'=>'throw',
       
   409                          'Trace'=>$firephp->_encodeTrace($eTrace));
       
   410 
       
   411             $meta['Type'] = self::EXCEPTION;
       
   412 
       
   413             $skipFinalEncode = true;
       
   414 
       
   415         } else
       
   416         if ($meta['Type']==self::TRACE) {
       
   417 
       
   418             if (!$label && $var) {
       
   419                 $label = $var;
       
   420                 $var = null;
       
   421             }
       
   422 
       
   423             if (!$trace) {
       
   424                 $trace = $firephp->_getStackTrace(array_merge($options,
       
   425                                                               array('maxTraceDepth'=>$options['maxTraceDepth']+1)));
       
   426             }
       
   427 
       
   428             $var = array('Class'=>$trace[0]['class'],
       
   429                          'Type'=>$trace[0]['type'],
       
   430                          'Function'=>$trace[0]['function'],
       
   431                          'Message'=>$label,
       
   432                          'File'=>isset($trace[0]['file'])?$trace[0]['file']:'',
       
   433                          'Line'=>isset($trace[0]['line'])?$trace[0]['line']:'',
       
   434                          'Args'=>isset($trace[0]['args'])?$firephp->_encodeObject($trace[0]['args']):'',
       
   435                          'Trace'=>$firephp->_encodeTrace(array_splice($trace,1)));
       
   436 
       
   437           $skipFinalEncode = true;
       
   438 
       
   439         } else
       
   440         if ($meta['Type']==self::TABLE) {
       
   441 
       
   442           $var = $firephp->_encodeTable($var);
       
   443 
       
   444           $skipFinalEncode = true;
       
   445 
       
   446         } else {
       
   447             if ($meta['Type']===null) {
       
   448                 $meta['Type'] = self::LOG;
       
   449             }
       
   450         }
       
   451 
       
   452         if ($label!=null) {
       
   453             $meta['Label'] = $label;
       
   454         }
       
   455 
       
   456         switch ($meta['Type']) {
       
   457             case self::LOG:
       
   458             case self::INFO:
       
   459             case self::WARN:
       
   460             case self::ERROR:
       
   461             case self::EXCEPTION:
       
   462             case self::TRACE:
       
   463             case self::TABLE:
       
   464             case self::DUMP:
       
   465             case self::GROUP_START:
       
   466             case self::GROUP_END:
       
   467                 break;
       
   468             default:
       
   469                 require_once 'Zend/Wildfire/Exception.php';
       
   470                 throw new Zend_Wildfire_Exception('Log style "'.$meta['Type'].'" not recognized!');
       
   471                 break;
       
   472         }
       
   473 
       
   474         if ($meta['Type'] != self::DUMP && $options['includeLineNumbers']) {
       
   475             if (!isset($meta['File']) || !isset($meta['Line'])) {
       
   476 
       
   477                 if (!$trace) {
       
   478                     $trace = $firephp->_getStackTrace(array_merge($options,
       
   479                                                                   array('maxTraceDepth'=>$options['maxTraceDepth']+1)));
       
   480                 }
       
   481 
       
   482                 $meta['File'] = isset($trace[0]['file'])?$trace[0]['file']:'';
       
   483                 $meta['Line'] = isset($trace[0]['line'])?$trace[0]['line']:'';
       
   484 
       
   485             }
       
   486         } else {
       
   487             unset($meta['File']);
       
   488             unset($meta['Line']);
       
   489         }
       
   490 
       
   491         if ($meta['Type'] == self::DUMP) {
       
   492 
       
   493           return $firephp->_recordMessage(self::STRUCTURE_URI_DUMP,
       
   494                                           array('key'=>$meta['Label'],
       
   495                                                 'data'=>$var),
       
   496                                           $skipFinalEncode);
       
   497 
       
   498         } else {
       
   499 
       
   500           return $firephp->_recordMessage(self::STRUCTURE_URI_FIREBUGCONSOLE,
       
   501                                           array('data'=>$var,
       
   502                                                 'meta'=>$meta),
       
   503                                           $skipFinalEncode);
       
   504         }
       
   505     }
       
   506 
       
   507     /**
       
   508      * Gets a stack trace
       
   509      *
       
   510      * @param array $options Options to change how the stack trace is returned
       
   511      * @return array The stack trace
       
   512      */
       
   513     protected function _getStackTrace($options)
       
   514     {
       
   515         $trace = debug_backtrace();
       
   516 
       
   517         $trace = array_splice($trace, $options['traceOffset']);
       
   518         
       
   519         if (!count($trace)) {
       
   520             return $trace;
       
   521         }
       
   522 
       
   523         if (isset($options['fixZendLogOffsetIfApplicable']) && $options['fixZendLogOffsetIfApplicable']) {
       
   524             if (count($trace) >=3 &&
       
   525                 isset($trace[0]['file']) && substr($trace[0]['file'], -7, 7)=='Log.php' &&
       
   526                 isset($trace[1]['function']) && $trace[1]['function']=='__call') {
       
   527 
       
   528                 $trace = array_splice($trace, 2);
       
   529             }
       
   530         }
       
   531 
       
   532         return array_splice($trace, 0, $options['maxTraceDepth']);
       
   533     }
       
   534 
       
   535     /**
       
   536      * Record a message with the given data in the given structure
       
   537      *
       
   538      * @param string $structure The structure to be used for the data
       
   539      * @param array $data The data to be recorded
       
   540      * @param boolean $skipEncode TRUE if variable encoding should be skipped
       
   541      * @return boolean Returns TRUE if message was recorded
       
   542      * @throws Zend_Wildfire_Exception
       
   543      */
       
   544     protected function _recordMessage($structure, $data, $skipEncode=false)
       
   545     {
       
   546         switch($structure) {
       
   547 
       
   548             case self::STRUCTURE_URI_DUMP:
       
   549 
       
   550                 if (!isset($data['key'])) {
       
   551                     require_once 'Zend/Wildfire/Exception.php';
       
   552                     throw new Zend_Wildfire_Exception('You must supply a key.');
       
   553                 }
       
   554                 if (!array_key_exists('data',$data)) {
       
   555                     require_once 'Zend/Wildfire/Exception.php';
       
   556                     throw new Zend_Wildfire_Exception('You must supply data.');
       
   557                 }
       
   558 
       
   559                 $value = $data['data'];
       
   560                 if (!$skipEncode) {
       
   561                   $value = $this->_encodeObject($data['data']);
       
   562                 }
       
   563 
       
   564                 return $this->_channel->getProtocol(self::PROTOCOL_URI)->
       
   565                            recordMessage($this,
       
   566                                          $structure,
       
   567                                          array($data['key']=>$value));
       
   568 
       
   569             case self::STRUCTURE_URI_FIREBUGCONSOLE:
       
   570 
       
   571                 if (!isset($data['meta']) ||
       
   572                     !is_array($data['meta']) ||
       
   573                     !array_key_exists('Type',$data['meta'])) {
       
   574 
       
   575                     require_once 'Zend/Wildfire/Exception.php';
       
   576                     throw new Zend_Wildfire_Exception('You must supply a "Type" in the meta information.');
       
   577                 }
       
   578                 if (!array_key_exists('data',$data)) {
       
   579                     require_once 'Zend/Wildfire/Exception.php';
       
   580                     throw new Zend_Wildfire_Exception('You must supply data.');
       
   581                 }
       
   582 
       
   583                 $value = $data['data'];
       
   584                 if (!$skipEncode) {
       
   585                   $value = $this->_encodeObject($data['data']);
       
   586                 }
       
   587 
       
   588                 return $this->_channel->getProtocol(self::PROTOCOL_URI)->
       
   589                            recordMessage($this,
       
   590                                          $structure,
       
   591                                          array($data['meta'],
       
   592                                                $value));
       
   593 
       
   594             default:
       
   595                 require_once 'Zend/Wildfire/Exception.php';
       
   596                 throw new Zend_Wildfire_Exception('Structure of name "'.$structure.'" is not recognized.');
       
   597                 break;
       
   598         }
       
   599         return false;
       
   600     }
       
   601 
       
   602     /**
       
   603      * Encodes a table by encoding each row and column with _encodeObject()
       
   604      *
       
   605      * @param array $Table The table to be encoded
       
   606      * @return array
       
   607      */
       
   608     protected function _encodeTable($table)
       
   609     {
       
   610       if (!$table) {
       
   611           return $table;
       
   612       }
       
   613       for ($i=0 ; $i<count($table) ; $i++) {
       
   614           if (is_array($table[$i])) {
       
   615               for ($j=0 ; $j<count($table[$i]) ; $j++) {
       
   616                   $table[$i][$j] = $this->_encodeObject($table[$i][$j]);
       
   617               }
       
   618           }
       
   619         }
       
   620       return $table;
       
   621     }
       
   622 
       
   623     /**
       
   624      * Encodes a trace by encoding all "args" with _encodeObject()
       
   625      *
       
   626      * @param array $Trace The trace to be encoded
       
   627      * @return array The encoded trace
       
   628      */
       
   629     protected function _encodeTrace($trace)
       
   630     {
       
   631       if (!$trace) {
       
   632           return $trace;
       
   633       }
       
   634       for ($i=0 ; $i<sizeof($trace) ; $i++) {
       
   635           if (isset($trace[$i]['args'])) {
       
   636               $trace[$i]['args'] = $this->_encodeObject($trace[$i]['args']);
       
   637           }
       
   638       }
       
   639       return $trace;
       
   640     }
       
   641 
       
   642     /**
       
   643      * Encode an object by generating an array containing all object members.
       
   644      *
       
   645      * All private and protected members are included. Some meta info about
       
   646      * the object class is added.
       
   647      *
       
   648      * @param mixed $object The object/array/value to be encoded
       
   649      * @return array The encoded object
       
   650      */
       
   651     protected function _encodeObject($object, $objectDepth = 1, $arrayDepth = 1)
       
   652     {
       
   653         $return = array();
       
   654 
       
   655         if (is_resource($object)) {
       
   656 
       
   657             return '** '.(string)$object.' **';
       
   658 
       
   659         } else
       
   660         if (is_object($object)) {
       
   661 
       
   662             if ($objectDepth > $this->_options['maxObjectDepth']) {
       
   663               return '** Max Object Depth ('.$this->_options['maxObjectDepth'].') **';
       
   664             }
       
   665 
       
   666             foreach ($this->_objectStack as $refVal) {
       
   667                 if ($refVal === $object) {
       
   668                     return '** Recursion ('.get_class($object).') **';
       
   669                 }
       
   670             }
       
   671             array_push($this->_objectStack, $object);
       
   672 
       
   673             $return['__className'] = $class = get_class($object);
       
   674 
       
   675             $reflectionClass = new ReflectionClass($class);
       
   676             $properties = array();
       
   677             foreach ( $reflectionClass->getProperties() as $property) {
       
   678                 $properties[$property->getName()] = $property;
       
   679             }
       
   680 
       
   681             $members = (array)$object;
       
   682 
       
   683             foreach ($properties as $just_name => $property) {
       
   684 
       
   685                 $name = $raw_name = $just_name;
       
   686 
       
   687                 if ($property->isStatic()) {
       
   688                     $name = 'static:'.$name;
       
   689                 }
       
   690                 if ($property->isPublic()) {
       
   691                     $name = 'public:'.$name;
       
   692                 } else
       
   693                 if ($property->isPrivate()) {
       
   694                     $name = 'private:'.$name;
       
   695                     $raw_name = "\0".$class."\0".$raw_name;
       
   696                 } else
       
   697                 if ($property->isProtected()) {
       
   698                     $name = 'protected:'.$name;
       
   699                     $raw_name = "\0".'*'."\0".$raw_name;
       
   700                 }
       
   701 
       
   702                 if (!(isset($this->_objectFilters[$class])
       
   703                       && is_array($this->_objectFilters[$class])
       
   704                       && in_array($just_name,$this->_objectFilters[$class]))) {
       
   705 
       
   706                     if (array_key_exists($raw_name,$members)
       
   707                         && !$property->isStatic()) {
       
   708 
       
   709                         $return[$name] = $this->_encodeObject($members[$raw_name], $objectDepth + 1, 1);
       
   710 
       
   711                     } else {
       
   712                         if (method_exists($property,'setAccessible')) {
       
   713                             $property->setAccessible(true);
       
   714                             $return[$name] = $this->_encodeObject($property->getValue($object), $objectDepth + 1, 1);
       
   715                         } else
       
   716                         if ($property->isPublic()) {
       
   717                             $return[$name] = $this->_encodeObject($property->getValue($object), $objectDepth + 1, 1);
       
   718                         } else {
       
   719                             $return[$name] = '** Need PHP 5.3 to get value **';
       
   720                         }
       
   721                     }
       
   722                 } else {
       
   723                   $return[$name] = '** Excluded by Filter **';
       
   724                 }
       
   725             }
       
   726 
       
   727             // Include all members that are not defined in the class
       
   728             // but exist in the object
       
   729             foreach($members as $just_name => $value) {
       
   730 
       
   731                 $name = $raw_name = $just_name;
       
   732 
       
   733                 if ($name{0} == "\0") {
       
   734                     $parts = explode("\0", $name);
       
   735                     $name = $parts[2];
       
   736                 }
       
   737                 if (!isset($properties[$name])) {
       
   738                     $name = 'undeclared:'.$name;
       
   739 
       
   740                     if (!(isset($this->objectFilters[$class])
       
   741                           && is_array($this->objectFilters[$class])
       
   742                           && in_array($just_name,$this->objectFilters[$class]))) {
       
   743 
       
   744                       $return[$name] = $this->_encodeObject($value, $objectDepth + 1, 1);
       
   745                     } else {
       
   746                       $return[$name] = '** Excluded by Filter **';
       
   747                     }
       
   748                 }
       
   749             }
       
   750 
       
   751             array_pop($this->_objectStack);
       
   752 
       
   753         } elseif (is_array($object)) {
       
   754 
       
   755             if ($arrayDepth > $this->_options['maxArrayDepth']) {
       
   756               return '** Max Array Depth ('.$this->_options['maxArrayDepth'].') **';
       
   757             }
       
   758 
       
   759             foreach ($object as $key => $val) {
       
   760 
       
   761               // Encoding the $GLOBALS PHP array causes an infinite loop
       
   762               // if the recursion is not reset here as it contains
       
   763               // a reference to itself. This is the only way I have come up
       
   764               // with to stop infinite recursion in this case.
       
   765               if ($key=='GLOBALS'
       
   766                   && is_array($val)
       
   767                   && array_key_exists('GLOBALS',$val)) {
       
   768 
       
   769                   $val['GLOBALS'] = '** Recursion (GLOBALS) **';
       
   770               }
       
   771               $return[$key] = $this->_encodeObject($val, 1, $arrayDepth + 1);
       
   772             }
       
   773         } else {
       
   774             return $object;
       
   775         }
       
   776         return $return;
       
   777     }
       
   778 
       
   779     /*
       
   780      * Zend_Wildfire_Plugin_Interface
       
   781      */
       
   782 
       
   783     /**
       
   784      * Get the unique indentifier for this plugin.
       
   785      *
       
   786      * @return string Returns the URI of the plugin.
       
   787      */
       
   788     public function getUri()
       
   789     {
       
   790         return self::PLUGIN_URI;
       
   791     }
       
   792 
       
   793     /**
       
   794      * Flush any buffered data.
       
   795      *
       
   796      * @param string $protocolUri The URI of the protocol that should be flushed to
       
   797      * @return void
       
   798      */
       
   799     public function flushMessages($protocolUri)
       
   800     {
       
   801         if (!$this->_messages || $protocolUri!=self::PROTOCOL_URI) {
       
   802             return;
       
   803         }
       
   804 
       
   805         foreach( $this->_messages as $message ) {
       
   806             if (!$message->getDestroy()) {
       
   807                 $this->send($message->getMessage(),
       
   808                             $message->getLabel(),
       
   809                             $message->getStyle(),
       
   810                             $message->getOptions());
       
   811             }
       
   812         }
       
   813 
       
   814         $this->_messages = array();
       
   815     }
       
   816 }