Implements Config and Logging plugins, tidies Main

Added Database and Flatfile plugin engines for both Config and Log classes.
Rewrote Main to make use of the new plugins.
Updated config file definitions to make use of plugins.
This commit is contained in:
2010-10-10 17:11:44 +01:00
parent ddd2e62c13
commit 676fef1deb
15 changed files with 408 additions and 112 deletions

View File

@@ -0,0 +1,27 @@
<?php
interface Sihnon_Log_IPlugin extends Sihnon_IPlugin {
/**
* Returns a new instance of the Plugin class
*
* @param array(string=>mixed) $options Configuration options for the Plugin object
*/
public static function create($options);
/**
* Records a new entry in the storage backend used by this logging plugin
*
* @param string $level Severity of the log entry
* @param int $ctime Time the log entry was created
* @param int $pid ID of the process that created the log entry
* @param string $hostname Hostname of the system that created the log entry
* @param string $progname Name of the application that created the log entry
* @param int $line Line number of the code that created the log entry
* @param string $message Message to be logged
*/
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message);
}
?>

View File

@@ -0,0 +1,49 @@
<?php
class Sihnon_Log_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
/**
* Name of this plugin
* @var string
*/
const PLUGIN_NAME = "Database";
private $database;
private $table;
protected function __construct($options) {
$this->database = $options['database'];
$this->table = Sihnon_Main::instance()->config()->get('logging.database.table');
}
public static function create($options) {
return new self($options);
}
/**
* Records a new entry in the storage backend used by this logging plugin
*
* @param string $level Severity of the log entry
* @param int $ctime Time the log entry was created
* @param int $pid ID of the process that created the log entry
* @param string $hostname Hostname of the system that created the log entry
* @param string $progname Name of the application that created the log entry
* @param int $line Line number of the code that created the log entry
* @param string $message Message to be logged
*/
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message) {
$this->database->insert("INSERT INTO {$this->table} (level,ctime,pid,hostname,progname,line,message) VALUES(:level, :ctime, :pid, :hostname, :progname, :line, :message)",
array(
array('name' => 'level', 'value' => $level, 'type' => PDO::PARAM_STR),
array('name' => 'ctime', 'value' => $ctime, 'type' => PDO::PARAM_INT),
array('name' => 'pid', 'value' => $pid, 'type' => PDO::PARAM_INT),
array('name' => 'hostname', 'value' => $hostname, 'type' => PDO::PARAM_STR),
array('name' => 'progname', 'value' => $progname, 'type' => PDO::PARAM_STR),
array('name' => 'line', 'value' => $line, 'type' => PDO::PARAM_INT),
array('name' => 'message', 'value' => $message, 'type' => PDO::PARAM_STR)
)
);
}
}
?>

View File

@@ -0,0 +1,44 @@
<?php
class Sihnon_Log_Plugin_FlatFile extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
/**
* Name of this plugin
* @var string
*/
const PLUGIN_NAME = "FlatFile";
protected $filename;
protected $fp;
protected function __construct($options) {
$this->filename = Sihnon_Main::instance()->config()->get('logging.flatfile.filename');
$this->fp = fopen($this->filename, 'a');
}
public function __destruct() {
fclose($this->fp);
}
public static function create($options) {
return new self($options);
}
/**
* Records a new entry in the storage backend used by this logging plugin
*
* @param string $level Severity of the log entry
* @param int $ctime Time the log entry was created
* @param int $pid ID of the process that created the log entry
* @param string $hostname Hostname of the system that created the log entry
* @param string $progname Name of the application that created the log entry
* @param int $line Line number of the code that created the log entry
* @param string $message Message to be logged
*/
public function log($level, $ctime, $pid, $hostname, $progname, $line, $message) {
$log_entry = implode(',', array($level, $ctime, $pid, $hostname, $progname, $line, $message)) . "\n";
fwrite($this->fp, $log_entry, strlen($log_entry));
}
}
?>

View File

@@ -0,0 +1,27 @@
<?php
class Sihnon_Log_PluginFactory extends Sihnon_PluginFactory {
const PLUGIN_DIR = 'Sihnon/Log/Plugin/';
const PLUGIN_PREFIX = 'Sihnon_Log_Plugin_';
const PLUGIN_INTERFACE = 'Sihnon_Log_IPlugin';
public static function init() {
}
public static function create($plugin, $options) {
self::ensureScanned();
if (! self::isValidPlugin($plugin)) {
throw new Sihnon_Exception_InvalidPluginName($plugin);
}
$classname = self::classname($plugin);
return call_user_func(array($classname, 'create'), $options);
}
}
?>