Skip to content

[Polyfill] A new component for portability across PHP versions and extensions #16240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"symfony/locale": "self.version",
"symfony/monolog-bridge": "self.version",
"symfony/options-resolver": "self.version",
"symfony/polyfill": "self.version",
"symfony/process": "self.version",
"symfony/property-access": "self.version",
"symfony/property-info": "self.version",
Expand Down Expand Up @@ -96,10 +97,10 @@
"Symfony\\Component\\": "src/Symfony/Component/"
},
"classmap": [
"src/Symfony/Component/HttpFoundation/Resources/stubs",
"src/Symfony/Component/Polyfill/Resources/stubs",
"src/Symfony/Component/Intl/Resources/stubs"
],
"files": [ "src/Symfony/Component/Intl/Resources/stubs/functions.php" ]
"files": [ "src/Symfony/Component/Polyfill/bootstrap.php" ]
},
"minimum-stability": "dev",
"extra": {
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Component\Polyfill\Tests\FunctionsTestListener" />
</listeners>
</phpunit>
13 changes: 3 additions & 10 deletions src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,9 @@ private function normalizeParams(array $params)
}

// detect if the too long string must be shorten
if (function_exists('mb_strlen')) {
if (self::MAX_STRING_LENGTH < mb_strlen($params[$index], 'UTF-8')) {
$params[$index] = mb_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]';
continue;
}
} else {
if (self::MAX_STRING_LENGTH < strlen($params[$index])) {
$params[$index] = substr($params[$index], 0, self::MAX_STRING_LENGTH - 6).' [...]';
continue;
}
if (self::MAX_STRING_LENGTH < iconv_strlen($params[$index], 'UTF-8')) {
$params[$index] = iconv_substr($params[$index], 0, self::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]';
continue;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ public function testLogLongString()
));
}

/**
* @requires extension mbstring
*/
public function testLogUTF8LongString()
{
$logger = $this->getMock('Psr\\Log\\LoggerInterface');
Expand All @@ -160,7 +157,7 @@ public function testLogUTF8LongString()
$dbalLogger
->expects($this->once())
->method('log')
->with('SQL', array('short' => $shortString, 'long' => mb_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, mb_detect_encoding($longString)).' [...]'))
->with('SQL', array('short' => $shortString, 'long' => iconv_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]'))
;

$dbalLogger->startQuery('SQL', array(
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
private $skippedFile = false;
private $wasSkipped = array();
private $isSkipped = array();
private $testsStack = array();

public function __destruct()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private function sanitizeString($string, $length = 40)
{
$string = trim(preg_replace('/\s+/', ' ', $string));

if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($string)) {
if (false !== $encoding = mb_detect_encoding($string)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes Polyfill a dependency of FrameworkBundle, doesn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, fixed

if (mb_strlen($string, $encoding) > $length) {
return mb_substr($string, 0, $length - 3, $encoding).'...';
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"symfony/event-dispatcher": "~2.8|~3.0.0",
"symfony/http-foundation": "~2.4.9|~2.5,>=2.5.4|~3.0.0",
"symfony/http-kernel": "~2.8",
"symfony/polyfill": "~2.8|~3.0.0",
"symfony/filesystem": "~2.3|~3.0.0",
"symfony/routing": "~2.8|~3.0.0",
"symfony/security-core": "~2.6|~3.0.0",
Expand Down
8 changes: 0 additions & 8 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,10 +1077,6 @@ public function setDefaultCommand($commandName)

private function stringWidth($string)
{
if (!function_exists('mb_strwidth')) {
return strlen($string);
}

if (false === $encoding = mb_detect_encoding($string)) {
return strlen($string);
}
Expand All @@ -1094,10 +1090,6 @@ private function splitStringByWidth($string, $width)
// additionally, array_slice() is not enough as some character has doubled width.
// we need a function to split string not by character count but by string width

if (!function_exists('mb_strwidth')) {
return str_split($string, $width);
}

if (false === $encoding = mb_detect_encoding($string)) {
return str_split($string, $width);
}
Expand Down
4 changes: 0 additions & 4 deletions src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public function getHelperSet()
*/
public static function strlen($string)
{
if (!function_exists('mb_strwidth')) {
return strlen($string);
}

if (false === $encoding = mb_detect_encoding($string)) {
return strlen($string);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private function renderCell(array $row, $column, $cellFormat)
}

// str_pad won't work properly with multi-byte strings, we need to fix the padding
if (function_exists('mb_strwidth') && false !== $encoding = mb_detect_encoding($cell)) {
if (false !== $encoding = mb_detect_encoding($cell)) {
$width += strlen($cell) - mb_strwidth($cell, $encoding);
}

Expand Down
3 changes: 0 additions & 3 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,6 @@ public function testRenderException()
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getDisplay(true), '->renderException() wraps messages when they are bigger than the terminal');
}

/**
* @requires extension mbstring
*/
public function testRenderExceptionWithDoubleWidthCharacters()
{
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public function testFormatBlock()
);
}

/**
* @requires extension mbstring
*/
public function testFormatBlockWithDiacriticLetters()
{
$formatter = new FormatterHelper();
Expand All @@ -68,9 +65,6 @@ public function testFormatBlockWithDiacriticLetters()
);
}

/**
* @requires extension mbstring
*/
public function testFormatBlockWithDoubleWidthDiacriticLetters()
{
$formatter = new FormatterHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ public function testRedrawFrequency()
$progress->advance(1);
}

/**
* @requires extension mbstring
*/
public function testMultiByteSupport()
{
$progress = new ProgressHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,6 @@ public function testRenderProvider()
);
}

/**
* @requires extension mbstring
*/
public function testRenderMultiByte()
{
$table = new TableHelper();
Expand All @@ -282,9 +279,6 @@ public function testRenderMultiByte()
$this->assertEquals($expected, $this->getOutputContent($output));
}

/**
* @requires extension mbstring
*/
public function testRenderFullWidthCharacters()
{
$table = new TableHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,6 @@ public function testRedrawFrequency()
$bar->advance(1);
}

/**
* @requires extension mbstring
*/
public function testMultiByteSupport()
{
$bar = new ProgressBar($output = $this->getOutputStream());
Expand Down Expand Up @@ -541,9 +538,6 @@ public function testMultilineFormat()
);
}

/**
* @requires extension mbstring
*/
public function testAnsiColorsAndEmojis()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 15);
Expand Down
3 changes: 0 additions & 3 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,6 @@ public function testRenderProvider()
);
}

