import v1.0.0-RC4 | 2009-05-20
This commit is contained in:
289
libs/Zend/Wildfire/Channel/HttpHeaders.php
Normal file
289
libs/Zend/Wildfire/Channel/HttpHeaders.php
Normal file
@ -0,0 +1,289 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Channel
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Wildfire_Channel_Interface */
|
||||
require_once 'Zend/Wildfire/Channel/Interface.php';
|
||||
|
||||
/** Zend_Wildfire_Exception */
|
||||
require_once 'Zend/Wildfire/Exception.php';
|
||||
|
||||
/** Zend_Controller_Request_Abstract */
|
||||
require_once('Zend/Controller/Request/Abstract.php');
|
||||
|
||||
/** Zend_Controller_Response_Abstract */
|
||||
require_once('Zend/Controller/Response/Abstract.php');
|
||||
|
||||
/** Zend_Controller_Plugin_Abstract */
|
||||
require_once 'Zend/Controller/Plugin/Abstract.php';
|
||||
|
||||
/** Zend_Wildfire_Protocol_JsonStream */
|
||||
require_once 'Zend/Wildfire/Protocol/JsonStream.php';
|
||||
|
||||
/** Zend_Controller_Front **/
|
||||
require_once 'Zend/Controller/Front.php';
|
||||
|
||||
/**
|
||||
* Implements communication via HTTP request and response headers for Wildfire Protocols.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Channel
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Wildfire_Channel_HttpHeaders extends Zend_Controller_Plugin_Abstract implements Zend_Wildfire_Channel_Interface
|
||||
{
|
||||
/**
|
||||
* The string to be used to prefix the headers.
|
||||
* @var string
|
||||
*/
|
||||
protected static $_headerPrefix = 'X-WF-';
|
||||
|
||||
/**
|
||||
* Singleton instance
|
||||
* @var Zend_Wildfire_Channel_HttpHeaders
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* The index of the plugin in the controller dispatch loop plugin stack
|
||||
* @var integer
|
||||
*/
|
||||
protected static $_controllerPluginStackIndex = 999;
|
||||
|
||||
/**
|
||||
* The protocol instances for this channel
|
||||
* @var array
|
||||
*/
|
||||
protected $_protocols = null;
|
||||
|
||||
/**
|
||||
* Initialize singleton instance.
|
||||
*
|
||||
* @param string $class OPTIONAL Subclass of Zend_Wildfire_Channel_HttpHeaders
|
||||
* @return Zend_Wildfire_Channel_HttpHeaders Returns the singleton Zend_Wildfire_Channel_HttpHeaders instance
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
public static function init($class = null)
|
||||
{
|
||||
if (self::$_instance!==null) {
|
||||
throw new Zend_Wildfire_Exception('Singleton instance of Zend_Wildfire_Channel_HttpHeaders already exists!');
|
||||
}
|
||||
if ($class!==null) {
|
||||
if (!is_string($class)) {
|
||||
throw new Zend_Wildfire_Exception('Third argument is not a class string');
|
||||
}
|
||||
Zend_Loader::loadClass($class);
|
||||
self::$_instance = new $class();
|
||||
if (!self::$_instance instanceof Zend_Wildfire_Channel_HttpHeaders) {
|
||||
self::$_instance = null;
|
||||
throw new Zend_Wildfire_Exception('Invalid class to third argument. Must be subclass of Zend_Wildfire_Channel_HttpHeaders.');
|
||||
}
|
||||
} else {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get or create singleton instance
|
||||
*
|
||||
* @param $skipCreate boolean True if an instance should not be created
|
||||
* @return Zend_Wildfire_Channel_HttpHeaders
|
||||
*/
|
||||
public static function getInstance($skipCreate=false)
|
||||
{
|
||||
if (self::$_instance===null && $skipCreate!==true) {
|
||||
return self::init();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the singleton instance
|
||||
*
|
||||
* Primarily used for testing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function destroyInstance()
|
||||
{
|
||||
self::$_instance = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance of a give protocol for this channel
|
||||
*
|
||||
* @param string $uri The URI for the protocol
|
||||
* @return object Returns the protocol instance for the diven URI
|
||||
*/
|
||||
public function getProtocol($uri)
|
||||
{
|
||||
if (!isset($this->_protocols[$uri])) {
|
||||
$this->_protocols[$uri] = $this->_initProtocol($uri);
|
||||
}
|
||||
|
||||
$this->_registerControllerPlugin();
|
||||
|
||||
return $this->_protocols[$uri];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a new protocol
|
||||
*
|
||||
* @param string $uri The URI for the protocol to be initialized
|
||||
* @return object Returns the new initialized protocol instance
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
protected function _initProtocol($uri)
|
||||
{
|
||||
switch ($uri) {
|
||||
case Zend_Wildfire_Protocol_JsonStream::PROTOCOL_URI;
|
||||
return new Zend_Wildfire_Protocol_JsonStream();
|
||||
}
|
||||
throw new Zend_Wildfire_Exception('Tyring to initialize unknown protocol for URI "'.$uri.'".');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Flush all data from all protocols and send all data to response headers.
|
||||
*
|
||||
* @return boolean Returns TRUE if data was flushed
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
if (!$this->_protocols || !$this->isReady()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ( $this->_protocols as $protocol ) {
|
||||
|
||||
$payload = $protocol->getPayload($this);
|
||||
|
||||
if ($payload) {
|
||||
foreach( $payload as $message ) {
|
||||
|
||||
$this->getResponse()->setHeader(self::$_headerPrefix.$message[0],
|
||||
$message[1], true);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the index of the plugin in the controller dispatch loop plugin stack
|
||||
*
|
||||
* @param integer $index The index of the plugin in the stack
|
||||
* @return integer The previous index.
|
||||
*/
|
||||
public static function setControllerPluginStackIndex($index)
|
||||
{
|
||||
$previous = self::$_controllerPluginStackIndex;
|
||||
self::$_controllerPluginStackIndex = $index;
|
||||
return $previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this object as a controller plugin.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _registerControllerPlugin()
|
||||
{
|
||||
$controller = Zend_Controller_Front::getInstance();
|
||||
if (!$controller->hasPlugin(get_class($this))) {
|
||||
$controller->registerPlugin($this, self::$_controllerPluginStackIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Zend_Wildfire_Channel_Interface
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determine if channel is ready.
|
||||
*
|
||||
* @return boolean Returns TRUE if channel is ready.
|
||||
*/
|
||||
public function isReady()
|
||||
{
|
||||
return ($this->getResponse()->canSendHeaders() &&
|
||||
preg_match_all('/\s?FirePHP\/([\.|\d]*)\s?/si',
|
||||
$this->getRequest()->getHeader('User-Agent'),$m));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Zend_Controller_Plugin_Abstract
|
||||
*/
|
||||
|
||||
/**
|
||||
* Flush messages to headers as late as possible but before headers have been sent.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function dispatchLoopShutdown()
|
||||
{
|
||||
$this->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request object
|
||||
*
|
||||
* @return Zend_Controller_Request_Abstract
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
if (!$this->_request) {
|
||||
$controller = Zend_Controller_Front::getInstance();
|
||||
$this->setRequest($controller->getRequest());
|
||||
}
|
||||
if (!$this->_request) {
|
||||
throw new Zend_Wildfire_Exception('Request objects not initialized.');
|
||||
}
|
||||
return $this->_request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the response object
|
||||
*
|
||||
* @return Zend_Controller_Response_Abstract
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
if (!$this->_response) {
|
||||
$response = Zend_Controller_Front::getInstance()->getResponse();
|
||||
if ($response) {
|
||||
$this->setResponse($response);
|
||||
}
|
||||
}
|
||||
if (!$this->_response) {
|
||||
throw new Zend_Wildfire_Exception('Response objects not initialized.');
|
||||
}
|
||||
return $this->_response;
|
||||
}
|
||||
}
|
37
libs/Zend/Wildfire/Channel/Interface.php
Normal file
37
libs/Zend/Wildfire/Channel/Interface.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Wildfire_Channel_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Determine if channel is ready.
|
||||
*
|
||||
* @return boolean Returns TRUE if channel is ready.
|
||||
*/
|
||||
public function isReady();
|
||||
|
||||
}
|
35
libs/Zend/Wildfire/Exception.php
Normal file
35
libs/Zend/Wildfire/Exception.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
|
||||
/** Zend_Exception */
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Wildfire_Exception extends Zend_Exception
|
||||
{}
|
||||
|
610
libs/Zend/Wildfire/Plugin/FirePhp.php
Normal file
610
libs/Zend/Wildfire/Plugin/FirePhp.php
Normal file
@ -0,0 +1,610 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Loader */
|
||||
require_once 'Zend/Loader.php';
|
||||
|
||||
/** Zend_Wildfire_Exception */
|
||||
require_once 'Zend/Wildfire/Exception.php';
|
||||
|
||||
/** Zend_Controller_Request_Abstract */
|
||||
require_once('Zend/Controller/Request/Abstract.php');
|
||||
|
||||
/** Zend_Controller_Response_Abstract */
|
||||
require_once('Zend/Controller/Response/Abstract.php');
|
||||
|
||||
/** Zend_Wildfire_Channel_HttpHeaders */
|
||||
require_once 'Zend/Wildfire/Channel/HttpHeaders.php';
|
||||
|
||||
/** Zend_Wildfire_Protocol_JsonStream */
|
||||
require_once 'Zend/Wildfire/Protocol/JsonStream.php';
|
||||
|
||||
/** Zend_Wildfire_Plugin_Interface */
|
||||
require_once 'Zend/Wildfire/Plugin/Interface.php';
|
||||
|
||||
/**
|
||||
* Primary class for communicating with the FirePHP Firefox Extension.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Wildfire_Plugin_FirePhp implements Zend_Wildfire_Plugin_Interface
|
||||
{
|
||||
/**
|
||||
* Plain log style.
|
||||
*/
|
||||
const LOG = 'LOG';
|
||||
|
||||
/**
|
||||
* Information style.
|
||||
*/
|
||||
const INFO = 'INFO';
|
||||
|
||||
/**
|
||||
* Warning style.
|
||||
*/
|
||||
const WARN = 'WARN';
|
||||
|
||||
/**
|
||||
* Error style that increments Firebug's error counter.
|
||||
*/
|
||||
const ERROR = 'ERROR';
|
||||
|
||||
/**
|
||||
* Trace style showing message and expandable full stack trace.
|
||||
*/
|
||||
const TRACE = 'TRACE';
|
||||
|
||||
/**
|
||||
* Exception style showing message and expandable full stack trace.
|
||||
* Also increments Firebug's error counter.
|
||||
*/
|
||||
const EXCEPTION = 'EXCEPTION';
|
||||
|
||||
/**
|
||||
* Table style showing summary line and expandable table
|
||||
*/
|
||||
const TABLE = 'TABLE';
|
||||
|
||||
/**
|
||||
* Dump variable to Server panel in Firebug Request Inspector
|
||||
*/
|
||||
const DUMP = 'DUMP';
|
||||
|
||||
/**
|
||||
* Start a group in the Firebug Console
|
||||
*/
|
||||
const GROUP_START = 'GROUP_START';
|
||||
|
||||
/**
|
||||
* End a group in the Firebug Console
|
||||
*/
|
||||
const GROUP_END = 'GROUP_END';
|
||||
|
||||
/**
|
||||
* The plugin URI for this plugin
|
||||
*/
|
||||
const PLUGIN_URI = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
|
||||
|
||||
/**
|
||||
* The protocol URI for this plugin
|
||||
*/
|
||||
const PROTOCOL_URI = Zend_Wildfire_Protocol_JsonStream::PROTOCOL_URI;
|
||||
|
||||
/**
|
||||
* The structure URI for the Dump structure
|
||||
*/
|
||||
const STRUCTURE_URI_DUMP = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1';
|
||||
|
||||
/**
|
||||
* The structure URI for the Firebug Console structure
|
||||
*/
|
||||
const STRUCTURE_URI_FIREBUGCONSOLE = 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1';
|
||||
|
||||
/**
|
||||
* Singleton instance
|
||||
* @var Zend_Wildfire_Plugin_FirePhp
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Flag indicating whether FirePHP should send messages to the user-agent.
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_enabled = true;
|
||||
|
||||
/**
|
||||
* The channel via which to send the encoded messages.
|
||||
* @var Zend_Wildfire_Channel_Interface
|
||||
*/
|
||||
protected $_channel = null;
|
||||
|
||||
/**
|
||||
* Messages that are buffered to be sent when protocol flushes
|
||||
* @var array
|
||||
*/
|
||||
protected $_messages = array();
|
||||
|
||||
/**
|
||||
* The maximum depth to traverse objects when encoding
|
||||
* @var int
|
||||
*/
|
||||
protected $_maxObjectDepth = 10;
|
||||
|
||||
/**
|
||||
* The maximum depth to traverse nested arrays when encoding
|
||||
* @var int
|
||||
*/
|
||||
protected $_maxArrayDepth = 20;
|
||||
|
||||
/**
|
||||
* A stack of objects used during encoding to detect recursion
|
||||
* @var array
|
||||
*/
|
||||
protected $_objectStack = array();
|
||||
|
||||
/**
|
||||
* Create singleton instance.
|
||||
*
|
||||
* @param string $class OPTIONAL Subclass of Zend_Wildfire_Plugin_FirePhp
|
||||
* @return Zend_Wildfire_Plugin_FirePhp Returns the singleton Zend_Wildfire_Plugin_FirePhp instance
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
public static function init($class = null)
|
||||
{
|
||||
if (self::$_instance!==null) {
|
||||
throw new Zend_Wildfire_Exception('Singleton instance of Zend_Wildfire_Plugin_FirePhp already exists!');
|
||||
}
|
||||
if ($class!==null) {
|
||||
if (!is_string($class)) {
|
||||
throw new Zend_Wildfire_Exception('Third argument is not a class string');
|
||||
}
|
||||
Zend_Loader::loadClass($class);
|
||||
self::$_instance = new $class();
|
||||
if (!self::$_instance instanceof Zend_Wildfire_Plugin_FirePhp) {
|
||||
self::$_instance = null;
|
||||
throw new Zend_Wildfire_Exception('Invalid class to third argument. Must be subclass of Zend_Wildfire_Plugin_FirePhp.');
|
||||
}
|
||||
} else {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @return void
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$this->_channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
|
||||
$this->_channel->getProtocol(self::PROTOCOL_URI)->registerPlugin($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create singleton instance
|
||||
*
|
||||
* @param $skipCreate boolean True if an instance should not be created
|
||||
* @return Zend_Wildfire_Plugin_FirePhp
|
||||
*/
|
||||
public static function getInstance($skipCreate=false)
|
||||
{
|
||||
if (self::$_instance===null && $skipCreate!==true) {
|
||||
return self::init();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the singleton instance
|
||||
*
|
||||
* Primarily used for testing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function destroyInstance()
|
||||
{
|
||||
self::$_instance = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable sending of messages to user-agent.
|
||||
* If disabled all headers to be sent will be removed.
|
||||
*
|
||||
* @param boolean $enabled Set to TRUE to enable sending of messages.
|
||||
* @return boolean The previous value.
|
||||
*/
|
||||
public function setEnabled($enabled)
|
||||
{
|
||||
$previous = $this->_enabled;
|
||||
$this->_enabled = $enabled;
|
||||
if (!$this->_enabled) {
|
||||
$this->_messages = array();
|
||||
$this->_channel->getProtocol(self::PROTOCOL_URI)->clearMessages($this);
|
||||
}
|
||||
return $previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if logging to user-agent is enabled.
|
||||
*
|
||||
* @return boolean Returns TRUE if logging is enabled.
|
||||
*/
|
||||
public function getEnabled()
|
||||
{
|
||||
return $this->_enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a group in the Firebug Console
|
||||
*
|
||||
* @param string $title The title of the group
|
||||
* @return TRUE if the group instruction was added to the response headers or buffered.
|
||||
*/
|
||||
public static function group($title)
|
||||
{
|
||||
return self::send(null, $title, self::GROUP_START);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a group in the Firebug Console
|
||||
*
|
||||
* @return TRUE if the group instruction was added to the response headers or buffered.
|
||||
*/
|
||||
public static function groupEnd()
|
||||
{
|
||||
return self::send(null, null, self::GROUP_END);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs variables to the Firebug Console
|
||||
* via HTTP response headers and the FirePHP Firefox Extension.
|
||||
*
|
||||
* @param mixed $var The variable to log.
|
||||
* @param string $label OPTIONAL Label to prepend to the log event.
|
||||
* @param string $style OPTIONAL Style of the log event.
|
||||
* @return boolean Returns TRUE if the variable was added to the response headers or buffered.
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
public static function send($var, $label=null, $style=null)
|
||||
{
|
||||
if (self::$_instance===null) {
|
||||
self::getInstance();
|
||||
}
|
||||
|
||||
if (!self::$_instance->_enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($var instanceof Zend_Wildfire_Plugin_FirePhp_Message) {
|
||||
|
||||
if ($var->getBuffered()) {
|
||||
if (!in_array($var, self::$_instance->_messages)) {
|
||||
self::$_instance->_messages[] = $var;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($var->getDestroy()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$style = $var->getStyle();
|
||||
$label = $var->getLabel();
|
||||
$var = $var->getMessage();
|
||||
}
|
||||
|
||||
if (!self::$_instance->_channel->isReady()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($var instanceof Exception) {
|
||||
|
||||
$var = array('Class'=>get_class($var),
|
||||
'Message'=>$var->getMessage(),
|
||||
'File'=>$var->getFile(),
|
||||
'Line'=>$var->getLine(),
|
||||
'Type'=>'throw',
|
||||
'Trace'=>$var->getTrace());
|
||||
|
||||
$style = self::EXCEPTION;
|
||||
|
||||
} else
|
||||
if ($style==self::TRACE) {
|
||||
|
||||
$trace = debug_backtrace();
|
||||
if(!$trace) return false;
|
||||
|
||||
for ( $i=0 ; $i<sizeof($trace) ; $i++ ) {
|
||||
if (isset($trace[$i]['class']) &&
|
||||
substr($trace[$i]['class'],0,8)!='Zend_Log' &&
|
||||
substr($trace[$i]['class'],0,13)!='Zend_Wildfire') {
|
||||
|
||||
$i--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($i==sizeof($trace)) {
|
||||
$i = 0;
|
||||
}
|
||||
|
||||
$var = array('Class'=>$trace[$i]['class'],
|
||||
'Type'=>$trace[$i]['type'],
|
||||
'Function'=>$trace[$i]['function'],
|
||||
'Message'=>(isset($trace[$i]['args'][0]))?$trace[$i]['args'][0]:'',
|
||||
'File'=>(isset($trace[$i]['file']))?$trace[$i]['file']:'',
|
||||
'Line'=>(isset($trace[$i]['line']))?$trace[$i]['line']:'',
|
||||
'Args'=>$trace[$i]['args'],
|
||||
'Trace'=>array_splice($trace,$i+1));
|
||||
} else {
|
||||
if ($style===null) {
|
||||
$style = self::LOG;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($style) {
|
||||
case self::LOG:
|
||||
case self::INFO:
|
||||
case self::WARN:
|
||||
case self::ERROR:
|
||||
case self::EXCEPTION:
|
||||
case self::TRACE:
|
||||
case self::TABLE:
|
||||
case self::DUMP:
|
||||
case self::GROUP_START:
|
||||
case self::GROUP_END:
|
||||
break;
|
||||
default:
|
||||
throw new Zend_Wildfire_Exception('Log style "'.$style.'" not recognized!');
|
||||
break;
|
||||
}
|
||||
|
||||
if ($style == self::DUMP) {
|
||||
|
||||
return self::$_instance->_recordMessage(self::STRUCTURE_URI_DUMP,
|
||||
array('key'=>$label,
|
||||
'data'=>$var));
|
||||
|
||||
} else {
|
||||
|
||||
$meta = array('Type'=>$style);
|
||||
|
||||
if ($label!=null) {
|
||||
$meta['Label'] = $label;
|
||||
}
|
||||
|
||||
return self::$_instance->_recordMessage(self::STRUCTURE_URI_FIREBUGCONSOLE,
|
||||
array('data'=>$var,
|
||||
'meta'=>$meta));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Record a message with the given data in the given structure
|
||||
*
|
||||
* @param string $structure The structure to be used for the data
|
||||
* @param array $data The data to be recorded
|
||||
* @return boolean Returns TRUE if message was recorded
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
protected function _recordMessage($structure, $data)
|
||||
{
|
||||
switch($structure) {
|
||||
|
||||
case self::STRUCTURE_URI_DUMP:
|
||||
|
||||
if (!isset($data['key'])) {
|
||||
throw new Zend_Wildfire_Exception('You must supply a key.');
|
||||
}
|
||||
if (!array_key_exists('data',$data)) {
|
||||
throw new Zend_Wildfire_Exception('You must supply data.');
|
||||
}
|
||||
|
||||
return $this->_channel->getProtocol(self::PROTOCOL_URI)->
|
||||
recordMessage($this,
|
||||
$structure,
|
||||
array($data['key']=>$this->_encodeObject($data['data'])));
|
||||
|
||||
case self::STRUCTURE_URI_FIREBUGCONSOLE:
|
||||
|
||||
if (!isset($data['meta']) ||
|
||||
!is_array($data['meta']) ||
|
||||
!array_key_exists('Type',$data['meta'])) {
|
||||
|
||||
throw new Zend_Wildfire_Exception('You must supply a "Type" in the meta information.');
|
||||
}
|
||||
if (!array_key_exists('data',$data)) {
|
||||
throw new Zend_Wildfire_Exception('You must supply data.');
|
||||
}
|
||||
|
||||
return $this->_channel->getProtocol(self::PROTOCOL_URI)->
|
||||
recordMessage($this,
|
||||
$structure,
|
||||
array($data['meta'],
|
||||
$this->_encodeObject($data['data'])));
|
||||
|
||||
default:
|
||||
throw new Zend_Wildfire_Exception('Structure of name "'.$structure.'" is not recognized.');
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode an object by generating an array containing all object members.
|
||||
*
|
||||
* All private and protected members are included. Some meta info about
|
||||
* the object class is added.
|
||||
*
|
||||
* @param mixed $object The object/array/value to be encoded
|
||||
* @return array The encoded object
|
||||
*/
|
||||
protected function _encodeObject($object, $depth = 1)
|
||||
{
|
||||
$return = array();
|
||||
|
||||
if (is_resource($object)) {
|
||||
|
||||
return '** '.(string)$object.' **';
|
||||
|
||||
} else
|
||||
if (is_object($object)) {
|
||||
|
||||
if ($depth > $this->_maxObjectDepth) {
|
||||
return '** Max Depth **';
|
||||
}
|
||||
|
||||
foreach ($this->_objectStack as $refVal) {
|
||||
if ($refVal === $object) {
|
||||
return '** Recursion ('.get_class($object).') **';
|
||||
}
|
||||
}
|
||||
array_push($this->_objectStack, $object);
|
||||
|
||||
$return['__className'] = $class = get_class($object);
|
||||
|
||||
$reflectionClass = new ReflectionClass($class);
|
||||
$properties = array();
|
||||
foreach ( $reflectionClass->getProperties() as $property) {
|
||||
$properties[$property->getName()] = $property;
|
||||
}
|
||||
|
||||
$members = (array)$object;
|
||||
|
||||
foreach ($properties as $raw_name => $property) {
|
||||
|
||||
$name = $raw_name;
|
||||
if ($property->isStatic()) {
|
||||
$name = 'static:'.$name;
|
||||
}
|
||||
if ($property->isPublic()) {
|
||||
$name = 'public:'.$name;
|
||||
} else
|
||||
if ($property->isPrivate()) {
|
||||
$name = 'private:'.$name;
|
||||
$raw_name = "\0".$class."\0".$raw_name;
|
||||
} else
|
||||
if ($property->isProtected()) {
|
||||
$name = 'protected:'.$name;
|
||||
$raw_name = "\0".'*'."\0".$raw_name;
|
||||
}
|
||||
|
||||
if (array_key_exists($raw_name,$members)
|
||||
&& !$property->isStatic()) {
|
||||
|
||||
$return[$name] = $this->_encodeObject($members[$raw_name], $depth + 1);
|
||||
|
||||
} else {
|
||||
if (method_exists($property,'setAccessible')) {
|
||||
$property->setAccessible(true);
|
||||
$return[$name] = $this->_encodeObject($property->getValue($object), $depth + 1);
|
||||
} else
|
||||
if ($property->isPublic()) {
|
||||
$return[$name] = $this->_encodeObject($property->getValue($object), $depth + 1);
|
||||
} else {
|
||||
$return[$name] = '** Need PHP 5.3 to get value **';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Include all members that are not defined in the class
|
||||
// but exist in the object
|
||||
foreach($members as $name => $value) {
|
||||
if ($name{0} == "\0") {
|
||||
$parts = explode("\0", $name);
|
||||
$name = $parts[2];
|
||||
}
|
||||
if (!isset($properties[$name])) {
|
||||
$name = 'undeclared:'.$name;
|
||||
$return[$name] = $this->_encodeObject($value, $depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
array_pop($this->_objectStack);
|
||||
|
||||
} elseif (is_array($object)) {
|
||||
|
||||
if ($depth > $this->_maxArrayDepth) {
|
||||
return '** Max Depth **';
|
||||
}
|
||||
|
||||
foreach ($object as $key => $val) {
|
||||
|
||||
// Encoding the $GLOBALS PHP array causes an infinite loop
|
||||
// if the recursion is not reset here as it contains
|
||||
// a reference to itself. This is the only way I have come up
|
||||
// with to stop infinite recursion in this case.
|
||||
if ($key=='GLOBALS'
|
||||
&& is_array($val)
|
||||
&& array_key_exists('GLOBALS',$val)) {
|
||||
|
||||
$val['GLOBALS'] = '** Recursion (GLOBALS) **';
|
||||
}
|
||||
$return[$key] = $this->_encodeObject($val, $depth + 1);
|
||||
}
|
||||
} else {
|
||||
return $object;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Zend_Wildfire_Plugin_Interface
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the unique indentifier for this plugin.
|
||||
*
|
||||
* @return string Returns the URI of the plugin.
|
||||
*/
|
||||
public function getUri()
|
||||
{
|
||||
return self::PLUGIN_URI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush any buffered data.
|
||||
*
|
||||
* @param string $protocolUri The URI of the protocol that should be flushed to
|
||||
* @return void
|
||||
*/
|
||||
public function flushMessages($protocolUri)
|
||||
{
|
||||
if(!$this->_messages || $protocolUri!=self::PROTOCOL_URI) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach( $this->_messages as $message ) {
|
||||
if (!$message->getDestroy()) {
|
||||
$this->send($message->getMessage(), $message->getLabel(), $message->getStyle());
|
||||
}
|
||||
}
|
||||
|
||||
$this->_messages = array();
|
||||
}
|
||||
}
|
195
libs/Zend/Wildfire/Plugin/FirePhp/Message.php
Normal file
195
libs/Zend/Wildfire/Plugin/FirePhp/Message.php
Normal file
@ -0,0 +1,195 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* A message envelope that can be passed to Zend_Wildfire_Plugin_FirePhp to be
|
||||
* logged to Firebug instead of a variable.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Wildfire_Plugin_FirePhp_Message
|
||||
{
|
||||
/**
|
||||
* The style of the message
|
||||
* @var string
|
||||
*/
|
||||
protected $_style = null;
|
||||
|
||||
/**
|
||||
* The label of the message
|
||||
* @var string
|
||||
*/
|
||||
protected $_label = null;
|
||||
|
||||
/**
|
||||
* The message value
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_message = null;
|
||||
|
||||
/**
|
||||
* Flag indicating if message buffering is enabled
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_buffered = false;
|
||||
|
||||
/**
|
||||
* Flag indicating if message should be destroyed and not delivered
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_destroy = false;
|
||||
|
||||
/**
|
||||
* Random unique ID used to identify message in comparison operations
|
||||
* @var string
|
||||
*/
|
||||
protected $_ruid = false;
|
||||
|
||||
/**
|
||||
* Creates a new message with the given style and message
|
||||
*
|
||||
* @param string $style Style of the message.
|
||||
* @param mixed $message The message
|
||||
* @return void
|
||||
*/
|
||||
function __construct($style, $message)
|
||||
{
|
||||
$this->_style = $style;
|
||||
$this->_message = $message;
|
||||
$this->_ruid = md5(microtime().mt_rand());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the label of the message
|
||||
*
|
||||
* @param string $label The label to be set
|
||||
* @return void
|
||||
*/
|
||||
public function setLabel($label)
|
||||
{
|
||||
$this->_label = $label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the label of the message
|
||||
*
|
||||
* @return string The label of the message
|
||||
*/
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->_label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable message buffering
|
||||
*
|
||||
* If a message is buffered it can be updated for the duration of the
|
||||
* request and is only flushed at the end of the request.
|
||||
*
|
||||
* @param boolean $buffered TRUE to enable buffering FALSE otherwise
|
||||
* @return boolean Returns previous buffering value
|
||||
*/
|
||||
public function setBuffered($buffered)
|
||||
{
|
||||
$previous = $this->_buffered;
|
||||
$this->_buffered = $buffered;
|
||||
return $previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if buffering is enabled or disabled
|
||||
*
|
||||
* @return boolean Returns TRUE if buffering is enabled, FALSE otherwise.
|
||||
*/
|
||||
public function getBuffered()
|
||||
{
|
||||
return $this->_buffered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the message to prevent delivery
|
||||
*
|
||||
* @param boolean $destroy TRUE to destroy FALSE otherwise
|
||||
* @return boolean Returns previous destroy value
|
||||
*/
|
||||
public function setDestroy($destroy)
|
||||
{
|
||||
$previous = $this->_destroy;
|
||||
$this->_destroy = $destroy;
|
||||
return $previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if message should be destroyed
|
||||
*
|
||||
* @return boolean Returns TRUE if message should be destroyed, FALSE otherwise.
|
||||
*/
|
||||
public function getDestroy()
|
||||
{
|
||||
return $this->_destroy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the style of the message
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setStyle($style)
|
||||
{
|
||||
$this->_style = $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the style of the message
|
||||
*
|
||||
* @return string The style of the message
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
return $this->_style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the actual message to be sent in its final format.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setMessage($message)
|
||||
{
|
||||
$this->_message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actual message to be sent in its final format.
|
||||
*
|
||||
* @return mixed Returns the message to be sent.
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->_message;
|
||||
}
|
||||
}
|
||||
|
98
libs/Zend/Wildfire/Plugin/FirePhp/TableMessage.php
Normal file
98
libs/Zend/Wildfire/Plugin/FirePhp/TableMessage.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Wildfire_Plugin_FirePhp */
|
||||
require_once 'Zend/Wildfire/Plugin/FirePhp.php';
|
||||
|
||||
/** Zend_Wildfire_Plugin_FirePhp_Message */
|
||||
require_once 'Zend/Wildfire/Plugin/FirePhp/Message.php';
|
||||
|
||||
/**
|
||||
* A message envelope that can be updated for the duration of the requet before
|
||||
* it gets flushed at the end of the request.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Wildfire_Plugin_FirePhp_TableMessage extends Zend_Wildfire_Plugin_FirePhp_Message
|
||||
{
|
||||
/**
|
||||
* The header of the table containing all columns
|
||||
* @var array
|
||||
*/
|
||||
protected $_header = null;
|
||||
|
||||
/**
|
||||
* The rows of the table
|
||||
* $var array
|
||||
*/
|
||||
protected $_rows = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $label The label of the table
|
||||
*/
|
||||
function __construct($label)
|
||||
{
|
||||
parent::__construct(Zend_Wildfire_Plugin_FirePhp::TABLE, null);
|
||||
$this->setLabel($label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the table header
|
||||
*
|
||||
* @param array $header The header columns
|
||||
* @return void
|
||||
*/
|
||||
public function setHeader($header)
|
||||
{
|
||||
$this->_header = $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a row to the end of the table.
|
||||
*
|
||||
* @param array $row An array of column values representing a row.
|
||||
* @return void
|
||||
*/
|
||||
public function addRow($row)
|
||||
{
|
||||
$this->_rows[] = $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actual message to be sent in its final format.
|
||||
*
|
||||
* @return mixed Returns the message to be sent.
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
$table = $this->_rows;
|
||||
array_unshift($table,$this->_header);
|
||||
return $table;
|
||||
}
|
||||
|
||||
}
|
||||
|
47
libs/Zend/Wildfire/Plugin/Interface.php
Normal file
47
libs/Zend/Wildfire/Plugin/Interface.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Plugin
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Wildfire_Plugin_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* Flush any buffered data.
|
||||
*
|
||||
* @param string $protocolUri The URI of the protocol that should be flushed to
|
||||
* @return void
|
||||
*/
|
||||
public function flushMessages($protocolUri);
|
||||
|
||||
/**
|
||||
* Get the unique indentifier for this plugin.
|
||||
*
|
||||
* @return string Returns the URI of the plugin.
|
||||
*/
|
||||
public function getUri();
|
||||
|
||||
}
|
235
libs/Zend/Wildfire/Protocol/JsonStream.php
Normal file
235
libs/Zend/Wildfire/Protocol/JsonStream.php
Normal file
@ -0,0 +1,235 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Protocol
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Wildfire_Exception */
|
||||
require_once 'Zend/Wildfire/Exception.php';
|
||||
|
||||
/** Zend_Wildfire_Plugin_Interface */
|
||||
require_once 'Zend/Wildfire/Plugin/Interface.php';
|
||||
|
||||
/** Zend_Wildfire_Channel_Interface */
|
||||
require_once 'Zend/Wildfire/Channel/Interface.php';
|
||||
|
||||
/** Zend_Json */
|
||||
require_once 'Zend/Json.php';
|
||||
|
||||
/**
|
||||
* Encodes messages into the Wildfire JSON Stream Communication Protocol.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Wildfire
|
||||
* @subpackage Protocol
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Wildfire_Protocol_JsonStream
|
||||
{
|
||||
/**
|
||||
* The protocol URI for this protocol
|
||||
*/
|
||||
const PROTOCOL_URI = 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2';
|
||||
|
||||
/**
|
||||
* All messages to be sent.
|
||||
* @var array
|
||||
*/
|
||||
protected $_messages = array();
|
||||
|
||||
/**
|
||||
* Plugins that are using this protocol
|
||||
* @var array
|
||||
*/
|
||||
protected $_plugins = array();
|
||||
|
||||
/**
|
||||
* Register a plugin that uses this protocol
|
||||
*
|
||||
* @param Zend_Wildfire_Plugin_Interface $plugin The plugin to be registered
|
||||
* @return boolean Returns TRUE if plugin was registered, false if it was already registered
|
||||
*/
|
||||
public function registerPlugin(Zend_Wildfire_Plugin_Interface $plugin)
|
||||
{
|
||||
if (in_array($plugin,$this->_plugins)) {
|
||||
return false;
|
||||
}
|
||||
$this->_plugins[] = $plugin;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record a message with the given data in the given structure
|
||||
*
|
||||
* @param Zend_Wildfire_Plugin_Interface $plugin The plugin recording the message
|
||||
* @param string $structure The structure to be used for the data
|
||||
* @param array $data The data to be recorded
|
||||
* @return boolean Returns TRUE if message was recorded
|
||||
*/
|
||||
public function recordMessage(Zend_Wildfire_Plugin_Interface $plugin, $structure, $data)
|
||||
{
|
||||
if(!isset($this->_messages[$structure])) {
|
||||
$this->_messages[$structure] = array();
|
||||
}
|
||||
|
||||
$uri = $plugin->getUri();
|
||||
|
||||
if(!isset($this->_messages[$structure][$uri])) {
|
||||
$this->_messages[$structure][$uri] = array();
|
||||
}
|
||||
|
||||
$this->_messages[$structure][$uri][] = $this->_encode($data);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all qued messages
|
||||
*
|
||||
* @param Zend_Wildfire_Plugin_Interface $plugin The plugin for which to clear messages
|
||||
* @return boolean Returns TRUE if messages were present
|
||||
*/
|
||||
public function clearMessages(Zend_Wildfire_Plugin_Interface $plugin)
|
||||
{
|
||||
$uri = $plugin->getUri();
|
||||
|
||||
$present = false;
|
||||
foreach ($this->_messages as $structure => $messages) {
|
||||
|
||||
if(!isset($this->_messages[$structure][$uri])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$present = true;
|
||||
|
||||
unset($this->_messages[$structure][$uri]);
|
||||
|
||||
if (!$this->_messages[$structure]) {
|
||||
unset($this->_messages[$structure]);
|
||||
}
|
||||
}
|
||||
return $present;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all qued messages
|
||||
*
|
||||
* @return mixed Returns qued messages or FALSE if no messages are qued
|
||||
*/
|
||||
public function getMessages()
|
||||
{
|
||||
if (!$this->_messages) {
|
||||
return false;
|
||||
}
|
||||
return $this->_messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the JSON encoding scheme for the value specified
|
||||
*
|
||||
* @param mixed $value The value to be encoded
|
||||
* @return string The encoded value
|
||||
*/
|
||||
protected function _encode($value)
|
||||
{
|
||||
return Zend_Json::encode($value, true, array('silenceCyclicalExceptions'=>true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all formatted data ready to be sent by the channel.
|
||||
*
|
||||
* @param Zend_Wildfire_Channel_Interface $channel The instance of the channel that will be transmitting the data
|
||||
* @return mixed Returns the data to be sent by the channel.
|
||||
* @throws Zend_Wildfire_Exception
|
||||
*/
|
||||
public function getPayload(Zend_Wildfire_Channel_Interface $channel)
|
||||
{
|
||||
if (!$channel instanceof Zend_Wildfire_Channel_HttpHeaders) {
|
||||
throw new Zend_Wildfire_Exception('The '.get_class($channel).' channel is not supported by the '.get_class($this).' protocol.');
|
||||
}
|
||||
|
||||
if ($this->_plugins) {
|
||||
foreach ($this->_plugins as $plugin) {
|
||||
$plugin->flushMessages(self::PROTOCOL_URI);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->_messages) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$protocol_index = 1;
|
||||
$structure_index = 1;
|
||||
$plugin_index = 1;
|
||||
$message_index = 1;
|
||||
|
||||
$payload = array();
|
||||
|
||||
$payload[] = array('Protocol-'.$protocol_index, self::PROTOCOL_URI);
|
||||
|
||||
foreach ($this->_messages as $structure_uri => $plugin_messages ) {
|
||||
|
||||
$payload[] = array($protocol_index.'-Structure-'.$structure_index, $structure_uri);
|
||||
|
||||
foreach ($plugin_messages as $plugin_uri => $messages ) {
|
||||
|
||||
$payload[] = array($protocol_index.'-Plugin-'.$plugin_index, $plugin_uri);
|
||||
|
||||
foreach ($messages as $message) {
|
||||
|
||||
$parts = explode("\n",chunk_split($message, 5000, "\n"));
|
||||
|
||||
for ($i=0 ; $i<count($parts) ; $i++) {
|
||||
|
||||
$part = $parts[$i];
|
||||
if ($part) {
|
||||
|
||||
$msg = '';
|
||||
|
||||
if (count($parts)>2) {
|
||||
$msg = (($i==0)?strlen($message):'')
|
||||
. '|' . $part . '|'
|
||||
. (($i<count($parts)-2)?'\\':'');
|
||||
} else {
|
||||
$msg = strlen($part) . '|' . $part . '|';
|
||||
}
|
||||
|
||||
$payload[] = array($protocol_index . '-'
|
||||
. $structure_index . '-'
|
||||
. $plugin_index . '-'
|
||||
. $message_index,
|
||||
$msg);
|
||||
|
||||
$message_index++;
|
||||
|
||||
if ($message_index > 99999) {
|
||||
throw new Zend_Wildfire_Exception('Maximum number (99,999) of messages reached!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$plugin_index++;
|
||||
}
|
||||
$structure_index++;
|
||||
}
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user