* @license LGPL http://www.gnu.org/licenses/lgpl.html * @package wb * @subpackage Factory */ /** * Register LoadClass with the spl provided __autoload stack * * @see WB::loadClass() */ spl_autoload_register(array('wb', 'LoadClass')); /** * Factory class and very basic functionallity * * WB provides a static interface to instanciate and load classes. * Furthermore it keeps some settings that make the loading and creation * process very flexible. * * As this class loeader provides very basic functionallity, it is meant * to be included right at the beginning * * @version 0.1.0 * @package wb * @subpackage Factory * @example test/example/wb.php * @test test/fit/ * @deprecated in favour of WBParam and WBClass */ class WB { /** * include path * @var array */ static protected $_incPath = array(); /** * basic parameter * @var array */ static protected $_param = array( 'genesis' => null, 'local' => array( 'WB' => 'WB/' ) ); /** * list of loaded classes * @var array */ static protected $_loaded = array(); /** * simple setter for any parameter * * Simply store the given value in static map * * @param string $name * @param mixed $value * @return bool true on success * @see get() */ static public function set($name, $value) { self::$_param[$name] = $value; return true; } /** * simple getter for any parameter * * receive a value from static parameter map * * @param string $name * @return mixed value * @see set() */ static public function get($name) { if (!isset( self::$_param[$name])) { return null; } return self::$_param[$name]; } /** * add path to list of include dirs * * * * @param string $dir * @return bool true on success */ static public function addIncludePath( $dir ) { if (!is_dir($dir)) { return false; } array_unshift(self::$_incPath, $dir); return true; } /** * receive current include directories * * @return array $dirs * @see addIncludePath() */ static public function getIncludePath() { return self::$_incPath; } /** * remove all directories from include path * * CAUTION: With an empty include path, no classes can be loaded! * * @return int amount of folders in include path * @see addIncludePath() */ static public function flushIncludePath() { $count = count(self::$_incPath); self::$_incPath = array(); return $count; } /** * class loader * * walks through all configured folders and tries to * include the wanted class * * @param string $name of the to-be-loaded class * @return bool true on success */ static public function loadClass($name) { if (class_exists($name, false)) { return true; } $loaded = false; // walk through include path foreach (self::$_incPath as $incDir) { // try to match file to "local" dirs foreach (self::$_param['local'] as $prefix => $dir) { $length = strlen( $prefix ); if (strncmp($prefix, $name, $length) != 0) { continue; } $file = $dir . substr($name, $length); $file = $incDir . '/' . str_replace('_', '/', $file) . '.php'; break; } if (!file_exists($file)) { continue; } $loaded = include $file; break; } if ($loaded) { self::$_loaded[] = $name; return true; } return false; } } /** * remember the beginning of live :-) */ WB::set('genesis', microtime(true)); /** * The basic include paths need to be known right at the beginning * @see WB::addIncludePath() */ $incPath = explode(PATH_SEPARATOR, ini_get('include_path')); foreach ($incPath as $dir) { if ($dir == '.') { continue; } WB::addIncludePath($dir); } WB::addIncludePath(dirname( __FILE__ )); ?>