/**
* @requires extension mbstring
*/
public function testRenderMultiByte()
{
$table = new Table($output = $this->getOutputStream());
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.3.9"
"php": ">=5.3.9",
"symfony/polyfill": "~2.8|~3.0.0"
},
"require-dev": {
"symfony/event-dispatcher": "~2.1|~3.0.0",
Expand Down
29 changes: 1 addition & 28 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,34 +173,7 @@ public function addHtmlContent($content, $charset = 'UTF-8')

try {
// Convert charset to HTML-entities to work around bugs in DOMDocument::loadHTML()

if (function_exists('mb_convert_encoding')) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
} elseif (function_exists('iconv')) {
$content = preg_replace_callback(
'/[\x80-\xFF]+/',
function ($m) {
$m = unpack('C*', $m[0]);
$i = 1;
$entities = '';

while (isset($m[$i])) {
if (0xF0 <= $m[$i]) {
$c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
} elseif (0xE0 <= $m[$i]) {
$c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
} else {
$c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
}

$entities .= '&#'.$c.';';
}

return $entities;
},
iconv($charset, 'UTF-8', $content)
);
}
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
} catch (\Exception $e) {
}

Expand Down
2 changes: 0 additions & 2 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public function testAddHtmlContentWithBaseTag()

/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
* @requires extension mbstring
*/
public function testAddHtmlContentCharset()
{
Expand Down Expand Up @@ -137,7 +136,6 @@ public function testAddHtmlContentUnsupportedCharset()

/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
* @requires extension mbstring
*/
public function testAddHtmlContentCharsetGbk()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/DomCrawler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.3.9"
"php": ">=5.3.9",
"symfony/polyfill": "~2.8|~3.0.0"
},
"require-dev": {
"symfony/css-selector": "~2.8|~3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function reverseTransform($value)
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
}

if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value)) {
if (false !== $encoding = mb_detect_encoding($value)) {
$length = mb_strlen($value, $encoding);
$remainder = mb_substr($value, $position, $length, $encoding);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ public function testReverseTransformWithGrouping($to, $from, $locale)

/**
* @see https://github.com/symfony/symfony/issues/7609
*
* @requires extension mbstring
*/
public function testReverseTransformWithGroupingAndFixedSpaces()
{
Expand Down Expand Up @@ -583,7 +581,6 @@ public function testReverseTransformDisallowsCenteredExtraCharacters()
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
* @expectedExceptionMessage The number contains unrecognized characters: "foo8"
* @requires extension mbstring
*/
public function testReverseTransformDisallowsCenteredExtraCharactersMultibyte()
{
Expand All @@ -600,7 +597,6 @@ public function testReverseTransformDisallowsCenteredExtraCharactersMultibyte()
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
* @expectedExceptionMessage The number contains unrecognized characters: "foo8"
* @requires extension mbstring
*/
public function testReverseTransformIgnoresTrailingSpacesInExceptionMessage()
{
Expand Down Expand Up @@ -628,7 +624,6 @@ public function testReverseTransformDisallowsTrailingExtraCharacters()
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
* @expectedExceptionMessage The number contains unrecognized characters: "foo"
* @requires extension mbstring
*/
public function testReverseTransformDisallowsTrailingExtraCharactersMultibyte()
{
Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Component/Form/Tests/Util/StringUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function testTrim()

/**
* @dataProvider spaceProvider
* @requires extension mbstring
*/
public function testTrimUtf8Separators($hex)
{
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"symfony/event-dispatcher": "~2.1|~3.0.0",
"symfony/intl": "~2.4|~3.0.0",
"symfony/options-resolver": "~2.6",
"symfony/polyfill": "~2.8|~3.0.0",
"symfony/property-access": "~2.3|~3.0.0"
},
"require-dev": {
Expand Down
29 changes: 1 addition & 28 deletions src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function setData($data = array())
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException($this->transformJsonError());
throw new \InvalidArgumentException(json_last_error_msg());
}

$this->data = $data;
Expand Down Expand Up @@ -196,31 +196,4 @@ protected function update()

return $this->setContent($this->data);
}

private function transformJsonError()
{
if (function_exists('json_last_error_msg')) {
return json_last_error_msg();
}

switch (json_last_error()) {
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded.';

case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch.';

case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found.';

case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON.';

case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded.';

default:
return 'Unknown error.';
}
}
}
Loading