* @copyright 2004/2005 by http://wombat.exit0.net * @package wombat * @subpackage framework */ // what time is it? list( $usec, $sec ) = explode( ' ', microtime() ); /** * static factory class * * - creates singletons and multiple objects for each class * - loads global settings * - dynamic class loader * - loads extensions * - loads modules * - configures loaded objects * - specialized factory opbjects that implement create function * - dynamic load of creation objects * - dynamic class loader * * @version 1.2.0 * @static * @package wombatSite * @subpackage factory */ class wbFactory { /** * Global wombat settings and variable container of the wombat Factory * * IMPORTANT: * All parameters in here should never be changed within this file! * * @see doc/config/global.txt how to adjust factory parameters * @see doc/config/factory.txt for parameter list * @var array */ protected static $params = array( 'systemDir' => null, 'baseDir' => null, 'debug' => 0, 'debugMode' => 'Html', 'useCache' => 1, 'htdocDir' => 'htdoc', 'configDir' => 'conf', 'configFile' => 'config.xml', 'mangleConfig' => null, 'adminDir' => 'admin', 'varDir' => 'var', 'includeDir' => 'include', 'localClasses' => array( 'pat' => 'pat', 'wb' => false ), 'loadedClasses' => array(), 'config' => null, 'lang' => array( 'lang' => 'C', 'tmpl' => 'C', 'form' => 'C' ), 'timestampGenesis' => null, 'withLog' => false, ); /** * List of create singleton instances * * @var array */ protected static $singletons = array(); /** * load basic config and set up factory * * @static * @param string $file configuration file name * @param boolean $load load site config from php and xml file * @return boolean $result true on success */ public static function configure( $file = 'conf/config.php', $load = true ) { // skip config if( $load ) { // relative path? if( $file[0] != '/' ) { $file = self::$params['baseDir'] . '/' . $file; } include $file; } // this class is required and must be loaded here - otherwise all errors will end in nirvana! wbFactory::includeClass( 'wbException' ); wbFactory::includeClass( 'patErrorManager' ); // configure error handling wbFactory::singleton( 'wbDebugger' ); $errorHdl = wbFactory::singleton( 'wbErrorHandler' ); set_error_handler( array( $errorHdl, 'handlePhpError' ) ); patErrorManager::setErrorHandling( E_ERROR, 'callback', array( $errorHdl, 'handlePatError' ) ); if( self::$params['debug'] ) { error_reporting( E_ALL ); patErrorManager::setErrorHandling( E_WARNING, 'callback', array( $errorHdl, 'handlePatWarning' ) ); patErrorManager::setErrorHandling( E_NOTICE, 'callback', array( $errorHdl, 'handlePatNotice' ) ); } else { error_reporting( E_ERROR | E_WARNING ); patErrorManager::setErrorHandling( E_WARNING, 'callback', array( $errorHdl, 'handlePatWarning' ) ); patErrorManager::setErrorHandling( E_NOTICE, 'ignore' ); } if( $load ) { wbFactory::loadSiteConfig(); } // configure NLS bindtextdomain( 'wombat', self::$params['systemDir'] . '/' . self::$params['varDir'] . '/lang' ); return true; } /** * load site config with patConfiguration * * Usually hhis will be called by configure() * * @return boolean $result true on success */ public static function loadSiteConfig() { $conf = wbFactory::singleton( 'patConfiguration' ); $conf->loadConfig( self::$params['configFile'] ); self::$params['config'] = $conf->getConfigValue(); $conf->clearConfigValue(); if( !self::$params['mangleConfig'] ) { return true; } // from array if( is_array( self::$params['mangleConfig'] ) ) { self::$params['config'] = array_merge( self::$params['config'], self::$params['mangleConfig'] ); return true; } // from config file if( is_string( self::$params['mangleConfig'] ) ) { $conf->loadConfig( self::$params['mangleConfig'] ); self::$params['config'] = array_merge( self::$params['config'], $conf->getConfigValue() ); $conf->clearConfigValue(); } return true; } /** * set configuration parameter * * @param string $name named parameter * @param mixed $value the new value of the parameter * @return boolean $result true on success */ public static function setParam( $name, $value ) { self::$params[$name] = $value; return true; } /** * get configuration parameter * * @param string $name named parameter * @return mixed $value the value of the parameter */ public static function getParam( $name ) { if( !isset( self::$params[$name] ) ) { return null; } return self::$params[$name]; } /** * get preconfigured singleton * * @param string $name named class * @param array $params mixed parameter to modify the creation process * @return object $obj reference to the singleton * @see create() */ public static function &singleton( $name, $params = array() ) { if( isset( self::$singletons[$name] ) && is_object( self::$singletons[$name] ) ) { return self::$singletons[$name]; } $obj = wbFactory::create( $name, $params ); // solve hen-egg-problem - see configure()! if( !patErrorManager::isError( $obj ) ) { self::$singletons[$name] = $obj; } return $obj; } /** * create a new object * * @param string $class identifier for an object to be created * @param array $params mixed parameter to modify the creation process * @return object $obj reference to the created object */ public static function &create( $class, $params = array() ) { // include class $result = wbFactory::includeClass( $class ); if( patErrorManager::isError( $result ) ) { return $result; } if( class_exists( 'wbFactory_' . $class ) ) { $obj = call_user_func( array( 'wbFactory_' . $class, 'create' ), $params ); return $obj; } // search for special creator if( file_exists( dirname( __FILE__ ) . '/wbFactory/' . $class . '.php' ) ) { include dirname( __FILE__ ) . '/wbFactory/' . $class . '.php'; $obj = call_user_func( array( 'wbFactory_' . $class, 'create' ), $params ); return $obj; } $obj = new $class(); return $obj; } /** * build module and return object * * @param string $name identifier for an object to be created * @param string $page page name * @param string $part part name * @param array @request request variables * @return object $obj reference to the created object */ public static function &getModule( $name, $page, $part, $params, $request ) { $obj = wbFactory::singleton( 'wbModule_' . $name ); if( patErrorManager::isError( $obj ) ) { return $obj; } // behave like a client specific extension $obj->setRoot( self::$params['baseDir'] ); $obj->setContext( $page, $part ); $obj->setParams( $params ); $obj->setRequest( $request ); return $obj; } /** * build client extension and return the object * * @param string $name identifier for an object to be created * @param string $page page name * @param array @request request variables * @return object $obj reference to the created object */ public static function &getExtension( $name, $page, $params, $request ) { if( isset( self::$singletons['wbExtension_' . $name ] ) && is_object( self::$singletons['wbExtension_' . $name ] ) ) { return self::$singletons['wbExtension_' . $name ]; } // include base class $result = wbFactory::includeClass( 'wbExtension' ); if( patErrorManager::isError( $result ) ) { return $result; } // search for extension if( !file_exists( self::$params['baseDir'] . '/extension/' . $name . '.php' ) ) { return patErrorManager::raiseWarning( 'wbFactory:2', 'extension file not found!', 'Extension: ' . $name ); } include self::$params['baseDir'] . '/extension/' . $name . '.php'; $class = 'wbExtension_' . $name; $obj = new $class(); $obj->setRoot( self::$params['baseDir'] ); $obj->setPage( $page ); $obj->setParams( $params ); $obj->setRequest( $request ); self::$singletons['wbExtension_' . $name ] = $obj; return $obj; } /** * include php file for * * @param string $class name class to be included * @return boolean $result true on success * @see create() */ public static function includeClass( $class ) { // already loaded? if( in_array( $class, self::$params['loadedClasses'] ) ) { return true; } // translate class name to directories and file $name = str_replace( '_', '/', $class ); $prefix = ''; $locals = array_keys( self::$params['localClasses'] ); $incDirs = array( self::$params['baseDir'] ); if( self::$params['baseDir'] != self::$params['systemDir'] ) { $incDirs[] = self::$params['systemDir']; } foreach( $incDirs as $incDir ) { foreach( $locals as $local ) { if( strstr( $name, $local ) ) { // include base-classes $base = explode( '/', $name ); if( count( $base ) > 1 ) { array_pop( $base ); $baseName = implode( '_', $base ); $result = wbFactory::includeClass( $baseName ); if( patErrorManager::isError( $result ) ) { return $result; } } $prefix = $incDir . '/' . self::$params['includeDir'] . '/'; // add subdirectoy if( self::$params['localClasses'][$local] !== false ) { $prefix .= self::$params['localClasses'][$local] . '/'; } break; } } if( @include $prefix . $name . '.php' ) { array_push( self::$params['loadedClasses'], $class ); return true; } } return patErrorManager::raiseWarning( 'wbFactory:3', 'Failed to include class', 'Class: ' . $class ); } } /** * Set some static variables * * This is sort of a static initializer */ wbFactory::setParam( 'systemDir', realpath( dirname( __FILE__ ) . '/..' ) ); wbFactory::setParam( 'baseDir', wbFactory::getParam( 'systemDir' ) ); wbFactory::setParam( 'timestampGenesis', (float) $usec + (float) $sec ); ?>