* @copyright 2004 by http://wombat.exit0.net * @package wombat * @subpackage framework */ // what time is it? list( $usec, $sec ) = explode( ' ', microtime() ); /** * Gloabl wombat settings and variable container of the wombat Factory * * IMPORTANT: * Use wbFactory::configure( 'myconfigFile.php', true|false ) to adjust factory parameter * * All parameters in here should never be changed within this file! * Cause changes may afect functionality, they have to be made very cerafully. * * Desciption of factory parameters: * * 'site' subclass of the site runner - default is "". Examples: "Cms" * * 'systemDir' the installation folder, this is where all the classe etc are * Will be found automatically! DO NOT CHANGE! * * 'baseDir' Location of client's site. Allows multiple client installations * in a single womabt-installation. By default baseDir is the same * as systemDir * * 'debug' Either 0 or 1 - switch debugmode on or of. Debugmode is usefull * during development, switch off on life-system! * * 'debugMode' Default: "Html" - select suitable debugger. * * 'useCache' Either 0 or 1 - switch caching feature on or off. Default: 1 * * 'htdocDir' Location of the web-root - relative to baseDIr. Usually, the default, * "htdoc" should be alright. * * 'configDir' Location of your configuration files. Path relative to baseDir. * The default value "conf" should be alright. This should only be * adjusted, if you want to run two separate wombat-sites within the * same baseDir (example to use a different pages, ...). * * 'configFile' Name of the main configuration file, default value: "config.xml" * This may be changed if you need to change the layout or other * global configurations for a special part of your site. * Note: per-page settings can be changed in page wide scope! * * 'adminDir' Where to find all site-admin stuff * Will be found automatically! DO NOT CHANGE! * * 'varDir' The default value "var" should suit most cases. * * 'includePath' With this path, wbFactory is able to load php-classes * Will be found automatically! DO NOT CHANGE! * * 'localClasses' This array lists class-prefixes and include-folders of * php-classes that can be included from includePath. All * other classes e.g. PEAR-classes don't come with a the * Wombat intallation and cannot be included locally. Still, * if your php.ini is set up proberly, using server-wide * classe (e.g. PEAR:DB) is not a drama. * The default values should be alright and match the current * version of Womabt Web Bench. In other words: DO NOT CHANGE! * * 'singletons' This is just a container where all create singletons will * stored. Therefore it is not a real parameter and this means: * DO NOT CHANGE - NEVER EVER! * * 'config' This will hold the global config read from configFile in * configDir. This parameter is not meant to be changed, but * you can easily access the config with: * wbFactory::getParam( 'config' ); * Again: DO NOT CHANGE! * * 'timestampGenesis' * Well, this let's the factory know when it was "born". :-) * Even if it is not the exact "start of life", it will be used * to meassure the complete processing time. Therefore it gives you * an idea about performance etc... DO NOT CHANGE! */ $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, // 'image_path' => 'img', ); /** * 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.1 * @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 = dirname( __FILE__ ) . '/../' . $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(); 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; } } ?>