8000 [2.6] Towards 100% HHVM compat · symfony/symfony@d851b46 · GitHub
[go: up one dir, main page]

Skip to content

Commit d851b46

Browse files
[2.6] Towards 100% HHVM compat
1 parent 6a45129 commit d851b46

File tree

8 files changed

+89
-67
lines changed

8 files changed

+89
-67
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,34 @@ public function setCallback($callback = null)
9595
*/
9696
public function setData($data = array())
9797
{
98-
$errorHandler = null;
99-
$errorHandler = set_error_handler(function () use (&$errorHandler) {
100-
if (JSON_ERROR_NONE !== json_last_error()) {
101-
return;
102-
}
103-
104-
if ($errorHandler) {
105-
call_user_func_array($errorHandler, func_get_args());
106-
}
107-
});
108-
109-
try {
110-
// Clear json_last_error()
111-
json_encode(null);
112-
113-
$this->data = json_encode($data, $this->encodingOptions);
114-
98+
if (PHP_VERSION_ID < 50500) {
99+
// In PHP 5.3 and 5.4, PHP triggers annoying warnings for some
100+
// types that can't be serialized as JSON (INF, resources, etc.)
101+
set_error_handler('var_dump', 0);
102+
$data = @json_encode($data, $this->encodingOptions);
115103
restore_error_handler();
116-
} catch (\Exception $exception) {
117-
restore_error_handler();
118-
119-
throw $exception;
104+
} elseif (defined('HHVM_VERSION')) {
105+
// When an exception is thrown from a JsonSerializable object,
106+
// HHVM let it pass through,
107+
// whereas PHP wraps it in a new Exception.
108+
$data = json_encode($data, $this->encodingOptions);
109+
} else {
110+
try {
111+
$data = json_encode($data, $this->encodingOptions);
112+
} catch (\Exception $e) {
113+
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
114+
throw $e->getPrevious() ?: $e;
115+
}
116+
throw $e;
117+
}
120118
}
121119

122-
if (JSON_ERROR_NONE !== json_last_error()) {
120+
if (false === $data && JSON_ERROR_NONE !== json_last_error()) {
123121
throw new \InvalidArgumentException($this->transformJsonError());
124122
}
125123

124+
$this->data = $data;
125+
126126
return $this->update();
127127
}
128128

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,15 +1242,9 @@ public static function closeOutputBuffers($targetLevel, $flush)
12421242
{
12431243
$status = ob_get_status(true);
12441244
$level = count($status);
1245+
$flags = PHP_VERSION_ID >= 50400 ? PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE) : -1;
12451246

1246-
while ($level-- > $targetLevel
1247-
&& (!empty($status[$level]['del'])
1248-
|| (isset($status[$level]['flags'])
1249-
&& ($status[$level]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE)
1250-
&& ($status[$level]['flags'] & ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE))
1251-
)
1252-
)
1253-
) {
1247+
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || $flags === ($s['flags'] & $flags) : $s['del'])) {
12541248
if ($flush) {
12551249
ob_end_flush();
12561250
} else {

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@ public function testSetContent()
203203
}
204204

205205
/**
206-
* @expectedException Exception
207-
* @expectedExceptionMessage Failed calling Symfony\Component\HttpFoundation\Tests\JsonSerializableObject::jsonSerialize()
208-
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php#114688
206+
* @expectedException \Exception
207+
* @expectedExceptionMessage This error is expected
209208
*/
210209
public function testSetContentJsonSerializeError()
211210
{
@@ -224,9 +223,7 @@ class JsonSerializableObject implements \JsonSerializable
224223
{
225224
public function jsonSerialize()
226225
{
227-
trigger_error('This error is expected', E_USER_WARNING);
228-
229-
return array();
226+
throw new \Exception('This error is expected');
230227
}
231228
}
232229
}

src/Symfony/Component/VarDumper/Tests/Caster/PdoCasterTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ public function testCastPdo()
4545
'ORACLE_NULLS' => $attr['ORACLE_NULLS'],
4646
'CLIENT_VERSION' => $pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION),
4747
'SERVER_VERSION' => $pdo->getAttribute(\PDO::ATTR_SERVER_VERSION),
48-
'STATEMENT_CLASS' => array(
49-
'PDOStatement',
50-
array($pdo),
51-
),
48+
'STATEMENT_CLASS' => array('PDOStatement'),
5249
'DEFAULT_FETCH_MODE' => $attr['DEFAULT_FETCH_MODE'],
5350
),
5451
);
52+
unset($cast["\0~\0attributes"]['STATEMENT_CLASS'][1]);
5553

