$params); } // Pull values from $params array $encode = isset($params['encode']) ? $params['encode'] : true; $deleted = isset($params['deleted']) ? $params['deleted'] : $deleted; if (!isset(self::$loadedBeans[$module])) { self::$loadedBeans[$module] = array(); self::$touched[$module] = array(); } $beanClass = self::getBeanName($module); if (empty($beanClass) || !class_exists($beanClass)) return false; if (!empty($id)) { if (empty(self::$loadedBeans[$module][$id])) { $bean = new $beanClass(); $result = $bean->retrieve($id, $encode, $deleted); if($result == null) return FALSE; else self::registerBean($module, $bean, $id); } else { self::$hits++; self::$touched[$module][$id]++; $bean = self::$loadedBeans[$module][$id]; } } else { $bean = new $beanClass(); } return $bean; } public static function newBean($module) { return self::getBean($module); } public static function getBeanName($module) { global $beanList; if (empty($beanList[$module])) return false; return $beanList[$module]; } /** * Returns the object name / dictionary key for a given module. This should normally * be the same as the bean name, but may not for special case modules (ex. Case vs aCase) * @static * @param String $module * @return bool */ public static function getObjectName($module) { global $objectList; if (empty($objectList[$module])) return self::getBeanName($module); return $objectList[$module]; } /** * @static * This function registers a bean with the bean factory so that it can be access from accross the code without doing * multiple retrieves. Beans should be registered as soon as they have an id. * @param String $module * @param SugarBean $bean * @param bool|String $id * @return bool true if the bean registered successfully. */ public static function registerBean($module, $bean, $id=false) { global $beanList; if (empty($beanList[$module])) return false; if (!isset(self::$loadedBeans[$module])) self::$loadedBeans[$module] = array(); //Do not double register a bean if (!empty($id) && isset(self::$loadedBeans[$module][$id])) return true; $index = "i" . (self::$total % self::$maxLoaded); //We should only hold a limited number of beans in memory at a time. //Once we have the max, unload the oldest bean. if (count(self::$loadOrder) >= self::$maxLoaded - 1) { for($i = 0; $i < self::$maxLoaded; $i++) { if (isset(self::$loadOrder[$index])) { $info = self::$loadOrder[$index]; //If a bean isn't in the database yet, we need to hold onto it. if (!empty(self::$loadedBeans[$info['module']][$info['id']]->in_save)) { self::$total++; } //Beans that have been used recently should be held in memory if possible else if (!empty(self::$touched[$info['module']][$info['id']]) && self::$touched[$info['module']][$info['id']] > 0) { self::$touched[$info['module']][$info['id']]--; self::$total++; } else break; } else { break; } $index = "i" . (self::$total % self::$maxLoaded); } if (isset(self::$loadOrder[$index])) { unset(self::$loadedBeans[$info['module']][$info['id']]); unset(self::$touched[$info['module']][$info['id']]); unset(self::$loadOrder[$index]); } } if(!empty($bean->id)) $id = $bean->id; if ($id) { self::$loadedBeans[$module][$id] = $bean; self::$total++; self::$loadOrder[$index] = array("module" => $module, "id" => $id); self::$touched[$module][$id] = 0; } else{ return false; } return true; } }