Last active
July 31, 2023 10:01
-
-
Save EscApp2/230e8dddc9148a216c0314aa88f3e516 to your computer and use it in GitHub Desktop.
bitrix wrap php cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
if(!function_exists("wrapPhpCache")){ | |
function wrapPhpCache(){ | |
//$MainParam, $Callable, $FunctionParam = false | |
//$Callable, $MainParam, $FunctionParam = false | |
//$MainParam, $Callable, $FunctionParam1, $FunctionParam2 .... | |
$arArgs = func_get_args(); | |
$_MParams = $arArgs[0]; | |
$_MCallable = $arArgs[1]; | |
if(is_callable($_MParams) | |
&& (is_array($_MCallable) || is_string($_MCallable)) | |
){ | |
$Callable = $_MParams; | |
$MainParam = $_MCallable; | |
}elseif(is_callable($_MCallable) | |
&& (is_array($_MParams) || is_string($_MParams)) | |
){ | |
$Callable = $_MCallable; | |
$MainParam = $_MParams; | |
} | |
$FunctionParam = $arArgs[2]; | |
if(count($arArgs)>3){ | |
$arArgs2 = $arArgs; | |
unset($arArgs2[0],$arArgs2[1]); | |
$FunctionParam = array_values($arArgs2); | |
} | |
$OutMainParam = array(); | |
if(empty($Callable)){ | |
if(is_array($MainParam)){ | |
if($MainParam['d']){ | |
echo \Bitrix\Main\Diag\Debug::dump("not callable function 0",'',true); | |
} | |
} | |
return false; | |
} | |
$OutMainParam['call'] = $Callable; | |
static $CLOSURE_ID; | |
if(empty($CLOSURE_ID)){ | |
$CLOSURE_ID = 0; | |
} | |
if(is_array($MainParam)){ | |
if(!isset($MainParam['name'])){ | |
if(is_callable($OutMainParam['call']) && !is_string($OutMainParam['call'])){ | |
$reflection = new ReflectionFunction($OutMainParam['call']); | |
$file = $reflection->getFileName(); | |
$begin = $reflection->getStartLine(); | |
$end = $reflection->getEndline(); | |
if($begin != $end){ | |
$OutMainParam['name'] = 'reflection_'.$file."_".$begin."_".$end; | |
}else{ | |
$CLOSURE_ID++; | |
$OutMainParam['name'] = 'reflection_'.$file."_".$begin."_".$end."_".$CLOSURE_ID; | |
} | |
}elseif(is_callable($OutMainParam['call']) && is_string($OutMainParam['call'])){ | |
$OutMainParam['name'] = $OutMainParam['call']; | |
}else{ | |
if($MainParam['d']){ | |
echo \Bitrix\Main\Diag\Debug::dump("no name",'',true); | |
} | |
return false; | |
} | |
}else{ | |
$OutMainParam['name'] = $MainParam['name']; | |
} | |
}else{ | |
$OutMainParam['name'] = $MainParam; | |
} | |
if(is_array($MainParam)){ | |
if(isset($MainParam['time'])){ | |
$OutMainParam['time'] = (int)$MainParam['time']; | |
} | |
if(isset($MainParam['dir'])){ | |
$OutMainParam['dir'] = (string)$MainParam['dir']; | |
} | |
if(isset($MainParam['d'])){ | |
$OutMainParam['d'] = true; | |
} | |
if(isset($MainParam['c'])){ | |
$OutMainParam['c'] = true; | |
} | |
if(isset($MainParam['tag'])){ | |
if(!is_array($MainParam['tag'])){ | |
$arTMP = explode(" ",$MainParam['tag']); | |
if(count($arTMP)>1){ | |
$MainParam['tag'] = $arTMP; | |
}else{ | |
$MainParam['tag'] = array($MainParam['tag']); | |
} | |
} | |
$arTAG = array(); | |
foreach($MainParam['tag'] as $TAG){ | |
if(is_numeric($TAG)){ | |
//$MainParam['tag'] need iblock_id | |
$arTAG[] = "iblock_id_".intval($TAG); | |
}else{ | |
$arTAG[] = $TAG; | |
} | |
} | |
$OutMainParam['tag'] = implode(" ",$arTAG); | |
} | |
} | |
if(empty($OutMainParam['call'])){ | |
if($OutMainParam['d']){ | |
echo \Bitrix\Main\Diag\Debug::dump("not callable function",'',true); | |
} | |
return false; | |
} | |
if(empty($OutMainParam['name'])){ | |
if($OutMainParam['d']){ | |
echo \Bitrix\Main\Diag\Debug::dump("no name",'',true); | |
} | |
return false; | |
} | |
if(empty($OutMainParam['time'])){ | |
$OutMainParam['time'] = 24*3600; | |
} | |
$name = str_replace(array("-","::","/","\\",":","'"," ",".",","),"_",$OutMainParam['name']); | |
if(empty($OutMainParam['dir'])){ | |
$tag = ""; | |
if(!empty($OutMainParam['tag'])){ | |
$tag = "tag_".str_replace(" ","_",$OutMainParam['tag'])."/"; | |
} | |
$OutMainParam['dir'] = "/custom/func/".$tag.$name."/"; | |
} | |
if(empty($FunctionParam)){ | |
$serialize = $OutMainParam; | |
unset($serialize['call']); | |
$uniq_string = serialize($serialize); | |
}else{ | |
$uniq_string = $name."_".serialize($FunctionParam); | |
} | |
if($OutMainParam['c']){ | |
BXClearCache(true, $OutMainParam['dir']); | |
} | |
$obCache = new CPHPCache(); | |
if ($obCache->InitCache($OutMainParam['time'], $uniq_string, $OutMainParam['dir'])) | |
{ | |
$arCache = $obCache->GetVars(); | |
if($OutMainParam['d']){ | |
echo \Bitrix\Main\Diag\Debug::dump("cache",'',true); | |
} | |
} | |
elseif ($obCache->StartDataCache()) | |
{ | |
if($OutMainParam['d']){ | |
echo \Bitrix\Main\Diag\Debug::dump('no-cache','',true); | |
} | |
if($OutMainParam['tag']){ | |
global $CACHE_MANAGER; | |
$CACHE_MANAGER->StartTagCache($OutMainParam['dir']); | |
} | |
$arCache = array(); | |
if(is_array($FunctionParam)){ | |
$result = call_user_func_array($OutMainParam['call'],$FunctionParam); | |
}else{ | |
$result = call_user_func($OutMainParam['call']); | |
} | |
$arCache = $result; | |
if($OutMainParam['tag']){ | |
$arTags = explode(" ",$OutMainParam['tag']); | |
foreach($arTags as $tag){ | |
$CACHE_MANAGER->RegisterTag($tag); | |
} | |
$CACHE_MANAGER->EndTagCache(); | |
} | |
$obCache->EndDataCache($arCache); | |
} | |
if (!isset($arCache)) | |
$arCache = array(); | |
return $arCache; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment