Page MenuHomePhabricator

D9923.diff
No OneTemporary

D9923.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -435,6 +435,7 @@
'phutil_utf8v' => 'utils/utf8.php',
'phutil_utf8v_codepoints' => 'utils/utf8.php',
'phutil_utf8v_combined' => 'utils/utf8.php',
+ 'phutil_var_export' => 'utils/utils.php',
'ppull' => 'utils/utils.php',
'qsprintf' => 'xsprintf/qsprintf.php',
'queryfx' => 'xsprintf/queryfx.php',
diff --git a/src/utils/__tests__/PhutilUtilsTestCase.php b/src/utils/__tests__/PhutilUtilsTestCase.php
--- a/src/utils/__tests__/PhutilUtilsTestCase.php
+++ b/src/utils/__tests__/PhutilUtilsTestCase.php
@@ -565,4 +565,29 @@
}
}
+ public function testVarExport() {
+ $this->assertEqual(
+ 'array()',
+ phutil_var_export(array()));
+ $this->assertEqual(
+ implode("\n", array(
+ 'array(',
+ ' 0 => 1,',
+ ' 1 => 2,',
+ ' 2 => 3,',
+ ')',
+ )),
+ phutil_var_export(array(1, 2, 3)));
+ $this->assertEqual(
+ implode("\n", array(
+ 'array(',
+ " 0 => 'foo',",
+ " 'bar' => array(",
+ " 'baz' => stdClass::__set_state(array()),",
+ ' ),',
+ ')',
+ )),
+ phutil_var_export(array('foo', 'bar' => array('baz' => new stdClass()))));
+ }
+
}
diff --git a/src/utils/utils.php b/src/utils/utils.php
--- a/src/utils/utils.php
+++ b/src/utils/utils.php
@@ -1071,3 +1071,22 @@
function phutil_censor_credentials($string) {
return preg_replace(',(?<=://)([^/@\s]+)(?=@|$),', 'xxxxx', $string);
}
+
+
+/**
+ * Returns a parsable string representation of a variable.
+ *
+ * @param wild The variable you want to export.
+ * @return string
+ */
+function phutil_var_export($var) {
+ $regex = array(
+ '/\s+$/m' => '',
+ "/=>\n\s+/" => '=> ',
+ "/array\s*\(\n\s*\)/" => 'array()',
+ '/array \(/' => 'array(',
+ );
+
+ $var = var_export($var, true);
+ return preg_replace(array_keys($regex), array_values($regex), $var);
+}

File Metadata

Mime Type
text/plain
Expires
Tue, May 14, 11:12 PM (2 w, 3 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6274303
Default Alt Text
D9923.diff (2 KB)

Event Timeline