Index: branches/5.3.x/core/kernel/utility/factory.php
===================================================================
diff -u -N -r15902 -r16156
--- branches/5.3.x/core/kernel/utility/factory.php (.../factory.php) (revision 15902)
+++ branches/5.3.x/core/kernel/utility/factory.php (.../factory.php) (revision 16156)
@@ -1,6 +1,6 @@
Application->ModuleInfo ) {
+ $error_msg = 'Autoloader configuration can be only performed after module information is available';
+ throw new LogicException($error_msg);
+ }
+
+ $this->namespaceMap = array();
+
+ foreach ( $this->Application->ModuleInfo as $module_name => $module_info ) {
+ if ( $module_name == 'In-Portal' ) {
+ continue;
+ }
+
+ $this->namespaceMap[$module_info['ClassNamespace']] = rtrim($module_info['Path'], '/');
+ }
+
+ if ( defined('IS_INSTALL') && IS_INSTALL ) {
+ // During installation process all modules, because unit configs from all modules are scanned too.
+ $class_map_builders = ClassMapBuilder::createBuilders();
+ }
+ else {
+ $class_map_builders = ClassMapBuilder::createBuilders($this->Application->ModuleInfo);
+ }
+
+ foreach ( $class_map_builders as $class_map_builder ) {
+ $class_map = $class_map_builder->get();
+ $class_names = array_keys($class_map);
+
+ $this->classMap = array_merge($this->classMap, $class_map);
+ $this->realClasses = array_merge($this->realClasses, array_combine($class_names, $class_names));
+ }
+ }
+
+ /**
* Sets data from cache to object
*
* @param Array $data
@@ -61,6 +109,7 @@
public function setFromCache(&$data)
{
$this->classMap = $data['Factory.Files'];
+ $this->namespaceMap = $data['Factory.Namespaces'];
$this->realClasses = $data['Factory.realClasses'];
}
@@ -116,15 +165,11 @@
$class_path .= str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
- foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
- if ( $module_name == 'In-Portal' ) {
- continue;
- }
-
- if ( strpos($class, $module_info['ClassNamespace']) === 0 ) {
+ foreach ( $this->namespaceMap as $namespace_prefix => $namespace_path ) {
+ if ( strpos($class, $namespace_prefix) === 0 ) {
$test_class_path = str_replace(
- str_replace('\\', DIRECTORY_SEPARATOR, $module_info['ClassNamespace']),
- rtrim($module_info['Path'], '/'),
+ str_replace('\\', DIRECTORY_SEPARATOR, $namespace_prefix),
+ $namespace_path,
$class_path
);
@@ -145,8 +190,13 @@
*/
public function getToCache()
{
+ ksort($this->classMap);
+ ksort($this->namespaceMap);
+ ksort($this->realClasses);
+
return Array (
'Factory.Files' => $this->classMap,
+ 'Factory.Namespaces' => $this->namespaceMap,
'Factory.realClasses' => $this->realClasses,
);
}
@@ -182,17 +232,15 @@
return Array ('prefix' => $prefix, 'special' => $special, 'prefix_special' => $prefix_special);
}
-
/**
- * Returns object using params specified, creates it if is required
+ * Returns object using params specified, creates it if is required.
*
- * @param string $name
- * @param string $pseudo_class
- * @param Array $event_params
- * @param Array $arguments
+ * @param string $name Object name in factory.
+ * @param string $pseudo_class Pseudo class.
+ * @param Array $event_params Event params.
+ * @param Array $arguments Constructor arguments.
+ *
* @return kBase
- * @access public
- * @throws kFactoryException
*/
public function getObject($name, $pseudo_class = '', $event_params = Array (), $arguments = Array ())
{
@@ -208,23 +256,6 @@
$pseudo_class = $ret['prefix'];
}
- if ( !isset($this->realClasses[$pseudo_class]) ) {
- $error_msg = 'RealClass not defined for pseudo_class ' . $pseudo_class . '';
-
- if ( $this->Application->isInstalled() ) {
- throw new kFactoryException($error_msg);
- }
- else {
- if ( $this->Application->isDebugMode() ) {
- $this->Application->Debugger->appendTrace();
- }
-
- trigger_error($error_msg, E_USER_WARNING);
- }
-
- return false;
- }
-
if ( defined('DEBUG_MODE') && defined('DBG_FACTORY') && DBG_FACTORY && $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendHTML('Creating object: Pseudo class: ' . $pseudo_class . ' Prefix: ' . $name);
$this->Application->Debugger->appendTrace();
@@ -262,21 +293,36 @@
}
/**
- * Get's real class name for pseudo class,
- * includes class file and creates class
- * instance.
- * All parameters except first one are passed to object constuctor
- * through mediator method makeClass that creates instance of class
+ * Get's real class name for pseudo class, includes class file and creates class instance.
*
* Pattern: Factory Method
*
- * @param string $pseudo_class
- * @param Array $arguments
+ * @param string $pseudo_class Pseudo class.
+ * @param array $arguments Constructor arguments.
+ *
* @return kBase
- * @access public
+ * @throws kFactoryException When class not found.
*/
public function makeClass($pseudo_class, $arguments = Array ())
{
+ if ( !isset($this->realClasses[$pseudo_class]) ) {
+ $error_msg = 'RealClass not defined for "' . $pseudo_class . '" pseudo_class.';
+ $error_msg .= ' Please use "php tools/build_class_map.php" to discover new classes.';
+
+ if ( $this->Application->isInstalled() ) {
+ throw new kFactoryException($error_msg);
+ }
+ else {
+ if ( $this->Application->isDebugMode() ) {
+ $this->Application->Debugger->appendTrace();
+ }
+
+ trigger_error($error_msg, E_USER_WARNING);
+ }
+
+ return false;
+ }
+
$real_class = $this->realClasses[$pseudo_class];
$mem_before = memory_get_usage();
@@ -344,4 +390,4 @@
class kFactoryException extends Exception {
-}
\ No newline at end of file
+}