Updates class autoloader to use class_alias

Makes Exceptions extend the base classes, rather than the subclasses to
avoid class redefinition errors.
Makes PluginFactories define the interface using the base class rather
than a subclass to avoid class_implements() to fail by testing for a
class not in the hierarchy.
This commit is contained in:
2010-10-11 23:04:20 +01:00
parent f592abe15f
commit 6d1705f5a9
4 changed files with 43 additions and 43 deletions

View File

@@ -3,7 +3,7 @@
class SihnonFramework_Config_PluginFactory extends Sihnon_PluginFactory { class SihnonFramework_Config_PluginFactory extends Sihnon_PluginFactory {
protected static $plugin_prefix = 'Sihnon_Config_Plugin_'; protected static $plugin_prefix = 'Sihnon_Config_Plugin_';
protected static $plugin_interface = 'Sihnon_Config_IPlugin'; protected static $plugin_interface = 'SihnonFramework_Config_IPlugin';
protected static $plugin_dir = array( protected static $plugin_dir = array(
SihnonFramework_Lib => 'SihnonFramework/Config/Plugin', SihnonFramework_Lib => 'SihnonFramework/Config/Plugin',
Sihnon_Lib => 'Sihnon/Config/Plugin/', Sihnon_Lib => 'Sihnon/Config/Plugin/',

View File

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

View File

@@ -3,7 +3,7 @@
class SihnonFramework_Log_PluginFactory extends Sihnon_PluginFactory { class SihnonFramework_Log_PluginFactory extends Sihnon_PluginFactory {
protected static $plugin_prefix = 'Sihnon_Log_Plugin_'; protected static $plugin_prefix = 'Sihnon_Log_Plugin_';
protected static $plugin_interface = 'Sihnon_Log_IPlugin'; protected static $plugin_interface = 'SihnonFramework_Log_IPlugin';
protected static $plugin_dir = array( protected static $plugin_dir = array(
SihnonFramework_Lib => 'SihnonFramework/Log/Plugin/', SihnonFramework_Lib => 'SihnonFramework/Log/Plugin/',
Sihnon_Lib => 'Sihnon/Log/Plugin/', Sihnon_Lib => 'Sihnon/Log/Plugin/',

View File

@@ -114,7 +114,7 @@ class SihnonFramework_Main {
// Ensure the class to load begins with our prefix // Ensure the class to load begins with our prefix
if (preg_match("/^{$class['base']}_/", $classname)) { if (preg_match("/^{$class['base']}_/", $classname)) {
// Special case: all related exceptions are grouped into a single file // Special case: all related exceptions are grouped into a single file
if (preg_match("/^({$class['base']}_(?:.*_))Exception/", $classname, $matches = array())) { if (preg_match("/^({$class['base']}_(?:.*?_)?)Exception/", $classname, $matches)) {
require_once($class['base_dir_prefix'] . preg_replace('/_/', '/', $matches[1]) . 'Exceptions.class.php'); require_once($class['base_dir_prefix'] . preg_replace('/_/', '/', $matches[1]) . 'Exceptions.class.php');
return; return;
} }
@@ -124,7 +124,7 @@ class SihnonFramework_Main {
// 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(realpath($filename));
return; return;
} }
} elseif ($class['subclass'] && preg_match("/^{$class['subclass']}_/", $classname)) { } elseif ($class['subclass'] && preg_match("/^{$class['subclass']}_/", $classname)) {
@@ -132,9 +132,16 @@ class SihnonFramework_Main {
// If a subclass doesn't exist, create it on the fly // If a subclass doesn't exist, create it on the fly
// Special case: all related exceptions are grouped into a single file // Special case: all related exceptions are grouped into a single file
if (preg_match("/^({$class['subclass']}_(?:.*_))Exception/", $classname, $matches = array())) { if (preg_match("/^({$class['subclass']}_(?:.*?_)?)Exception/", $classname, $matches)) {
require_once($class['subclass_dir_prefix'] . preg_replace('/_/', '/', $matches[1]) . 'Exceptions.class.php'); $exceptions_filename = $class['subclass_dir_prefix'] . preg_replace('/_/', '/', $matches[1]) . 'Exceptions.class.php';
return; if (file_exists($exceptions_filename)) {
require_once($exceptions_filename);
} else {
// Create this class to extend the Framework parent
$parent_classname = preg_replace("/^{$class['subclass']}_/", "{$class['base']}_", $classname);
class_alias($parent_classname, $classname);
return;
}
} }
// Replace any underscores with directory separators // Replace any underscores with directory separators
@@ -142,32 +149,12 @@ class SihnonFramework_Main {
// 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(realpath($filename));
return; return;
} else { } else {
// Create this class to extend the Framework parent // Create this class to extend the Framework parent
$parent_classname = preg_replace("/^{$class['subclass']}_/", "{$class['base']}_", $classname); $parent_classname = preg_replace("/^{$class['subclass']}_/", "{$class['base']}_", $classname);
class_alias($parent_classname, $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; return;
} }
} }
@@ -190,6 +177,14 @@ class SihnonFramework_Main {
* @param unknown_type $subclass_dir_prefix * @param unknown_type $subclass_dir_prefix
*/ */
public static function registerAutoloadClasses($base, $base_dir_prefix, $subclass = null, $subclass_dir_prefix = null) { public static function registerAutoloadClasses($base, $base_dir_prefix, $subclass = null, $subclass_dir_prefix = null) {
// The paths must end with a trailing slash
if ($base_dir_prefix && $base_dir_prefix[strlen($base_dir_prefix) - 1] != DIRECTORY_SEPARATOR) {
$base_dir_prefix .= DIRECTORY_SEPARATOR;
}
if ($subclass_dir_prefix && $subclass_dir_prefix[strlen($subclass_dir_prefix) - 1] != DIRECTORY_SEPARATOR) {
$subclass_dir_prefix .= DIRECTORY_SEPARATOR;
}
self::$autoload_classes[] = array( self::$autoload_classes[] = array(
'base' => $base, 'base' => $base,
'base_dir_prefix' => $base_dir_prefix, 'base_dir_prefix' => $base_dir_prefix,
@@ -210,6 +205,11 @@ class SihnonFramework_Main {
} }
} }
public static function makeAbsolutePath($relative_path) {
$absolute_path = getcwd() . DIRECTORY_SEPARATOR . $relative_path;
return realpath($absolute_path);
}
public static function mkdir_recursive($directory, $permissions=0777) { public static function mkdir_recursive($directory, $permissions=0777) {
$parts = explode('/', $directory); $parts = explode('/', $directory);
$path = ''; $path = '';