* @copyright 2004 by http://wombat.exit0.net * @package wombat * @subpackage framework */ // what time is it? list( $usec, $sec ) = explode( ' ', microtime() ); /** * Global wombat settings and variable container of the wombat Factory * * This is sort of a "fake" static variable... * * 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 */ $GLOBALS['_wbFactory'] = array( 'systemDir' => realpath( dirname( __FILE__ ) . '/..' ), 'baseDir' => realpath( dirname( __FILE__ ) . '/..' ), 'debug' => 0, 'debugMode' => 'Html', 'useCache' => 1, 'htdocDir' => 'htdoc', 'configDir' => 'conf', 'configFile' => 'config.xml', 'adminDir' => 'admin', 'varDir' => 'var', 'includePath' => dirname( __FILE__ ), 'localClasses' => array( 'pat' => 'pat', 'wb' => false ), 'singletons' => array(), 'config' => null, 'timestampGenesis' => (float) $usec + (float)$sec, ); /** * 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.1.3 * @static * @package wombatSite * @subpackage factory */ class wbFactory { /** * 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 */ function configure( $file = 'conf/config.php', $load = true ) { // skip config if( $load ) { // relative path? if( $file[0] != '/' ) { $file = $GLOBALS['_wbFactory']['baseDir'] . '/' . $file; } include_once $file; } // this class is required and must be loaded here - otherwise all errors will end in nirvana! 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( $GLOBALS['_wbFactory']['debug'] ) { error_reporting( E_ALL ); patErrorManager::setErrorHandling( E_WARNING, 'callback', array( $errorHdl, 'handlePatWarning' ) ); patErrorManager::setErrorHandling( E_NOTICE, 'callback', array( $errorHdl, 'handlePatNotice' ) ); } else { patErrorManager::setErrorHandling( E_WARNING, 'callback', array( $errorHdl, 'handlePatWarning' ) ); patErrorManager::setErrorHandling( E_NOTICE, 'ignore' ); } if( $load ) { wbFactory::loadSiteConfig(); } return true; } /** * load site config with patConfiguration * * Usually hhis will be called by configure() * * @static * @return boolean $result true on success */ function loadSiteConfig() { $conf =& wbFactory::singleton( 'patConfiguration' ); $conf->loadConfig( $GLOBALS['_wbFactory']['configFile'] ); $GLOBALS['_wbFactory']['config'] = $conf->getConfigValue(); $conf->clearConfigValue(); if( isset( $GLOBALS['_wbFactory']['config']['locale']['all'] ) ) { setlocale( LC_ALL, $GLOBALS['_wbFactory']['config']['locale']['all'] ); } if( isset( $GLOBALS['_wbFactory']['config']['locale']['time'] ) ) { setlocale( LC_TIME, $GLOBALS['_wbFactory']['config']['locale']['time'] ); } return true; } /** * set configuration parameter * * @static * @access public * @param string $name named parameter * @param mixed $value the new value of the parameter * @return boolean $result true on success */ function setParam( $name, $value ) { $GLOBALS['_wbFactory'][$name] = $value; return true; } /** * get configuration parameter * * @static * @access public * @param string $name named parameter * @return mixed $value the value of the parameter */ function getParam( $name ) { if( !isset( $GLOBALS['_wbFactory'][$name] ) ) { return null; } return $GLOBALS['_wbFactory'][$name]; } /** * get preconfigured singleton * * @static * @access public * @param string $name named class * @param array $params mixed parameter to modify the creation process * @return object $obj reference to the singleton * @see create() */ function &singleton( $name, $params = array() ) { if( isset( $GLOBALS['_wbFactory']['singletons'][$name] ) && is_object( $GLOBALS['_wbFactory']['singletons'][$name] ) ) { return $GLOBALS['_wbFactory']['singletons'][$name]; } $obj =& wbFactory::create( $name, $params ); // solve hen-egg-problem - see configure()! if( !patErrorManager::isError( $obj ) ) { $GLOBALS['_wbFactory']['singletons'][$name] =& $obj; } return $obj; } /** * create a new object * * @static * @access public * @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 */ function &create( $class, $params = array() ) { // include class $result =& wbFactory::includeClass( $class ); if( patErrorManager::isError( $result ) ) { return $result; } // search for special creator if( file_exists( dirname( __FILE__ ) . '/wbFactory/' . $class . '.php' ) ) { include_once dirname( __FILE__ ) . '/wbFactory/' . $class . '.php'; return call_user_func( array( 'wbFactory_' . $class, 'create' ), $params ); } return new $class(); } /** * build module and return object * * * @static * @access public * @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 */ 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( $GLOBALS['_wbFactory']['baseDir'] ); $obj->setContext( $page, $part ); $obj->setParams( $params ); $obj->setRequest( $request ); return $obj; } /** * build client extension and return the object * * @static * @access public * @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 */ function &getExtension( $name, $page, $params, $request ) { if( isset( $GLOBALS['_wbFactory']['singletons']['wbExtension_' . $name ] ) && is_object( $GLOBALS['_wbFactory']['singletons']['wbExtension_' . $name ] ) ) { return $GLOBALS['_wbFactory']['singletons']['wbExtension_' . $name ]; } // include base class $result =& wbFactory::includeClass( 'wbExtension' ); if( patErrorManager::isError( $result ) ) { return $result; } // search for extension if( !file_exists( $GLOBALS['_wbFactory']['baseDir'] . '/extension/' . $name . '.php' ) ) { return patErrorManager::raiseWarning( 'wbFactory:2', 'extension file not found!', 'Extension: ' . $name ); } include_once $GLOBALS['_wbFactory']['baseDir'] . '/extension/' . $name . '.php'; $class = 'wbExtension_' . $name; $obj =& new $class(); $obj->setRoot( $GLOBALS['_wbFactory']['baseDir'] ); $obj->setPage( $page ); $obj->setParams( $params ); $obj->setRequest( $request ); return $obj; } /** * include php file for * * @static * @access public * @param string $class name class to be included * @return boolean $result true on success * @see create() */ function includeClass( $class ) { // translate class name to directories and file $name = str_replace( '_', '/', $class ); $prefix = ''; $locals = array_keys( $GLOBALS['_wbFactory']['localClasses'] ); 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 = $GLOBALS['_wbFactory']['includePath'] . '/'; // add subdirectoy if( $GLOBALS['_wbFactory']['localClasses'][$local] !== false ) { $prefix .= $GLOBALS['_wbFactory']['localClasses'][$local] . '/'; } break; } } if( !( include_once $prefix . $name . '.php' ) ) { return patErrorManager::raiseWarning( 'wbFactory:3', 'Failed to include class', 'Class: ' . $class ); } return true; } } ?>