/** * Various dev/debug helpers * TODO: cleanup or at least re-organize * * @copyright Copyright (C) 2010-2012 Combodo SARL * @license http://opensource.org/licenses/AGPL-3.0 */ /** * MyHelpers * * @package iTopORM */ class MyHelpers { public static function CheckValueInArray($sDescription, $value, $aData) { if (!in_array($value, $aData)) { self::HandleWrongValue($sDescription, $value, $aData); } } public static function CheckKeyInArray($sDescription, $key, $aData) { if (!array_key_exists($key, $aData)) { self::HandleWrongValue($sDescription, $key, array_keys($aData)); } } public static function HandleWrongValue($sDescription, $value, $aData) { if (count($aData) == 0) { $sArrayDesc = "{}"; } else { $sArrayDesc = "{".implode(", ", $aData)."}"; } // exit! throw new CoreException("Wrong value for $sDescription, found '$value' while expecting a value in $sArrayDesc"); } // getmicrotime() // format sss.mmmuuupppnnn public static function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } /* * MakeSQLComment * converts hash into text comment which we can use in a (mySQL) query */ public static function MakeSQLComment ($aHash) { if (empty($aHash)) return ""; $sComment = ""; { foreach($aHash as $sKey=>$sValue) { $sComment .= "\n-- ". $sKey ."=>" . $sValue; } } return $sComment; } public static function var_dump_html($aWords, $bFullDisplay = false) { echo "
\n"; if ($bFullDisplay) { print_r($aWords); // full dump! } else { var_dump($aWords); // truncate things when they are too big } echo "\n\n"; } public static function arg_dump_html() { echo "
\n"; echo "GET:\n"; var_dump($_GET); echo "POST:\n"; var_dump($_POST); echo "\n\n"; } public static function var_dump_string($var) { ob_start(); print_r($var); $sRet = ob_get_clean(); return $sRet; } protected static function first_diff_line($s1, $s2) { $aLines1 = explode("\n", $s1); $aLines2 = explode("\n", $s2); for ($i = 0 ; $i < min(count($aLines1), count($aLines2)) ; $i++) { if ($aLines1[$i] != $aLines2[$i]) return $i; } return false; } protected static function highlight_line($sMultiline, $iLine, $sHighlightStart = '', $sHightlightEnd = '') { $aLines = explode("\n", $sMultiline); $aLines[$iLine] = $sHighlightStart.$aLines[$iLine].$sHightlightEnd; return implode("\n", $aLines); } protected static function first_diff($s1, $s2) { // do not work fine with multiline strings $iLen1 = strlen($s1); $iLen2 = strlen($s2); for ($i = 0 ; $i < min($iLen1, $iLen2) ; $i++) { if ($s1[$i] !== $s2[$i]) return $i; } return false; } protected static function last_diff($s1, $s2) { // do not work fine with multiline strings $iLen1 = strlen($s1); $iLen2 = strlen($s2); for ($i = 0 ; $i < min(strlen($s1), strlen($s2)) ; $i++) { if ($s1[$iLen1 - $i - 1] !== $s2[$iLen2 - $i - 1]) return array($iLen1 - $i, $iLen2 - $i); } return false; } protected static function text_cmp_html($sText1, $sText2, $sHighlight) { $iDiffPos = self::first_diff_line($sText1, $sText2); $sDisp1 = self::highlight_line($sText1, $iDiffPos, '
$sDisp1 | \n";
echo "$sDisp2 | \n";
echo "
$sStart$sMiddle1$sEnd
\n"; echo "$sStart$sMiddle2$sEnd
\n"; } protected static function object_cmp_html($oObj1, $oObj2, $sHighlight) { $sObj1 = self::var_dump_string($oObj1); $sObj2 = self::var_dump_string($oObj2); return self::text_cmp_html($sObj1, $sObj2, $sHighlight); } public static function var_cmp_html($var1, $var2, $sHighlight = 'color:red; font-weight:bold;') { if (is_object($var1)) { return self::object_cmp_html($var1, $var2, $sHighlight); } else if (count(explode("\n", $var1)) > 1) { // multiline string return self::text_cmp_html($var1, $var2, $sHighlight); } else { return self::string_cmp_html($var1, $var2, $sHighlight); } } public static function get_callstack_html($iLevelsToIgnore = 0, $aCallStack = null) { if ($aCallStack == null) $aCallStack = debug_backtrace(); $aCallStack = array_slice($aCallStack, $iLevelsToIgnore); $aDigestCallStack = array(); $bFirstLine = true; foreach ($aCallStack as $aCallInfo) { $sLine = empty($aCallInfo['line']) ? "" : $aCallInfo['line']; $sFile = empty($aCallInfo['file']) ? "" : $aCallInfo['file']; $sClass = empty($aCallInfo['class']) ? "" : $aCallInfo['class']; $sType = empty($aCallInfo['type']) ? "" : $aCallInfo['type']; $sFunction = empty($aCallInfo['function']) ? "" : $aCallInfo['function']; if ($bFirstLine) { $bFirstLine = false; // For this line do not display the "function name" because // that will be the name of our error handler for sure ! $sFunctionInfo = "N/A"; } else { $args = ''; if (empty($aCallInfo['args'])) $aCallInfo['args'] = array(); foreach ($aCallInfo['args'] as $a) { if (!empty($args)) { $args .= ', '; } switch (gettype($a)) { case 'integer': case 'double': $args .= $a; break; case 'string': $a = Str::pure2html(self::beautifulstr($a, 1024, true, true)); $args .= "\"$a\""; break; case 'array': $args .= 'Array('.count($a).')'; break; case 'object': $args .= 'Object('.get_class($a).')'; break; case 'resource': $args .= 'Resource('.strstr($a, '#').')'; break; case 'boolean': $args .= $a ? 'True' : 'False'; break; case 'NULL': $args .= 'Null'; break; default: $args .= 'Unknown'; } } $sFunctionInfo = "$sClass $sType $sFunction($args)"; } $aDigestCallStack[] = array('File'=>$sFile, 'Line'=>$sLine, 'Function'=>$sFunctionInfo); } return self::make_table_from_assoc_array($aDigestCallStack); } public static function dump_callstack($iLevelsToIgnore = 0, $aCallStack = null) { return self::get_callstack_html($iLevelsToIgnore, $aCallStack); } /////////////////////////////////////////////////////////////////////////////// // Source: New // Last modif: 2004/12/20 RQU /////////////////////////////////////////////////////////////////////////////// public static function make_table_from_assoc_array(&$aData) { if (!is_array($aData)) throw new CoreException("make_table_from_assoc_array: Error - the passed argument is not an array"); $aFirstRow = reset($aData); if (count($aData) == 0) return ''; if (!is_array($aFirstRow)) throw new CoreException("make_table_from_assoc_array: Error - the passed argument is not a bi-dimensional array"); $sOutput = ""; $sOutput .= "".$fieldname." | \n"; } $sOutput .= "
".$data." | \n"; } $sOutput .= "