aquí está mi enfoque.
crea un archivo php temporal y lo incluye.
pero de esta manera si el código que desea ejecutar esta función tiene programa termina errores antes de retirar archivo temporal
así que hago un procedimiento de limpieza automática de la función. de esta forma, limpia los archivos temporales viejos por un tiempo de espera cada vez que se ejecuta la función. puede configurar el tiempo de espera o deshabilitarlo desde las opciones al inicio de la función
También he agregado la opción de ignorar error para resolver archivos temporales no eliminados. si se ignoran los errores, el programa continuará y eliminará el archivo temporal.
También algunos proyectos tienen que deshabilitar la limpieza automática porque escanea todo el directorio cada vez que se ejecuta. podría dañar el rendimiento del disco.
function eval2($c) {
$auto_clean_old_temporary_files=false; //checks old temporary eval2 files for this spesific temporary file names generated by settings below
$ignore_all_errors=true; //if you ignore errors you can remove temporary files even there is an error
$tempfiledirectory=''; //temporary file directory
$tempfileheader='eval2_'; // temporary file header
$tempfiletimeseperator='__'; // temporary file seperator for time
$tempfileremovetimeout=200; // temp file cleaning time in seconds
if ($auto_clean_old_temporary_files===true) {
$sd=scandir('.'); //scaning for old temporary files
foreach ($sd as $sf) {
if (strlen($sf)>(32+strlen($tempfileheader)+strlen($tempfiletimeseperator)+3)) { // if filename long enough
$t1=substr($sf,(32+strlen($tempfileheader)),strlen($tempfiletimeseperator)); //searching time seperator
$t2=substr($sf,0,strlen($tempfileheader)); //searching file header
if ($t1==$tempfiletimeseperator && $t2==$tempfileheader) { //checking for timeseperator and file name header
$ef=explode('.',$sf);
unset($ef[count($ef)]);//removing file extension
$nsf=implode('.',$ef);//joining file name without extension
$ef=explode($tempfiletimeseperator,$nsf);
$tm=(int)end($ef); //getting time from filename
$tmf=time()-$tm;
if ($tmf>$tempfileremovetimeout && $tmf<123456 && $tmf>0) { // if time passed more then timeout and difference with real time is logical
unlink($sf); // finally removing temporary file
}
}
}
}
}
$n=$tempfiledirectory.$tempfileheader . md5(microtime().rand(0,5000)). $tempfiletimeseperator . time() .'.php'; //creating spesific temporary file name
$c='<?php' . PHP_EOL . $c . PHP_EOL; //generating php content
file_put_contents($n,$c); //creating temporary file
if ($ignore_all_errors===true) { // including temporary file by your choise
[email protected]($n);
}else{
$s=include($n);
}
return $s;
}
Gracias por esta pregunta. Me ayudó con esto: https://github.com/tedivm/Stash/pull/135 – CMCDragonkai