Updates framework to support automated framework subclasses
All framework classes have been renamed to use the SihnonFramework_ prefix. The class autoloader now looks for subclasses of the framework classes in the Sihnon_Lib directory, and automatically creates them if they don't exist. The autoloader correctly creates interfaces and abstract classes as needed, by using reflection to check the type of the parent class. All references to classes within the framework now use the Sihnon_ prefix. The PluginFactory supports multiple scan directories, and will search both the framework and subclass class tree to find candidate plugins.
This commit is contained in:
@@ -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 {};
|
||||
|
||||
?>
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Sihnon_BackgroundTask {
|
||||
class SihnonFramework_BackgroundTask {
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Sihnon_Cache {
|
||||
class SihnonFramework_Cache {
|
||||
|
||||
protected $config;
|
||||
protected $cache_dir;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Sihnon_Config {
|
||||
class SihnonFramework_Config {
|
||||
|
||||
/**
|
||||
* Boolean value type
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Sihnon_Database {
|
||||
class SihnonFramework_Database {
|
||||
|
||||
private $config;
|
||||
private $dbh;
|
||||
24
source/lib/SihnonFramework/Exceptions.class.php
Normal file
24
source/lib/SihnonFramework/Exceptions.class.php
Normal 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 {};
|
||||
|
||||
?>
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Sihnon_ForegroundTask {
|
||||
class SihnonFramework_ForegroundTask {
|
||||
|
||||
const PIPE_STDIN = 0;
|
||||
const PIPE_STDOUT = 1;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
interface Sihnon_IPlugin {
|
||||
interface SihnonFramework_IPlugin {
|
||||
|
||||
public static function init();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
interface Sihnon_IPluginFactory {
|
||||
interface SihnonFramework_IPluginFactory {
|
||||
|
||||
public static function init();
|
||||
|
||||
@@ -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();
|
||||
|
||||
?>
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
abstract class Sihnon_LogEntry {
|
||||
abstract class SihnonFramework_LogEntry {
|
||||
|
||||
protected static $table_name = "";
|
||||
|
||||
@@ -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');
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace any underscores with directory separators
|
||||
$filename = Sihnon_Lib . preg_replace('/_/', '/', $classname);
|
||||
|
||||
// Tack on the class file suffix
|
||||
$filename .= '.class.php';
|
||||
|
||||
// If this file exists, load it
|
||||
if (file_exists($filename)) {
|
||||
require_once $filename;
|
||||
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 = 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();
|
||||
|
||||
?>
|
||||
@@ -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.
|
||||
@@ -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,14 +32,24 @@ 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 ($iterator as /** @var SplFileInfo */ $file) {
|
||||
$plugin = preg_replace('/.class.php$/', '', $file->getFilename());
|
||||
$plugins[] = $plugin;
|
||||
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;
|
||||
@@ -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());
|
||||
}
|
||||
@@ -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) == '.');
|
||||
}
|
||||
@@ -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) == '.');
|
||||
}
|
||||
Reference in New Issue
Block a user