<?PHP
/**
*
* $Id: logParser.class.php 74 2005-12-31 03:38:16Z khaless $
* $Revision: 74 $
* $Author: khaless $
* $Date: 2005-12-31 14:38:16 +1100 (Sat, 31 Dec 2005) $
*
* Copyright (c) 2005 Mathew Rodley <hide@address.com>
*
* Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class handler
{
/**
* @var integer
* @access public
*/
public $id;
/**
* @var string
* @access public
*/
public $regularExpression;
/**
* @var object
* @access public
*/
public $object;
/**
* @var string
* @access public
*/
public $method;
}
class logParser
{
function registerHandler($regularExpression, &$object, $method)
{
$newHandler = new handler;
$newHandler->id = $this->_handlerId++;
$newHandler->regularExpression = $regularExpression;
$newHandler->object = &$object;
$newHandler->method = $method;
$this->_handlers[] = &$newHandler;
}
function useHandler($rawMessage)
{
$handlerCount = count($this->_handlers);
for ($i = 0; $i < $handlerCount; $i++)
{
// we are either going to have a full preg with // or just the regular expression,
// figure this out.
if (substr($this->_handlers[$i]->regularExpression, 0, 1) == '/') {
$regex = $this->_handlers[$i]->regularExpression;
} else {
$regex = '/'.$this->_handlers[$i]->regularExpression.'/';
}
if((preg_match($regex, $rawMessage, $matches) == 1))
{
// ok, check if our method exists..
if (@method_exists($this->_handlers[$i]->object, $this->_handlers[$i]->method))
{
$this->_handlers[$i]->object->{$this->_handlers[$i]->method}($matches);
}
else die('Method '.$this->_handlers[$i]->method.' does not exist!');
}
}
}
}
?>