Improve var_export for collections

The current output of var_export for collections isn't very useful. It
outputs a call to __set_state like so:

  Map::__set_state(array(..))

This isn't particularly useful since collections don't implement the
__set_state() method, and it is also suboptimal for the design of
collections since it relies on using PHP arrays.

This diff changes var_export to use collection literal syntax for
collections instead of generating a call to __set_state().
Esse commit está contido em:
Drew Paroski
2013-05-02 17:29:29 -07:00
commit de Sara Golemon
commit 697bca0182
6 arquivos alterados com 45 adições e 31 exclusões
+15 -4
Ver Arquivo
@@ -545,7 +545,12 @@ void VariableSerializer::writeArrayHeader(int size, bool isVectorData) {
}
if (!m_objClass.empty()) {
m_buf->append(m_objClass);
m_buf->append("::__set_state(array(\n");
if (m_objCode == 'O') {
m_buf->append("::__set_state(array(\n");
} else {
assert(m_objCode == 'V' || m_objCode == 'K');
m_buf->append(" {\n");
}
} else {
m_buf->append("array (\n");
}
@@ -758,7 +763,6 @@ void VariableSerializer::writeCollectionKey(CVarRef key) {
}
void VariableSerializer::writeCollectionKeylessPrefix() {
ArrayInfo &info = m_arrayInfos.back();
switch (m_type) {
case PrintR:
case VarExport:
@@ -772,7 +776,8 @@ void VariableSerializer::writeCollectionKeylessPrefix() {
case DebuggerSerialize:
break;
case JSON:
case DebuggerDump:
case DebuggerDump: {
ArrayInfo &info = m_arrayInfos.back();
if (!info.first_element) {
m_buf->append(',');
}
@@ -781,6 +786,7 @@ void VariableSerializer::writeCollectionKeylessPrefix() {
indent();
}
break;
}
default:
assert(false);
break;
@@ -831,7 +837,12 @@ void VariableSerializer::writeArrayFooter() {
case PHPOutput:
indent();
if (info.is_object) {
m_buf->append("))");
if (m_objCode == 'O') {
m_buf->append("))");
} else {
assert(m_objCode == 'V' || m_objCode == 'K');
m_buf->append("}");
}
} else {
m_buf->append(')');
}
+4 -1
Ver Arquivo
@@ -4578,10 +4578,13 @@ void collectionSerialize(ObjectData* obj, VariableSerializer* serializer) {
serializer->writeArrayHeader(sz, true);
if (serializer->getType() == VariableSerializer::Serialize ||
serializer->getType() == VariableSerializer::APCSerialize ||
serializer->getType() == VariableSerializer::DebuggerSerialize) {
serializer->getType() == VariableSerializer::DebuggerSerialize ||
serializer->getType() == VariableSerializer::VarExport ||
serializer->getType() == VariableSerializer::PHPOutput) {
// For the 'V' serialization format, we don't print out keys
// for Serialize, APCSerialize, DebuggerSerialize
for (ArrayIter iter(obj); iter; ++iter) {
serializer->writeCollectionKeylessPrefix();
serializer->writeArrayValue(iter.second());
}
} else {
@@ -12,10 +12,10 @@ Vector Object
[1] => foo
)
[7,"foo"]
Vector::__set_state(array(
0 => 7,
1 => 'foo',
))
Vector {
7,
'foo',
}
object(Vector)#2 (2) {
[0]=>
int(7)
@@ -28,7 +28,7 @@ Vector Object
[1] => foo
)
[7,"foo"]
Vector::__set_state(array(
0 => 7,
1 => 'foo',
))
Vector {
7,
'foo',
}
@@ -9,9 +9,9 @@ Map Object
[3] => foo
)
{"3":"foo"}
Map::__set_state(array(
Map {
3 => 'foo',
))
}
object(Map)#2 (1) {
[3]=>
string(3) "foo"
@@ -21,9 +21,9 @@ Map Object
[3] => foo
)
{"3":"foo"}
Map::__set_state(array(
Map {
3 => 'foo',
))
}
------------------------
string(28) "K:3:"Map":1:{s:3:"bar";i:7;}"
------------------------
@@ -36,9 +36,9 @@ Map Object
[bar] => 7
)
{"bar":7}
Map::__set_state(array(
Map {
'bar' => 7,
))
}
object(Map)#4 (1) {
["bar"]=>
int(7)
@@ -48,6 +48,6 @@ Map Object
[bar] => 7
)
{"bar":7}
Map::__set_state(array(
Map {
'bar' => 7,
))
}
@@ -12,10 +12,10 @@ StableMap Object
[bar] => 7
)
{"3":"foo","bar":7}
StableMap::__set_state(array(
StableMap {
3 => 'foo',
'bar' => 7,
))
}
object(StableMap)#2 (2) {
[3]=>
string(3) "foo"
@@ -28,10 +28,10 @@ StableMap Object
[bar] => 7
)
{"3":"foo","bar":7}
StableMap::__set_state(array(
StableMap {
3 => 'foo',
'bar' => 7,
))
}
------------------------
string(48) "K:9:"StableMap":2:{s:3:"bar";i:7;i:3;s:3:"foo";}"
------------------------
@@ -47,10 +47,10 @@ StableMap Object
[3] => foo
)
{"bar":7,"3":"foo"}
StableMap::__set_state(array(
StableMap {
'bar' => 7,
3 => 'foo',
))
}
object(StableMap)#4 (2) {
["bar"]=>
int(7)
@@ -63,7 +63,7 @@ StableMap Object
[3] => foo
)
{"bar":7,"3":"foo"}
StableMap::__set_state(array(
StableMap {
'bar' => 7,
3 => 'foo',
))
}
@@ -14,11 +14,11 @@ object(Set)#1 (3) refcount(2){
long(2) refcount(1)
long(3) refcount(1)
}
Set::__set_state(array(
Set {
1,
2,
3,
))
}
[1,2,3]
[
1,