5654
$this->assertSame($xCast, $cast);
5755
}

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testGet()
2828
$cloner = new VarCloner();
2929
$cloner->addCasters(array(
3030
':stream' => function ($res, $a) {
31-
unset($a['uri']);
31+
unset($a['uri'], $a['wrapper_data']);
3232

3333
return $a;
3434
},
@@ -40,12 +40,12 @@ public function testGet()
4040
$out = ob_get_clean();
4141
$out = preg_replace('/[ \t]+$/m', '', $out);
4242
$intMax = PHP_INT_MAX;
43-
$res1 = (int) $var['res'];
44-
$res2 = (int) $var[8];
43+
$res = (int) $var['res'];
4544

45+
$r = defined('HHVM_VERSION') ? '' : '#%d';
4646
$this->assertStringMatchesFormat(
4747
<<<EOTXT
48-
array:25 [
48+
array:24 [
4949
"number" => 1
5050
0 => &1 null
5151
"const" => 1.1
@@ -58,7 +58,7 @@ public function testGet()
5858
"str" => "déjà\\n"
5959
7 => b"é\\x00"
6060
"[]" => []
61-
"res" => stream resource {@{$res1}
61+
"res" => stream resource {@{$res}
6262
wrapper_type: "plainfile"
6363
stream_type: "STDIO"
6464
mode: "r"
@@ -69,12 +69,11 @@ public function testGet()
6969
eof: false
7070
options: []
7171
}
72-
8 => Unknown resource @{$res2}
7372
"obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
7473
+foo: "foo"
7574
+"bar": "bar"
7675
}
77-
"closure" => Closure {#%d
76+
"closure" => Closure {{$r}
7877
reflection: """
7978
Closure [ <user%S> %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {\\n
8079
@@ {$var['file']} {$var['line']} - {$var['line']}\\n
@@ -93,14 +92,43 @@ public function testGet()
9392
"recurs" => &4 array:1 [
9493
0 => &4 array:1 [&4]
9594
]
96-
9 => &1 null
95+
8 => &1 null
9796
"sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d}
9897
"snobj" => &3 {#%d}
9998
"snobj2" => {#%d}
10099
"file" => "{$var['file']}"
101100
b"bin-key-é" => ""
102101
]
103102
103+
EOTXT
104+
,
105+
$out
106+
);
107+
}
108+
109+
public function testClosedResource()
110+
{
111+
if (defined('HHVM_VERSION') && HHVM_VERSION_ID < 30600) {
112+
$this->markTestSkipped();
113+
}
114+
115+
$var = fopen(__FILE__, 'r');
116+
fclose($var);
117+
118+
$dumper = new CliDumper('php://output');
119+
$dumper->setColors(false);
120+
$cloner = new VarCloner();
121+
$data = $cloner->cloneVar($var);
122+
123+
ob_start();
124+
$dumper->dump($data);
125+
$out = ob_get_clean();
126+
$res = (int) $var;
127+
128+
$this->assertStringMatchesFormat(
129+
<<<EOTXT
130+
Unknown resource @{$res}
131+
104132
EOTXT
105133
,
106134
$out
@@ -114,6 +142,13 @@ public function testThrowingCaster()
114142
$dumper = new CliDumper();
115143
$dumper->setColors(false);
116144
$cloner = new VarCloner();
145+
$cloner->addCasters(array(
146+
':stream' => function ($res, $a) {
147+
unset($a['wrapper_data']);
148+
149+
return $a;
150+
},
151+
));
117152
$cloner->addCasters(array(
118153
':stream' => function () {
119154
throw new \Exception('Foobar');
@@ -128,24 +163,25 @@ public function testThrowingCaster()
128163
rewind($out);
129164
$out = stream_get_contents($out);
130165

166+
$r = defined('HHVM_VERSION') ? '' : '#%d';
131167
$this->assertStringMatchesFormat(
132168
<<<EOTXT
133169
stream resource {@{$ref}
134170
wrapper_type: "PHP"
135171
stream_type: "MEMORY"
136-
mode: "w+b"
172+
mode: "%s+b"
137173
unread_bytes: 0
138174
seekable: true
139175
uri: "php://memory"
140176
timed_out: false
141177
blocked: true
142178
eof: false
143179
options: []
144-
⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {#%d
180+
⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {{$r}
145181
#message: "Unexpected Exception thrown from a caster: Foobar"
146182
trace: array:1 [
147183
0 => array:2 [
148-
"call" => "%s{closure}()"
184+
"call" => "%slosure%s()"
149185
"file" => "{$file}:{$line}"
150186
]
151187
]
@@ -173,9 +209,10 @@ public function testRefsInProperties()
173209
rewind($out);
174210
$out = stream_get_contents($out);
175211

212+
$r = defined('HHVM_VERSION') ? '' : '#%d';
176213
$this->assertStringMatchesFormat(
177214
<<<EOTXT
178-
{#%d
215+
{{$r}
179216
+"foo": &1 "foo"
180217
+"bar": &1 "foo"
181218
}

src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ class DumbFoo
1313
$foo->bar = 'bar';
1414

1515
$g = fopen(__FILE__, 'r');
16-
$h = fopen(__FILE__, 'r');
17-
fclose($h);
1816

1917
$var = array(
2018
'number' => 1, null,
2119
'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX,
2220
'str' => "déjà\n", "\xE9\x00",
2321
'[]' => array(),
2422
'res' => $g,
25-
$h,
2623
'obj' => $foo,
2724
'closure' => function ($a, \PDO &$b = null) {},
2825
'line' => __LINE__ - 1,
@@ -40,4 +37,4 @@ class DumbFoo
4037
$var['file'] = __FILE__;
4138
$var["bin-key-\xE9"] = '';
4239

43-
unset($g, $h, $r);
40+
unset($g, $r);

src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testGet()
2929
$cloner = new VarCloner();
3030
$cloner->addCasters(array(
3131
':stream' => function ($res, $a) {
32-
unset($a['uri']);
32+
unset($a['uri'], $a['wrapper_data']);
3333

3434
return $a;
3535
},
@@ -44,12 +44,12 @@ public function testGet()
4444
$intMax = PHP_INT_MAX;
4545
preg_match('/sf-dump-\d+/', $out, $dumpId);
4646
$dumpId = $dumpId[0];
47-
$res1 = (int) $var['res'];
48-
$res2 = (int) $var[8];
47+
$res = (int) $var['res'];
4948

49+
$r = defined('HHVM_VERSION') ? '' : '<a class=sf-dump-ref>#%d</a>';
5050
$this->assertStringMatchesFormat(
5151
<<<EOTXT
52-
<foo></foo><bar><span class=sf-dump-note>array:25</span> [<samp& 10000 gt;
52+
<foo></foo><bar><span class=sf-dump-note>array:24</span> [<samp>
5353
"<span class=sf-dump-key>number</span>" => <span class=sf-dump-num>1</span>
5454
<span class=sf-dump-key>0</span> => <a class=sf-dump-ref href=#{$dumpId}-ref01 title="2 occurrences">&amp;1</a> <span class=sf-dump-const>null</span>
5555
"<span class=sf-dump-key>const</span>" => <span class=sf-dump-num>1.1</span>
@@ -62,7 +62,7 @@ public function testGet()
6262
"<span class=sf-dump-key>str</span>" => "<span class=sf-dump-str title="5 characters">d&#233;j&#224;</span>\\n"
6363
<span class=sf-dump-key>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;</span>\\x00"
6464
"<span class=sf-dump-key>[]</span>" => []
65-
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res1}</a><samp>
65+
"<span class=sf-dump-key>res</span>" => <span class=sf-dump-note>stream resource</span> <a class=sf-dump-ref>@{$res}</a><samp>
6666
<span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"
6767
<span class=sf-dump-meta>stream_type</span>: "<span class=sf-dump-str title="5 characters">STDIO</span>"
6868
<span class=sf-dump-meta>mode</span>: "<span class=sf-dump-str>r</span>"
@@ -73,12 +73,11 @@ public function testGet()
7373
<span class=sf-dump-meta>eof</span>: <span class=sf-dump-const>false</span>
7474
<span class=sf-dump-meta>options</span>: []
7575
</samp>}
76-
<span class=sf-dump-key>8</span> => <span class=sf-dump-note>Unknown resource</span> <a class=sf-dump-ref>@{$res2}</a>
7776
"<span class=sf-dump-key>obj</span>" => <abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="2 occurrences">#%d</a><samp id={$dumpId}-ref2%d>
7877
+<span class=sf-dump-public title="Public property">foo</span>: "<span class=sf-dump-str title="3 characters">foo</span>"
7978
+"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"
8079
</samp>}
81-
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-ref>#%d</a><samp>
80+
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {{$r}<samp>
8281
<span class=sf-dump-meta>reflection</span>: """
8382
<span class=sf-dump-str title="%d characters">Closure [ &lt;user%S&gt; %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {</span>\\n
8483
<span class=sf-dump-str title="%d characters"> @@ {$var['file']} {$var['line']} - {$var['line']}</span>\\n
@@ -97,7 +96,7 @@ public function testGet()
9796
"<span class=sf-dump-key>recurs</span>" => <a class=sf-dump-ref href=#{$dumpId}-ref04 title="2 occurrences">&amp;4</a> <span class=sf-dump-note>array:1</span> [<samp id={$dumpId}-ref04>
9897
<span class=sf-dump-index>0</span> => <a class=sf-dump-ref href=#{$dumpId}-ref04 title="2 occurrences">&amp;4</a> <span class=sf-dump-note>array:1</span> [<a class=sf-dump-ref href=#{$dumpId}-ref04 title="2 occurrences">&amp;4</a>]
9998
</samp>]
100-
<span class=sf-dump-key>9</span> => <a class=sf-dump-ref href=#{$dumpId}-ref01 title="2 occurrences">&amp;1</a> <span class=sf-dump-const>null</span>
99+
<span class=sf-dump-key>8</span> => <a class=sf-dump-ref href=#{$dumpId}-ref01 title="2 occurrences">&amp;1</a> <span class=sf-dump-const>null</span>
101100
"<span class=sf-dump-key>sobj</span>" => <abbr title="Symfony\Component\VarDumper\Tests\Fixture\DumbFoo" class=sf-dump-note>DumbFoo</abbr> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="2 occurrences">#%d</a>}
102101
"<span class=sf-dump-key>snobj</span>" => <a class=sf-dump-ref href=#{$dumpId}-ref03 title="2 occurrences">&amp;3</a> {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="3 occurrences">#%d</a>}
103102
"<span class=sf-dump-key>snobj2</span>" => {<a class=sf-dump-ref href=#{$dumpId}-ref2%d title="3 occurrences">#%d</a>}

src/Symfony/Component/VarDumper/Tests/VarClonerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testClone()
8181
[class] => stdClass
8282
[value] =>
8383
[cut] => 0
84-
[handle] => %d
84+
[handle] => %i
8585
[refCount] => 0
8686
[position] => 1
8787
)
@@ -96,7 +96,7 @@ public function testClone()
9696
[class] => stdClass
9797
[value] =>
9898
[cut] => 0
99-
[handle] => %d
99+
[handle] => %i
100100
[refCount] => 0
101101
[position] => 2
102102
)
@@ -107,7 +107,7 @@ public function testClone()
107107
[class] => stdClass
108108
[value] =>
109109
[cut] => 0
110-
[handle] => %d
110+
[handle] => %i
111111
[refCount] => 0
112112
[position] => 3
113113
)

0 commit comments

Comments
 (0)
0