diff --git a/source/lib/Sihnon/Exceptions.class.php b/source/lib/Sihnon/Exceptions.class.php deleted file mode 100644 index ff4b7b3..0000000 --- a/source/lib/Sihnon/Exceptions.class.php +++ /dev/null @@ -1,24 +0,0 @@ - diff --git a/source/lib/Sihnon/BackgroundTask.class.php b/source/lib/SihnonFramework/BackgroundTask.class.php similarity index 85% rename from source/lib/Sihnon/BackgroundTask.class.php rename to source/lib/SihnonFramework/BackgroundTask.class.php index 28d6148..783dad4 100644 --- a/source/lib/Sihnon/BackgroundTask.class.php +++ b/source/lib/SihnonFramework/BackgroundTask.class.php @@ -1,6 +1,6 @@ 'SihnonFramework/Config/Plugin', + Sihnon_Lib => 'Sihnon/Config/Plugin/', + ); public static function init() { diff --git a/source/lib/Sihnon/Database.class.php b/source/lib/SihnonFramework/Database.class.php similarity index 99% rename from source/lib/Sihnon/Database.class.php rename to source/lib/SihnonFramework/Database.class.php index 952e5c3..61978f6 100644 --- a/source/lib/Sihnon/Database.class.php +++ b/source/lib/SihnonFramework/Database.class.php @@ -1,6 +1,6 @@ diff --git a/source/lib/Sihnon/ForegroundTask.class.php b/source/lib/SihnonFramework/ForegroundTask.class.php similarity index 98% rename from source/lib/Sihnon/ForegroundTask.class.php rename to source/lib/SihnonFramework/ForegroundTask.class.php index 60adea4..3434c4f 100644 --- a/source/lib/Sihnon/ForegroundTask.class.php +++ b/source/lib/SihnonFramework/ForegroundTask.class.php @@ -1,6 +1,6 @@ diff --git a/source/lib/Sihnon/Log/IPlugin.class.php b/source/lib/SihnonFramework/Log/IPlugin.class.php similarity index 93% rename from source/lib/Sihnon/Log/IPlugin.class.php rename to source/lib/SihnonFramework/Log/IPlugin.class.php index b6fec93..a30d404 100644 --- a/source/lib/Sihnon/Log/IPlugin.class.php +++ b/source/lib/SihnonFramework/Log/IPlugin.class.php @@ -1,6 +1,6 @@ 'SihnonFramework/Log/Plugin/', + Sihnon_Lib => 'Sihnon/Log/Plugin/', + ); public static function init() { diff --git a/source/lib/Sihnon/LogEntry.class.php b/source/lib/SihnonFramework/LogEntry.class.php similarity index 98% rename from source/lib/Sihnon/LogEntry.class.php rename to source/lib/SihnonFramework/LogEntry.class.php index 0d3916c..b3494f6 100644 --- a/source/lib/Sihnon/LogEntry.class.php +++ b/source/lib/SihnonFramework/LogEntry.class.php @@ -1,6 +1,6 @@ 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(); ?> diff --git a/source/lib/Sihnon/PluginBase.class.php b/source/lib/SihnonFramework/PluginBase.class.php similarity index 86% rename from source/lib/Sihnon/PluginBase.class.php rename to source/lib/SihnonFramework/PluginBase.class.php index 360e38f..e2e42fa 100644 --- a/source/lib/Sihnon/PluginBase.class.php +++ b/source/lib/SihnonFramework/PluginBase.class.php @@ -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. diff --git a/source/lib/Sihnon/PluginFactory.class.php b/source/lib/SihnonFramework/PluginFactory.class.php similarity index 67% rename from source/lib/Sihnon/PluginFactory.class.php rename to source/lib/SihnonFramework/PluginFactory.class.php index b1e0102..4598d6d 100644 --- a/source/lib/Sihnon/PluginFactory.class.php +++ b/source/lib/SihnonFramework/PluginFactory.class.php @@ -1,6 +1,6 @@ $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; diff --git a/source/lib/Sihnon/Utility/ClassFilesIterator.class.php b/source/lib/SihnonFramework/Utility/ClassFilesIterator.class.php similarity index 63% rename from source/lib/Sihnon/Utility/ClassFilesIterator.class.php rename to source/lib/SihnonFramework/Utility/ClassFilesIterator.class.php index ba4941b..70fdcb7 100644 --- a/source/lib/Sihnon/Utility/ClassFilesIterator.class.php +++ b/source/lib/SihnonFramework/Utility/ClassFilesIterator.class.php @@ -1,6 +1,6 @@ current()->getFilename()); } diff --git a/source/lib/Sihnon/Utility/VisibleFilesIterator.class.php b/source/lib/SihnonFramework/Utility/VisibleFilesIterator.class.php similarity index 61% rename from source/lib/Sihnon/Utility/VisibleFilesIterator.class.php rename to source/lib/SihnonFramework/Utility/VisibleFilesIterator.class.php index 88ec65e..ed01180 100644 --- a/source/lib/Sihnon/Utility/VisibleFilesIterator.class.php +++ b/source/lib/SihnonFramework/Utility/VisibleFilesIterator.class.php @@ -1,6 +1,6 @@ current()->getFilename(), 0, 1) == '.'); } diff --git a/source/lib/Sihnon/Utility/VisibleFilesRecursiveIterator.class.php b/source/lib/SihnonFramework/Utility/VisibleFilesRecursiveIterator.class.php similarity index 56% rename from source/lib/Sihnon/Utility/VisibleFilesRecursiveIterator.class.php rename to source/lib/SihnonFramework/Utility/VisibleFilesRecursiveIterator.class.php index 6a5c65b..4fa559f 100644 --- a/source/lib/Sihnon/Utility/VisibleFilesRecursiveIterator.class.php +++ b/source/lib/SihnonFramework/Utility/VisibleFilesRecursiveIterator.class.php @@ -1,6 +1,6 @@ current()->getFilename(), 0, 1) == '.'); }