Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15413499
D9923.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D9923.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Thu, Mar 20, 7:12 PM (18 h, 57 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7421989
Default Alt Text
D9923.diff (2 KB)
Attached To
Mode
D9923: Add a `phutil_var_export` function
Attached
Detach File
Event Timeline
Log In to Comment