Skip to content

Commit 5aaf4e5

Browse files
Merge pull request #11 from mdeheij/splitHttpAcceptHeader
PHP7 consistent ordering for splitHttpAcceptHeader
2 parents c2da6e3 + 865bb94 commit 5aaf4e5

File tree

5 files changed

+182
-26
lines changed

5 files changed

+182
-26
lines changed

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
php:
3+
- 7.0
4+
- 5.6
5+
- hhvm
6+
7+
before_script:
8+
- composer install --dev
9+
10+
script:
11+
- vendor/bin/phpunit

composer.json

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
11
{
2-
"name": "symfony/symfony1",
2+
"name": "symfony/symfony1",
33
"description": "Symfony is a complete framework designed to optimize the development of web applications by way of several key features. For starters, it separates a web application's business rules, server logic, and presentation views. It contains numerous tools and classes aimed at shortening the development time of a complex web application. Additionally, it automates common tasks so that the developer can focus entirely on the specifics of an application. The end result of these advantages means there is no need to reinvent the wheel every time a new web application is built!",
4-
"authors": [
4+
"authors": [
55
{
66
"name": "Fabien Potencier",
77
"email": "[email protected]"
88
}
99
],
10-
"license": "MIT"
10+
"license": "MIT",
11+
"require-dev": {
12+
"phpunit/phpunit": "^5.5"
13+
},
14+
"autoload-dev": {
15+
"classmap": [
16+
"lib/action",
17+
"lib/addon",
18+
"lib/autoload",
19+
"lib/cache",
20+
"lib/command",
21+
"lib/config",
22+
"lib/controller",
23+
"lib/database",
24+
"lib/debug",
25+
"lib/event",
26+
"lib/exception",
27+
"lib/filter",
28+
"lib/form",
29+
"lib/generator",
30+
"lib/helper",
31+
"lib/i18n",
32+
"lib/log",
33+
"lib/plugin",
34+
"lib/request",
35+
"lib/response",
36+
"lib/routing",
37+
"lib/storage",
38+
"lib/task",
39+
"lib/user",
40+
"lib/util",
41+
"lib/validator",
42+
"lib/vendor",
43+
"lib/view",
44+
"lib/widget",
45+
"lib/yaml"
46+
]
47+
}
1148
}

lib/request/sfWebRequest.class.php

+40-23
Original file line numberDiff line numberDiff line change
@@ -927,34 +927,51 @@ public function setRelativeUrlRoot($value)
927927
$this->relativeUrlRoot = $value;
928928
}
929929

930-
/**
931-
* Splits an HTTP header for the current web request.
932-
*
933-
* @param string $header Header to split
934-
*/
935-
public function splitHttpAcceptHeader($header)
930+
/**
931+
* Splits an HTTP header for the current web request.
932+
*
933+
* @param string $header Header to split
934+
*/
935+
public function splitHttpAcceptHeader($header)
936+
{
937+
$i = 0;
938+
$values = array();
939+
foreach (array_filter(explode(',', $header)) as $value)
936940
{
937-
$values = array();
938-
foreach (array_filter(explode(',', $header)) as $value)
941+
// Cut off any q-value that might come after a semi-colon
942+
if ($pos = strpos($value, ';'))
939943
{
940-
// Cut off any q-value that might come after a semi-colon
941-
if ($pos = strpos($value, ';'))
942-
{
943-
$q = (float) trim(substr($value, $pos + 3));
944-
$value = trim(substr($value, 0, $pos));
945-
}
946-
else
947-
{
948-
$q = 1;
949-
}
950-
951-
$values[$value] = $q;
944+
$q = (float) trim(substr($value, $pos + 3));
945+
$value = trim(substr($value, 0, $pos));
946+
}
947+
else
948+
{
949+
$q = 1;
952950
}
953951

954-
arsort($values);
955-
956-
return array_keys($values);
952+
$values[$value] = array($q, $i++);
957953
}
954+
/**
955+
* Implement reverse stable sort instead of arsort
956+
*/
957+
uasort($values, function ($a, $b) {
958+
if ($a[0] == $b[0]) {
959+
// Can not be same since we used $i++
960+
if ($a[1] < $b[1]) {
961+
return 1;
962+
} else {
963+
return -1;
964+
}
965+
} elseif ($a[0] < $b[0]) {
966+
return 1;
967+
} else {
968+
return -1;
969+
}
970+
});
971+
972+
return array_keys($values);
973+
}
974+
958975

959976
/**
960977
* Returns the array that contains all request information ($_SERVER or $_ENV).

phpunit.xml.dist

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors = "true"
3+
bootstrap = "vendor/autoload.php"
4+
backupGlobals = "false"
5+
backupStaticAttributes = "false">
6+
7+
<testsuites>
8+
<testsuite name="Symfony1 test" >
9+
<directory>./test/</directory>
10+
</testsuite>
11+
</testsuites>
12+
<filter>
13+
<whitelist>
14+
<directory>./lib</directory>
15+
</whitelist>
16+
</filter>
17+
</phpunit>

test/lib/request/sfWebRequestTest.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
use PHPUnit\Framework\TestCase;
3+
4+
require_once(__DIR__ . '/../../../lib/request/sfWebRequest.class.php');
5+
6+
class sfWebRequestTest extends TestCase
7+
{
8+
9+
/**
10+
* @var sfWebRequest
11+
*/
12+
private $request;
13+
14+
protected function setUp()
15+
{
16+
$event_dispatcher = new sfEventDispatcher();
17+
$this->request = new sfWebRequest($event_dispatcher);
18+
}
19+
20+
/**
21+
* @dataProvider splitHttpAcceptHeaderProvider
22+
* @covers sfWebRequest::splitHttpAcceptHeader
23+
*/
24+
public function testSplitHttpAcceptHeader($header, $expected)
25+
{
26+
self::assertSame($expected, $this->request->splitHttpAcceptHeader($header));
27+
}
28+
29+
public function splitHttpAcceptHeaderProvider()
30+
{
31+
return [
32+
[
33+
"text/javascript, text/html, application/xml, text/xml, */*",
34+
[
35+
' */*',
36+
' text/xml',
37+
' application/xml',
38+
' text/html',
39+
'text/javascript',
40+
]
41+
],
42+
[
43+
"text/javascript, text/html;q=2.9, application/xml, text/xml, */*",
44+
[
45+
'text/html',
46+
' */*',
47+
' text/xml',
48+
' application/xml',
49+
'text/javascript',
50+
]
51+
],
52+
[
53+
"text/javascript;q=1.0, text/html;q=2.9, application/xml, text/xml, */*",
54+
[
55+
'text/html',
56+
' */*',
57+
' text/xml',
58+
' application/xml',
59+
'text/javascript',
60+
]
61+
],
62+
[
63+
"text/javascript;q=1.0, text/html;q=1.0, application/xml, text/xml, */*",
64+
[
65+
' */*',
66+
' text/xml',
67+
' application/xml',
68+
'text/html',
69+
'text/javascript',
70+
]
71+
]
72+
];
73+
}
74+
}

0 commit comments

Comments
 (0)