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 <?php
class Sihnon_BackgroundTask { class SihnonFramework_BackgroundTask {
protected function __construct() { protected function __construct() {

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
<?php <?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 * Name of this plugin

View File

@@ -1,6 +1,6 @@
<?php <?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 * Name of this plugin

View File

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

View File

@@ -1,6 +1,6 @@
<?php <?php
class Sihnon_Database { class SihnonFramework_Database {
private $config; private $config;
private $dbh; 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 <?php
class Sihnon_ForegroundTask { class SihnonFramework_ForegroundTask {
const PIPE_STDIN = 0; const PIPE_STDIN = 0;
const PIPE_STDOUT = 1; const PIPE_STDOUT = 1;

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
<?php <?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 * Name of this plugin

View File

@@ -1,6 +1,6 @@
<?php <?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 * Name of this plugin

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
<?php <?php
class Sihnon_Main { class SihnonFramework_Main {
protected static $instance; protected static $instance;
@@ -35,7 +35,7 @@ class Sihnon_Main {
/** /**
* *
* @return Sihnon_Main * @return SihnonFramework_Main
*/ */
public static function instance() { public static function instance() {
if (!self::$instance) { if (!self::$instance) {
@@ -48,7 +48,7 @@ class Sihnon_Main {
/** /**
* *
* @return Sihnon_Config * @return SihnonFramework_Config
*/ */
public function config() { public function config() {
return $this->config; return $this->config;
@@ -56,7 +56,7 @@ class Sihnon_Main {
/** /**
* *
* @return Sihnon_Database * @return SihnonFramework_Database
*/ */
public function database() { public function database() {
return $this->database; return $this->database;
@@ -64,7 +64,7 @@ class Sihnon_Main {
/** /**
* *
* @return Sihnon_Log * @return SihnonFramework_Log
*/ */
public function log() { public function log() {
return $this->log; return $this->log;
@@ -72,7 +72,7 @@ class Sihnon_Main {
/** /**
* *
* @return Sihnon_Cache * @return SihnonFramework_Cache
*/ */
public function cache() { public function cache() {
return $this->cache; return $this->cache;
@@ -91,35 +91,74 @@ class Sihnon_Main {
} }
public static function initialise() { public static function initialise() {
spl_autoload_register(array('Sihnon_Main','autoload')); spl_autoload_register(array('SihnonFramework_Main','autoload'));
} }
public static function autoload($classname) { public static function autoload($classname) {
// Ensure the classname contains only valid class name characters // Ensure the classname contains only valid class name characters
if (!preg_match('/^[A-Z][a-zA-Z0-9_]*$/', $classname)) { 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 // Ensure the class to load begins with our prefix
if (!preg_match('/^Sihnon_/', $classname)) { if (preg_match('/^SihnonFramework_/', $classname)) {
return; // 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');
// Special case: All exceptions are stored in the same file
if (preg_match('/^Sihnon_Exception/', $classname)) {
require_once(Sihnon_Lib . 'Sihnon/Exceptions.class.php');
return; return;
} }
// Replace any underscores with directory separators // Replace any underscores with directory separators
$filename = Sihnon_Lib . preg_replace('/_/', '/', $classname); $filename = SihnonFramework_Lib . preg_replace('/_/', '/', $classname) . '.class.php';
// Tack on the class file suffix
$filename .= '.class.php';
// If this file exists, load it // If this file exists, load it
if (file_exists($filename)) { if (file_exists($filename)) {
require_once $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; 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(); 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 * Base class for all plugins, providing default implementations for
* standard plugin methods. * standard plugin methods.
* *
* @class Sihnon_PluginBase * @class SihnonFramework_PluginBase
*/ */
class Sihnon_PluginBase { class SihnonFramework_PluginBase {
/** /**
* Provides a basic initialisation function that does nothing. * Provides a basic initialisation function that does nothing.

View File

@@ -1,6 +1,6 @@
<?php <?php
abstract class Sihnon_PluginFactory implements Sihnon_IPluginFactory { abstract class SihnonFramework_PluginFactory implements Sihnon_IPluginFactory {
static private $validPlugins = array(); static private $validPlugins = array();
@@ -11,9 +11,9 @@ abstract class Sihnon_PluginFactory implements Sihnon_IPluginFactory {
*/ */
public static function scan($force = false) { public static function scan($force = false) {
if ($force || ! isset(self::$validPlugins[get_called_class()])) { 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()]); return array_keys(self::$validPlugins[get_called_class()]);
} }
protected static function findPlugins($directory) { protected static function findPlugins($directories) {
$plugins = array(); $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) { foreach ($iterator as /** @var SplFileInfo */ $file) {
$plugin = preg_replace('/.class.php$/', '', $file->getFilename()); $plugin = preg_replace('/.class.php$/', '', $file->getFilename());
$plugins[] = $plugin; $plugins[] = $plugin;
} }
}
return $plugins; return $plugins;
} }

View File

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

View File

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

View File

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