2
0
forked from Wavyzz/dolibarr

Merge branch 'develop' into fix/PhanTypeMismatchReturn

This commit is contained in:
Laurent Destailleur
2024-03-16 16:27:32 +01:00
committed by GitHub
10 changed files with 5409 additions and 19 deletions

View File

@@ -12413,12 +12413,14 @@ function getElementProperties($elementType)
* @param int $element_id Element id (Use this or element_id but not both)
* @param string $element_type Element type ('module' or 'myobject@mymodule' or 'mymodule_myobject')
* @param string $element_ref Element ref (Use this or element_id but not both)
* @param int $useCache if you want to store object in cache or get it from cache 0 => no use cache , 1 use cache, 2 force reload cache
* @param int $maxCacheByType number of object in cache for this element type
* @return int|object object || 0 || <0 if error
* @see getElementProperties()
*/
function fetchObjectByElement($element_id, $element_type, $element_ref = '')
function fetchObjectByElement($element_id, $element_type, $element_ref = '', $useCache = 0, $maxCacheByType = 10)
{
global $db;
global $db, $globalCacheForGetObjectFromCache;
$ret = 0;
@@ -12437,11 +12439,19 @@ function fetchObjectByElement($element_id, $element_type, $element_ref = '')
}
if (is_array($element_prop) && (empty($element_prop['module']) || $ismodenabled)) {
if ($useCache === 1
&& !empty($globalCacheForGetObjectFromCache[$element_type])
&& !empty($globalCacheForGetObjectFromCache[$element_type][$element_id])
&& is_object($globalCacheForGetObjectFromCache[$element_type][$element_id])
) {
return $globalCacheForGetObjectFromCache[$element_type][$element_id];
}
dol_include_once('/'.$element_prop['classpath'].'/'.$element_prop['classfile'].'.class.php');
if (class_exists($element_prop['classname'])) {
$classname = $element_prop['classname'];
$objecttmp = new $classname($db);
$className = $element_prop['classname'];
$objecttmp = new $className($db);
if ($element_id > 0 || !empty($element_ref)) {
$ret = $objecttmp->fetch($element_id, $element_ref);
@@ -12449,6 +12459,20 @@ function fetchObjectByElement($element_id, $element_type, $element_ref = '')
if (empty($objecttmp->module)) {
$objecttmp->module = $element_prop['module'];
}
if ($useCache > 0) {
if (!isset($globalCacheForGetObjectFromCache[$element_type])) {
$globalCacheForGetObjectFromCache[$element_type] = [];
}
// Manage cache limit
if (! empty($globalCacheForGetObjectFromCache[$element_type]) && is_array($globalCacheForGetObjectFromCache[$element_type]) && count($globalCacheForGetObjectFromCache[$element_type]) >= $maxCacheByType) {
array_shift($globalCacheForGetObjectFromCache[$element_type]);
}
$globalCacheForGetObjectFromCache[$element_type][$element_id] = $objecttmp;
}
return $objecttmp;
}
} else {