Merge branch 'feature-subclass'

This commit is contained in:
2010-10-10 18:40:05 +01:00
25 changed files with 145 additions and 90 deletions

View File

@@ -1,24 +0,0 @@
<?php
class Sihnon_Exception extends Exception {};
class Sihnon_Exception_NotImplemented extends Sihnon_Exception {};
class Sihnon_Exception_MissingDefinition extends Sihnon_Exception {};
class Sihnon_Exception_DatabaseException extends Sihnon_Exception {};
class Sihnon_Exception_DatabaseConfigMissing extends Sihnon_Exception_DatabaseException {};
class Sihnon_Exception_DatabaseConnectFailed extends Sihnon_Exception_DatabaseException {};
class Sihnon_Exception_NoDatabaseConnection extends Sihnon_Exception_DatabaseException {};
class Sihnon_Exception_DatabaseQueryFailed extends Sihnon_Exception_DatabaseException {};
class Sihnon_Exception_ResultCountMismatch extends Sihnon_Exception_DatabaseException {};
class Sihnon_Exception_ConfigException extends Sihnon_Exception {};
class Sihnon_Exception_UnknownSetting extends Sihnon_Exception_ConfigException {};
class Sihnon_Exception_CacheException extends Sihnon_Exception {};
class Sihnon_Exception_InvalidCacheDir extends Sihnon_Exception_CacheException {};
class Sihnon_Exception_CacheObjectNotFound extends Sihnon_Exception_CacheException {};
class Sihnon_Exception_InvalidPluginName extends Sihnon_Exception {};
?>

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_BackgroundTask {
class SihnonFramework_BackgroundTask {
protected function __construct() {

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Cache {
class SihnonFramework_Cache {
protected $config;
protected $cache_dir;

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Config {
class SihnonFramework_Config {
/**
* Boolean value type

View File

@@ -1,6 +1,6 @@
<?php
interface Sihnon_Config_IPlugin extends Sihnon_IPlugin {
interface SihnonFramework_Config_IPlugin extends Sihnon_IPlugin {
/**
* Returns a new instance of the Plugin class

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Config_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Config_IPlugin {
class SihnonFramework_Config_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Config_IPlugin {
/**
* Name of this plugin

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Config_Plugin_FlatFile extends Sihnon_PluginBase implements Sihnon_Config_IPlugin {
class SihnonFramework_Config_Plugin_FlatFile extends Sihnon_PluginBase implements Sihnon_Config_IPlugin {
/**
* Name of this plugin

View File

@@ -1,10 +1,13 @@
<?php
class Sihnon_Config_PluginFactory extends Sihnon_PluginFactory {
class SihnonFramework_Config_PluginFactory extends Sihnon_PluginFactory {
const PLUGIN_DIR = 'Sihnon/Config/Plugin/';
const PLUGIN_PREFIX = 'Sihnon_Config_Plugin_';
const PLUGIN_INTERFACE = 'Sihnon_Config_IPlugin';
protected static $plugin_prefix = 'Sihnon_Config_Plugin_';
protected static $plugin_interface = 'Sihnon_Config_IPlugin';
protected static $plugin_dir = array(
SihnonFramework_Lib => 'SihnonFramework/Config/Plugin',
Sihnon_Lib => 'Sihnon/Config/Plugin/',
);
public static function init() {

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Database {
class SihnonFramework_Database {
private $config;
private $dbh;

View File

@@ -0,0 +1,24 @@
<?php
class SihnonFramework_Exception extends Exception {};
class SihnonFramework_Exception_NotImplemented extends Sihnon_Exception {};
class SihnonFramework_Exception_MissingDefinition extends Sihnon_Exception {};
class SihnonFramework_Exception_DatabaseException extends Sihnon_Exception {};
class SihnonFramework_Exception_DatabaseConfigMissing extends Sihnon_Exception_DatabaseException {};
class SihnonFramework_Exception_DatabaseConnectFailed extends Sihnon_Exception_DatabaseException {};
class SihnonFramework_Exception_NoDatabaseConnection extends Sihnon_Exception_DatabaseException {};
class SihnonFramework_Exception_DatabaseQueryFailed extends Sihnon_Exception_DatabaseException {};
class SihnonFramework_Exception_ResultCountMismatch extends Sihnon_Exception_DatabaseException {};
class SihnonFramework_Exception_ConfigException extends Sihnon_Exception {};
class SihnonFramework_Exception_UnknownSetting extends Sihnon_Exception_ConfigException {};
class SihnonFramework_Exception_CacheException extends Sihnon_Exception {};
class SihnonFramework_Exception_InvalidCacheDir extends Sihnon_Exception_CacheException {};
class SihnonFramework_Exception_CacheObjectNotFound extends Sihnon_Exception_CacheException {};
class SihnonFramework_Exception_InvalidPluginName extends Sihnon_Exception {};
?>

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_ForegroundTask {
class SihnonFramework_ForegroundTask {
const PIPE_STDIN = 0;
const PIPE_STDOUT = 1;

View File

@@ -1,6 +1,6 @@
<?php
interface Sihnon_IPlugin {
interface SihnonFramework_IPlugin {
public static function init();

View File

@@ -1,6 +1,6 @@
<?php
interface Sihnon_IPluginFactory {
interface SihnonFramework_IPluginFactory {
public static function init();

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Log {
class SihnonFramework_Log {
const LEVEL_DEBUG = 'DEBUG';
const LEVEL_INFO = 'INFO';
@@ -49,6 +49,6 @@ class Sihnon_Log {
}
Sihnon_Log::initialise();
SihnonFramework_Log::initialise();
?>

View File

@@ -1,6 +1,6 @@
<?php
interface Sihnon_Log_IPlugin extends Sihnon_IPlugin {
interface SihnonFramework_Log_IPlugin extends Sihnon_IPlugin {
/**
* Returns a new instance of the Plugin class

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Log_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
class SihnonFramework_Log_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
/**
* Name of this plugin

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Log_Plugin_FlatFile extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
class SihnonFramework_Log_Plugin_FlatFile extends Sihnon_PluginBase implements Sihnon_Log_IPlugin {
/**
* Name of this plugin

View File

@@ -1,10 +1,13 @@
<?php
class Sihnon_Log_PluginFactory extends Sihnon_PluginFactory {
class SihnonFramework_Log_PluginFactory extends Sihnon_PluginFactory {
const PLUGIN_DIR = 'Sihnon/Log/Plugin/';
const PLUGIN_PREFIX = 'Sihnon_Log_Plugin_';
const PLUGIN_INTERFACE = 'Sihnon_Log_IPlugin';
protected static $plugin_prefix = 'Sihnon_Log_Plugin_';
protected static $plugin_interface = 'Sihnon_Log_IPlugin';
protected static $plugin_dir = array(
SihnonFramework_Lib => 'SihnonFramework/Log/Plugin/',
Sihnon_Lib => 'Sihnon/Log/Plugin/',
);
public static function init() {

View File

@@ -1,6 +1,6 @@
<?php
abstract class Sihnon_LogEntry {
abstract class SihnonFramework_LogEntry {
protected static $table_name = "";

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Main {
class SihnonFramework_Main {
protected static $instance;
@@ -35,7 +35,7 @@ class Sihnon_Main {
/**
*
* @return Sihnon_Main
* @return SihnonFramework_Main
*/
public static function instance() {
if (!self::$instance) {
@@ -48,7 +48,7 @@ class Sihnon_Main {
/**
*
* @return Sihnon_Config
* @return SihnonFramework_Config
*/
public function config() {
return $this->config;
@@ -56,7 +56,7 @@ class Sihnon_Main {
/**
*
* @return Sihnon_Database
* @return SihnonFramework_Database
*/
public function database() {
return $this->database;
@@ -64,7 +64,7 @@ class Sihnon_Main {
/**
*
* @return Sihnon_Log
* @return SihnonFramework_Log
*/
public function log() {
return $this->log;
@@ -72,7 +72,7 @@ class Sihnon_Main {
/**
*
* @return Sihnon_Cache
* @return SihnonFramework_Cache
*/
public function cache() {
return $this->cache;
@@ -91,35 +91,74 @@ class Sihnon_Main {
}
public static function initialise() {
spl_autoload_register(array('Sihnon_Main','autoload'));
spl_autoload_register(array('SihnonFramework_Main','autoload'));
}
public static function autoload($classname) {
// Ensure the classname contains only valid class name characters
if (!preg_match('/^[A-Z][a-zA-Z0-9_]*$/', $classname)) {
throw new Exception('Illegal characters in classname'); // TODO Subclass this exception
throw new Exception('Illegal characters in classname');
}
// Ensure the class to load begins with our prefix
if (!preg_match('/^Sihnon_/', $classname)) {
return;
}
// Special case: All exceptions are stored in the same file
if (preg_match('/^Sihnon_Exception/', $classname)) {
require_once(Sihnon_Lib . 'Sihnon/Exceptions.class.php');
if (preg_match('/^SihnonFramework_/', $classname)) {
// Special case: all related exceptions are grouped into a single file
if (preg_match('/^(Sihnon(?:Framework)?_(?:.*_))Exception/', $classname, $matches = array())) {
require_once(Sihnon_Lib . preg_replace('/_/', '/', $matches[1]) . 'Exceptions.class.php');
return;
}
// Replace any underscores with directory separators
$filename = Sihnon_Lib . preg_replace('/_/', '/', $classname);
// Tack on the class file suffix
$filename .= '.class.php';
$filename = SihnonFramework_Lib . preg_replace('/_/', '/', $classname) . '.class.php';
// If this file exists, load it
if (file_exists($filename)) {
require_once $filename;
return;
}
} elseif (preg_match('/^Sihnon_/', $classname)) {
// Sihnon_ classes subclass the SihnonFramework_ classes.
// If a subclass doesn't exist, create it on the fly
// Special case: all related exceptions are grouped into a single file
if (preg_match('/^(Sihnon(?:Framework)?_(?:.*_))Exception/', $classname, $matches = array())) {
require_once(Sihnon_Lib . preg_replace('/_/', '/', $matches[1]) . 'Exceptions.class.php');
return;
}
// Replace any underscores with directory separators
$filename = Sihnon_Lib . preg_replace('/_/', '/', $classname) . '.class.php';
// If this file exists, load it
if (file_exists($filename)) {
require_once $filename;
return;
} else {
// Create this class to extend the Framework parent
$parent_classname = preg_replace('/^Sihnon_/', 'SihnonFramework_', $classname);
// Determine if the classname represents a class or an interface
$parent_class = new ReflectionClass($parent_classname);
$class_definition = '';
if ($parent_class->isFinal()) {
// Final classes cannot be extended
return;
}
if ($parent_class->isInterface()) {
$class_definition .= 'interface ';
} else {
if ($parent_class->isAbstract()) {
$class_definition .= 'abstract ';
}
$class_definition .= 'class ';
}
$class_definition .= "{$classname} extends {$parent_classname} {};";
eval($class_definition);
return;
}
}
}
@@ -155,7 +194,7 @@ class Sihnon_Main {
return $var;
}
if (is_string($default) && preg_match('/^Sihnon_Exception/', $default) && class_exists($default) && is_subclass_of($default, Sihnon_Exception)) {
if (is_string($default) && preg_match('/^Sihnon(Framework)?_Exception/', $default) && class_exists($default) && is_subclass_of($default, SihnonFramework_Exception)) {
throw new $default();
}
@@ -191,6 +230,6 @@ class Sihnon_Main {
}
Sihnon_Main::initialise();
SihnonFramework_Main::initialise();
?>

View File

@@ -4,9 +4,9 @@
* Base class for all plugins, providing default implementations for
* standard plugin methods.
*
* @class Sihnon_PluginBase
* @class SihnonFramework_PluginBase
*/
class Sihnon_PluginBase {
class SihnonFramework_PluginBase {
/**
* Provides a basic initialisation function that does nothing.

View File

@@ -1,6 +1,6 @@
<?php
abstract class Sihnon_PluginFactory implements Sihnon_IPluginFactory {
abstract class SihnonFramework_PluginFactory implements Sihnon_IPluginFactory {
static private $validPlugins = array();
@@ -11,9 +11,9 @@ abstract class Sihnon_PluginFactory implements Sihnon_IPluginFactory {
*/
public static function scan($force = false) {
if ($force || ! isset(self::$validPlugins[get_called_class()])) {
$candidatePlugins = static::findPlugins(static::PLUGIN_DIR);
$candidatePlugins = static::findPlugins(static::$plugin_dir);
static::loadPlugins($candidatePlugins, static::PLUGIN_PREFIX, static::PLUGIN_INTERFACE);
static::loadPlugins($candidatePlugins, static::$plugin_prefix, static::$plugin_interface);
}
}
@@ -32,15 +32,25 @@ abstract class Sihnon_PluginFactory implements Sihnon_IPluginFactory {
return array_keys(self::$validPlugins[get_called_class()]);
}
protected static function findPlugins($directory) {
protected static function findPlugins($directories) {
$plugins = array();
$iterator = new Sihnon_Utility_ClassFilesIterator(new Sihnon_Utility_VisibleFilesIterator(new DirectoryIterator(Sihnon_Lib . $directory)));
if (! is_array($directories)) {
$directories = array(SihnonLib => $directories);
}
foreach ($directories as $base_dir => $directory) {
if (! file_exists($base_dir . $directory)) {
continue;
}
$iterator = new Sihnon_Utility_ClassFilesIterator(new Sihnon_Utility_VisibleFilesIterator(new DirectoryIterator($base_dir . $directory)));
foreach ($iterator as /** @var SplFileInfo */ $file) {
$plugin = preg_replace('/.class.php$/', '', $file->getFilename());
$plugins[] = $plugin;
}
}
return $plugins;
}

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Utility_ClassFilesIterator extends FilterIterator {
class SihnonFramework_Utility_ClassFilesIterator extends FilterIterator {
public function accept() {
return preg_match('/.class.php$/i', $this->current()->getFilename());
}

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Utility_VisibleFilesIterator extends FilterIterator {
class SihnonFramework_Utility_VisibleFilesIterator extends FilterIterator {
public function accept() {
return !(substr($this->current()->getFilename(), 0, 1) == '.');
}

View File

@@ -1,6 +1,6 @@
<?php
class Sihnon_Utility_VisibleFilesRecursiveIterator extends RecursiveFilterIterator {
class SihnonFramework_Utility_VisibleFilesRecursiveIterator extends RecursiveFilterIterator {
public function accept() {
return !(substr($this->current()->getFilename(), 0, 1) == '.');
}