2011-10-12 06:54:18 +00:00

234 lines
6.0 KiB
PHP

<?php
/**
* @see Zend_Controller_Exception
*/
require_once 'Zend/Controller/Exception.php';
/**
* @category WsDebug
* @package WsDebug_Controller
* @subpackage Plugins
*/
class WsDebug_Controller_Plugin_Debug extends Zend_Controller_Plugin_Abstract
{
/**
* Contains registered plugins
*
* @var array
*/
protected $_plugins = array();
/**
* Contains options to change Debug Bar behavior
*/
protected $_options = array(
'plugins' => array(
'Exception' => null,
),
);
/**
* Standard plugins
*
* @var array
*/
public static $standardPlugins = array(
'Exception',
);
/**
* Creates a new instance of the Debug Bar
*
* @param array|Zend_Config $options
* @throws Zend_Controller_Exception
* @return void
*/
protected $_closingBracket = null;
public function __construct($options = null)
{
if (isset($options)) {
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
/*
* Verify that adapter parameters are in an array.
*/
if (!is_array($options)) {
throw new Zend_Exception('Parameters must be in an array or a Zend_Config object');
}
$this->setOptions($options);
}
/**
* Creating the log tab
*/
$logger = new WsDebug_Controller_Plugin_Debug_Plugin_Log();
$this->registerPlugin($logger);
$logger->mark('Startup - WsDebug construct()', true);
/**
* Loading already defined plugins
*/
$this->_loadPlugins();
}
/**
* Get the ZFDebug logger
*
* @return Zend_Log
*/
public function getLogger()
{
return $this->getPlugin('Log')->logger();
}
/**
* Sets options of the Debug Bar
*
* @param array $options
* @return ZFDebug_Controller_Plugin_Debug
*/
public function setOptions(array $options = array())
{
if (isset($options['plugins'])) {
$this->_options['plugins'] = $options['plugins'];
}
return $this;
}
/**
* Register a new plugin in the Debug Bar
*
* @param WsDebug_Controller_Plugin_Debug_Plugin_Interface
* @return WsDebug_Controller_Plugin_Debug
*/
public function registerPlugin(WsDebug_Controller_Plugin_Debug_Plugin_Interface $plugin)
{
$this->_plugins[$plugin->getIdentifier()] = $plugin;
return $this;
}
/**
* Unregister a plugin in the Debug Bar
*
* @param string $plugin
* @return WsDebug_Controller_Plugin_Debug
*/
public function unregisterPlugin($plugin)
{
if (false !== strpos($plugin, '_')) {
foreach ($this->_plugins as $key => $_plugin) {
if ($plugin == get_class($_plugin)) {
unset($this->_plugins[$key]);
}
}
} else {
$plugin = strtolower($plugin);
if (isset($this->_plugins[$plugin])) {
unset($this->_plugins[$plugin]);
}
}
return $this;
}
/**
* Get a registered plugin in the Debug Bar
*
* @param string $identifier
* @return WsDebug_Controller_Plugin_Debug_Plugin_Interface
*/
public function getPlugin($identifier)
{
$identifier = strtolower($identifier);
if (isset($this->_plugins[$identifier])) {
return $this->_plugins[$identifier];
}
return false;
}
/**
* Defined by Zend_Controller_Plugin_Abstract
*/
public function dispatchLoopShutdown()
{
$txt = '';
/**
* Creating content for all registered plugins
*/
foreach ($this->_plugins as $plugin) {
$data = $plugin->getData();
if ($data == '') {
continue;
}
$txt .= 'Debug - ' . ucfirst($plugin->getIdentifier())." : \n";
$txt .= $data;
}
$this->_output($txt);
}
### INTERNAL METHODS BELOW ###
/**
* Load plugins set in config option
*
* @return void;
*/
protected function _loadPlugins()
{
foreach ($this->_options['plugins'] as $plugin => $options) {
if (is_numeric($plugin)) {
# Plugin passed as array value instead of key
$plugin = $options;
$options = array();
}
// Register an instance
if (is_object($plugin) && in_array('WsDebug_Controller_Plugin_Debug_Plugin_Interface', class_implements($plugin))) {
$this->registerPlugin($plugin);
continue;
}
if (!is_string($plugin)) {
throw new Exception("Invalid plugin name", 1);
}
$plugin = ucfirst($plugin);
// Register a classname
if (in_array($plugin, WsDebug_Controller_Plugin_Debug::$standardPlugins)) {
// standard plugin
$pluginClass = 'WsDebug_Controller_Plugin_Debug_Plugin_' . $plugin;
} else {
// we use a custom plugin
if (!preg_match('~^[\w]+$~D', $plugin)) {
throw new Zend_Exception("WsDebug: Invalid plugin name [$plugin]");
}
$pluginClass = $plugin;
}
require_once str_replace('_', DIRECTORY_SEPARATOR, $pluginClass) . '.php';
$object = new $pluginClass($options);
$this->registerPlugin($object);
}
}
/**
* Output text for logging
*
* @param string $txt
* @return void
*/
protected function _output($txt)
{
//file_put_contents(APPLICATION_PATH . '/../test.log', $txt, FILE_APPEND);
//Is txt should be send by mail
//Is txt should be write to file
}
}