2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
39 require_once('data/SugarBean.php');
42 * Factory to create SugarBeans
46 protected static $loadedBeans = array();
47 protected static $maxLoaded = 10;
48 protected static $total = 0;
49 protected static $loadOrder = array();
50 protected static $touched = array();
51 public static $hits = 0;
54 * Returns a SugarBean object by id. The Last 10 loaded beans are cached in memory to prevent multiple retrieves per request.
55 * If no id is passed, a new bean is created.
57 * @param String $module
59 * @param Bool $encode @see SugarBean::retrieve
60 * @param Bool $deleted @see SugarBean::retrieve
63 public static function getBean($module, $id = null, $encode = true, $deleted = true)
65 if (!isset(self::$loadedBeans[$module])) {
66 self::$loadedBeans[$module] = array();
67 self::$touched[$module] = array();
70 $beanClass = self::getBeanName($module);
72 if (empty($beanClass) || !class_exists($beanClass)) return false;
76 if (empty(self::$loadedBeans[$module][$id]))
78 $bean = new $beanClass();
79 $result = $bean->retrieve($id, $encode, $deleted);
83 self::registerBean($module, $bean, $id);
87 self::$touched[$module][$id]++;
88 $bean = self::$loadedBeans[$module][$id];
91 $bean = new $beanClass();
97 public static function newBean($module)
99 return self::getBean($module);
102 public static function getBeanName($module)
105 if (empty($beanList[$module])) return false;
107 return $beanList[$module];
111 * Returns the object name / dictionary key for a given module. This should normally
112 * be the same as the bean name, but may not for special case modules (ex. Case vs aCase)
114 * @param String $module
117 public static function getObjectName($module)
120 if (empty($objectList[$module]))
121 return self::getBeanName($module);
123 return $objectList[$module];
129 * This function registers a bean with the bean factory so that it can be access from accross the code without doing
130 * multiple retrieves. Beans should be registered as soon as they have an id.
131 * @param String $module
132 * @param SugarBean $bean
133 * @param bool|String $id
134 * @return bool true if the bean registered successfully.
136 public static function registerBean($module, $bean, $id=false)
139 if (empty($beanList[$module])) return false;
141 if (!isset(self::$loadedBeans[$module]))
142 self::$loadedBeans[$module] = array();
144 //Do not double register a bean
145 if (!empty($id) && isset(self::$loadedBeans[$module][$id]))
148 $index = "i" . (self::$total % self::$maxLoaded);
149 //We should only hold a limited number of beans in memory at a time.
150 //Once we have the max, unload the oldest bean.
151 if (count(self::$loadOrder) >= self::$maxLoaded - 1)
153 for($i = 0; $i < self::$maxLoaded; $i++)
155 if (isset(self::$loadOrder[$index]))
157 $info = self::$loadOrder[$index];
158 //If a bean isn't in the database yet, we need to hold onto it.
159 if (!empty(self::$loadedBeans[$info['module']][$info['id']]->in_save))
163 //Beans that have been used recently should be held in memory if possible
164 else if (!empty(self::$touched[$info['module']][$info['id']]) && self::$touched[$info['module']][$info['id']] > 0)
166 self::$touched[$info['module']][$info['id']]--;
174 $index = "i" . (self::$total % self::$maxLoaded);
176 if (isset(self::$loadOrder[$index]))
178 unset(self::$loadedBeans[$info['module']][$info['id']]);
179 unset(self::$touched[$info['module']][$info['id']]);
180 unset(self::$loadOrder[$index]);
184 if(!empty($bean->id))
189 self::$loadedBeans[$module][$id] = $bean;
191 self::$loadOrder[$index] = array("module" => $module, "id" => $id);
192 self::$touched[$module][$id] = 0;