Implement own minimalistic var_export and drop the one from PEAR.

This commit is contained in:
Michal Čihař
2006-07-20 13:47:07 +00:00
parent 506e938ff0
commit aa73d33cf6
3 changed files with 45 additions and 153 deletions

View File

@@ -6,6 +6,10 @@ $Id$
$Source$ $Source$
2006-07-20 Michal Čihař <michal@cihar.com>
* libraries/compat/var_export.php: Dropped due to license problems.
* scripts/setup.php: Implement own var_export.
2006-07-19 Marc Delisle <lem9@users.sourceforge.net> 2006-07-19 Marc Delisle <lem9@users.sourceforge.net>
* browse_foreigners.php: bug #1525393, no page selector in foreign key * browse_foreigners.php: bug #1525393, no page selector in foreign key
browse page browse page

View File

@@ -1,145 +0,0 @@
<?php
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Aidan Lister <aidan@php.net> |
// +----------------------------------------------------------------------+
//
// $Id: var_export.php,v 1.18 2006/05/22 00:09:42 arpad Exp $
/**
* Replace var_export()
*
* @category PHP
* @package PHP_Compat
* @link http://php.net/function.var_export
* @author Aidan Lister <aidan@php.net>
* @version $Revision: 1.18 $
* @since PHP 4.2.0
* @require PHP 4.0.0 (user_error)
*/
function php_compat_var_export($var, $return = false, $level = 0, $inObject = false)
{
// Init
$indent = ' ';
$doublearrow = ' => ';
$lineend = ",\n";
$stringdelim = '\'';
$newline = "\n";
$find = array(null, '\\', '\'');
$replace = array('NULL', '\\\\', '\\\'');
$out = '';
// Indent
$level++;
for ($i = 1, $previndent = ''; $i < $level; $i++) {
$previndent .= $indent;
}
$varType = gettype($var);
// Handle object indentation oddity
if ($inObject && $varType != 'object') {
$previndent = substr($previndent, 0, -1);
}
// Handle each type
switch ($varType) {
// Array
case 'array':
if ($inObject) {
$out .= $newline . $previndent;
}
$out .= 'array (' . $newline;
foreach ($var as $key => $value) {
// Key
if (is_string($key)) {
// Make key safe
$key = str_replace($find, $replace, $key);
$key = $stringdelim . $key . $stringdelim;
}
// Value
if (is_array($value)) {
$export = php_compat_var_export($value, true, $level);
$value = $newline . $previndent . $indent . $export;
} else {
$value = php_compat_var_export($value, true, $level);
}
// Piece line together
$out .= $previndent . $indent . $key . $doublearrow . $value . $lineend;
}
// End string
$out .= $previndent . ')';
break;
// String
case 'string':
// Make the string safe
for ($i = 0, $c = count($find); $i < $c; $i++) {
$var = str_replace($find[$i], $replace[$i], $var);
}
$out = $stringdelim . $var . $stringdelim;
break;
// Number
case 'integer':
case 'double':
$out = (string) $var;
break;
// Boolean
case 'boolean':
$out = $var ? 'true' : 'false';
break;
// NULLs
case 'NULL':
case 'resource':
$out = 'NULL';
break;
// Objects
case 'object':
// Start the object export
$out = $newline . $previndent;
$out .= get_class($var) . '::__set_state(array(' . $newline;
// Export the object vars
foreach(get_object_vars($var) as $key => $value) {
$out .= $previndent . $indent . ' ' . $stringdelim . $key . $stringdelim . $doublearrow;
$out .= php_compat_var_export($value, true, $level, true) . $lineend;
}
$out .= $previndent . '))';
break;
}
// Method of output
if ($return === true) {
return $out;
} else {
echo $out;
}
}
// Define
if (!function_exists('var_export')) {
function var_export($var, $return = false)
{
return php_compat_var_export($var, $return);
}
}

View File

@@ -9,9 +9,6 @@ define( 'PMA_MINIMUM_COMMON', TRUE );
chdir('..'); chdir('..');
require_once('./libraries/common.lib.php'); require_once('./libraries/common.lib.php');
// var_export for older PHP
require_once('./libraries/compat/var_export.php');
// Grab configuration defaults // Grab configuration defaults
$PMA_Config = new PMA_Config(); $PMA_Config = new PMA_Config();
@@ -411,6 +408,39 @@ function get_server_name($val, $id = FALSE) {
return $ret; return $ret;
} }
/**
* Exports variable to PHP code, very limited version of var_export
*
* @param string data to export
*
* @see var_export
*
* @return string PHP code containing variable value
*/
function PMA_var_export($input) {
$output = '';
if (is_null($input)) {
$output .= 'NULL';
} elseif (is_array($input)) {
$output .= "array (\n";
foreach($input as $key => $value) {
$output .= PMA_var_export($key) . ' => ' . PMA_var_export($value);
$output .= ",\n";
}
$output .= ')';
} elseif (is_string($input)) {
$output .= '\'' . addslashes($input) . '\'';
} elseif (is_int($input) || is_double($input)) {
$output .= (string) $input;
} elseif (is_bool($input)) {
$output .= $input ? 'true' : 'false';
} else {
die('Unknown type for PMA_var_export: ' . $input);
}
return $output;
}
/** /**
* Creates configuration code for one variable * Creates configuration code for one variable
* *
@@ -432,23 +462,26 @@ function get_cfg_val($name, $val) {
$ret .= $name . " = array(\n"; $ret .= $name . " = array(\n";
} else { } else {
// Something unknown... // Something unknown...
$ret .= $name. ' = ' . var_export($val, TRUE) . ";\n"; $ret .= $name. ' = ' . PMA_var_export($val) . ";\n";
break; break;
} }
} }
if ($type == 'string') { if ($type == 'string') {
$ret .= $name. "['$k'] = " . var_export($v, TRUE) . ";\n"; $ret .= get_cfg_val($name . "['$k']", $v);
} elseif ($type == 'int') { } elseif ($type == 'int') {
$ret .= " " . var_export($v, TRUE) . ",\n"; $ret .= " " . PMA_var_export($v) . ",\n";
} }
} }
if ($type == 'int') { if (!isset($type)) {
/* Empty array */
$ret .= $name . " = array();\n";
} elseif ($type == 'int') {
$ret .= ");\n"; $ret .= ");\n";
} }
$ret .= "\n"; $ret .= "\n";
unset($type); unset($type);
} else { } else {
$ret .= $name . ' = ' . var_export($val, TRUE) . ";\n"; $ret .= $name . ' = ' . PMA_var_export($val) . ";\n";
} }
return $ret; return $ret;
} }