Created
November 21, 2013 14:39
-
-
Save bfintal/7582665 to your computer and use it in GitHub Desktop.
A factory class for getting the latest version of a class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( !function_exists( 'bfi_thumb' ) ) { | |
function bfi_thumb() { | |
return BFI_Class_Factory::getNewestVersion( 'BFI_Thumb' )->run(); | |
} | |
} | |
if ( !class_exists( 'BFI_Class_Factory' ) ) { | |
class BFI_Class_Factory { | |
public static $versions = array(); | |
public static $latestClass = array(); | |
public static function addClassVersion( $baseClassName, $className, $version ) { | |
if ( empty( self::$versions[$baseClassName] ) ) { | |
self::$versions[$baseClassName] = array(); | |
} | |
self::$versions[$baseClassName][] = array( | |
'class' => $className, | |
'version' => $version | |
); | |
} | |
public static function getNewestVersion( $baseClassName ) { | |
if ( empty( self::$latestClass[$baseClassName] ) ) { | |
usort( self::$versions[$baseClassName], array( __CLASS__, "versionCompare" ) ); | |
self::$latestClass[$baseClassName] = new self::$versions[$baseClassName][0]['class']; | |
unset( self::$versions[$baseClassName] ); | |
} | |
return self::$latestClass[$baseClassName]; | |
} | |
public static function versionCompare( $a, $b ) { | |
return version_compare( $a['version'], $b['version'] ) == 1 ? -1 : 1; | |
} | |
} | |
} | |
if ( !class_exists( 'BFI_Thumb_2_0' ) ) { | |
BFI_Class_Factory::addClassVersion( 'BFI_Thumb', 'BFI_Thumb_2_0', '2.0' ); | |
class BFI_Thumb_2_0 { | |
public function run() { | |
echo "Running v2.0"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment