Skip to content

Commit 8d18c98

Browse files
committed
minor symfony#12372 [Yaml] don't override internal PHP constants (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] don't override internal PHP constants | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#11783 | License | MIT | Doc PR | Commits ------- 376cc03 don't override internal PHP constants
2 parents 83c6ead + 376cc03 commit 8d18c98

File tree

9 files changed

+58
-42
lines changed

9 files changed

+58
-42
lines changed

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14-
if (!defined('ENT_SUBSTITUTE')) {
15-
define('ENT_SUBSTITUTE', 8);
16-
}
17-
1814
/**
1915
* Twig extension relate to PHP code and used by the profiler and the default exception templates.
2016
*
@@ -176,7 +172,13 @@ public function formatFile($file, $line, $text = null)
176172
$text = "$text at line $line";
177173

178174
if (false !== $link = $this->getFileLink($file, $line)) {
179-
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text);
175+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
176+
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
177+
} else {
178+
$flags = ENT_QUOTES;
179+
}
180+
181+
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
180182
}
181183

182184
return $text;

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
use Symfony\Component\Templating\Helper\Helper;
1515

16-
if (!defined('ENT_SUBSTITUTE')) {
17-
define('ENT_SUBSTITUTE', 8);
18-
}
19-
2016
/**
2117
* CodeHelper.
2218
*
@@ -170,7 +166,13 @@ public function formatFile($file, $line, $text = null)
170166
}
171167

172168
if (false !== $link = $this->getFileLink($file, $line)) {
173-
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text);
169+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
170+
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
171+
} else {
172+
$flags = ENT_QUOTES;
173+
}
174+
175+
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
174176
}
175177

176178
return $text;

src/Symfony/Component/ClassLoader/ClassMapGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Component\ClassLoader;
1313

14-
if (!defined('T_TRAIT')) {
15-
define('T_TRAIT', 0);
14+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
15+
define('SYMFONY_TRAIT', T_TRAIT);
16+
} else {
17+
define('SYMFONY_TRAIT', 0);
1618
}
1719

1820
/**
@@ -113,7 +115,7 @@ private static function findClasses($path)
113115
break;
114116
case T_CLASS:
115117
case T_INTERFACE:
116-
case T_TRAIT:
118+
case SYMFONY_TRAIT:
117119
// Find the classname
118120
while (($t = $tokens[++$i]) && is_array($t)) {
119121
if (T_STRING === $t[0]) {

src/Symfony/Component/Console/Input/InputDefinition.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
namespace Symfony\Component\Console\Input;
1313

14-
if (!defined('JSON_UNESCAPED_UNICODE')) {
15-
define('JSON_UNESCAPED_SLASHES', 64);
16-
define('JSON_UNESCAPED_UNICODE', 256);
17-
}
18-
1914
use Symfony\Component\Console\Descriptor\TextDescriptor;
2015
use Symfony\Component\Console\Descriptor\XmlDescriptor;
2116

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\Debug\Exception\FlattenException;
1616

17-
if (!defined('ENT_SUBSTITUTE')) {
18-
define('ENT_SUBSTITUTE', 8);
19-
}
20-
2117
/**
2218
* ExceptionHandler converts an exception to a Response object.
2319
*
@@ -292,22 +288,27 @@ private function abbrClass($class)
292288
*/
293289
private function formatArgs(array $args)
294290
{
291+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
292+
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
293+
} else {
294+
$flags = ENT_QUOTES;
295+
}
295296
$result = array();
296297
foreach ($args as $key => $item) {
297298
if ('object' === $item[0]) {
298299
$formattedValue = sprintf("<em>object</em>(%s)", $this->abbrClass($item[1]));
299300
} elseif ('array' === $item[0]) {
300301
$formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
301302
} elseif ('string' === $item[0]) {
302-
$formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset));
303+
$formattedValue = sprintf("'%s'", htmlspecialchars($item[1], $flags, $this->charset));
303304
} elseif ('null' === $item[0]) {
304305
$formattedValue = '<em>null</em>';
305306
} elseif ('boolean' === $item[0]) {
306307
$formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
307308
} elseif ('resource' === $item[0]) {
308309
$formattedValue = '<em>resource</em>';
309310
} else {
310-
$formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), true));
311+
$formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], $flags, $this->charset), true));
311312
}
312313

313314
$result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);

src/Symfony/Component/HttpKernel/Fragment/HIncludeFragmentRenderer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
namespace Symfony\Component\HttpKernel\Fragment;
1313

14-
if (!defined('ENT_SUBSTITUTE')) {
15-
define('ENT_SUBSTITUTE', 8);
16-
}
17-
1814
use Symfony\Component\HttpFoundation\Request;
1915
use Symfony\Component\HttpFoundation\Response;
2016
use Symfony\Component\Templating\EngineInterface;
@@ -111,11 +107,16 @@ public function render($uri, Request $request, array $options = array())
111107
}
112108
$renderedAttributes = '';
113109
if (count($attributes) > 0) {
110+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
111+
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
112+
} else {
113+
$flags = ENT_QUOTES;
114+
}
114115
foreach ($attributes as $attribute => $value) {
115116
$renderedAttributes .= sprintf(
116117
' %s="%s"',
117-
htmlspecialchars($attribute, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false),
118-
htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false)
118+
htmlspecialchars($attribute, $flags, $this->charset, false),
119+
htmlspecialchars($value, $flags, $this->charset, false)
119120
);
120121
}
121122
}

src/Symfony/Component/Templating/PhpEngine.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
use Symfony\Component\Templating\Helper\HelperInterface;
1818
use Symfony\Component\Templating\Loader\LoaderInterface;
1919

20-
if (!defined('ENT_SUBSTITUTE')) {
21-
define('ENT_SUBSTITUTE', 8);
22-
}
23-
2420
/**
2521
* PhpEngine is an engine able to render PHP templates.
2622
*
@@ -466,6 +462,11 @@ public function getGlobals()
466462
protected function initializeEscapers()
467463
{
468464
$that = $this;
465+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
466+
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
467+
} else {
468+
$flags = ENT_QUOTES;
469+
}
469470

470471
$this->escapers = array(
471472
'html' =>
@@ -476,10 +477,10 @@ protected function initializeEscapers()
476477
*
477478
* @return string the escaped value
478479
*/
479-
function ($value) use ($that) {
480+
function ($value) use ($that, $flags) {
480481
// Numbers and Boolean values get turned into strings which can cause problems
481482
// with type comparisons (e.g. === or is_int() etc).
482-
return is_string($value) ? htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $that->getCharset(), false) : $value;
483+
return is_string($value) ? htmlspecialchars($value, $flags, $that->getCharset(), false) : $value;
483484
},
484485

485486
'js' =>

src/Symfony/Component/Yaml/Exception/ParseException.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
namespace Symfony\Component\Yaml\Exception;
1313

14-
if (!defined('JSON_UNESCAPED_UNICODE')) {
15-
define('JSON_UNESCAPED_SLASHES', 64);
16-
define('JSON_UNESCAPED_UNICODE', 256);
17-
}
18-
1914
/**
2015
* Exception class thrown when an error occurs during parsing.
2116
*
@@ -130,7 +125,12 @@ private function updateRepr()
130125
}
131126

132127
if (null !== $this->parsedFile) {
133-
$this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
128+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
129+
$jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
130+
} else {
131+
$jsonOptions = 0;
132+
}
133+
$this->message .= sprintf(' in %s', json_encode($this->parsedFile, $jsonOptions));
134134
}
135135

136136
if ($this->parsedLine >= 0) {

src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ public function testGetMessage()
2727

2828
$this->assertEquals($message, $exception->getMessage());
2929
}
30+
31+
public function testGetMessageWithUnicodeInFilename()
32+
{
33+
$exception = new ParseException('Error message', 42, 'foo: bar', 'äöü.yml');
34+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
35+
$message = 'Error message in "äöü.yml" at line 42 (near "foo: bar")';
36+
} else {
37+
$message = 'Error message in "\u00e4\u00f6\u00fc.yml" at line 42 (near "foo: bar")';
38+
}
39+
40+
$this->assertEquals($message, $exception->getMessage());
41+
}
3042
}

0 commit comments

Comments
 (0)