From 376cc0332dcd95ea115980b4bc33f222c8fd6560 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 1 Nov 2014 13:49:09 +0100 Subject: [PATCH] don't override internal PHP constants --- src/Symfony/Bridge/Twig/Extension/CodeExtension.php | 12 +++++++----- .../Templating/Helper/CodeHelper.php | 12 +++++++----- .../Component/ClassLoader/ClassMapGenerator.php | 8 +++++--- .../Component/Console/Input/InputDefinition.php | 5 ----- src/Symfony/Component/Debug/ExceptionHandler.php | 13 +++++++------ .../Fragment/HIncludeFragmentRenderer.php | 13 +++++++------ src/Symfony/Component/Templating/PhpEngine.php | 13 +++++++------ .../Component/Yaml/Exception/ParseException.php | 12 ++++++------ .../Component/Yaml/Tests/ParseExceptionTest.php | 12 ++++++++++++ 9 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/CodeExtension.php b/src/Symfony/Bridge/Twig/Extension/CodeExtension.php index 0ec48ee036..1df3e5da28 100644 --- a/src/Symfony/Bridge/Twig/Extension/CodeExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/CodeExtension.php @@ -11,10 +11,6 @@ namespace Symfony\Bridge\Twig\Extension; -if (!defined('ENT_SUBSTITUTE')) { - define('ENT_SUBSTITUTE', 8); -} - /** * Twig extension relate to PHP code and used by the profiler and the default exception templates. * @@ -176,7 +172,13 @@ class CodeExtension extends \Twig_Extension $text = "$text at line $line"; if (false !== $link = $this->getFileLink($file, $line)) { - return sprintf('%s', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text); + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $flags = ENT_QUOTES | ENT_SUBSTITUTE; + } else { + $flags = ENT_QUOTES; + } + + return sprintf('%s', htmlspecialchars($link, $flags, $this->charset), $text); } return $text; diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php index 795b86e1b6..a72d5bf16a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php @@ -13,10 +13,6 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; -if (!defined('ENT_SUBSTITUTE')) { - define('ENT_SUBSTITUTE', 8); -} - /** * CodeHelper. * @@ -170,7 +166,13 @@ class CodeHelper extends Helper } if (false !== $link = $this->getFileLink($file, $line)) { - return sprintf('%s', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text); + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $flags = ENT_QUOTES | ENT_SUBSTITUTE; + } else { + $flags = ENT_QUOTES; + } + + return sprintf('%s', htmlspecialchars($link, $flags, $this->charset), $text); } return $text; diff --git a/src/Symfony/Component/ClassLoader/ClassMapGenerator.php b/src/Symfony/Component/ClassLoader/ClassMapGenerator.php index 6b618cbe63..24a1d9769a 100644 --- a/src/Symfony/Component/ClassLoader/ClassMapGenerator.php +++ b/src/Symfony/Component/ClassLoader/ClassMapGenerator.php @@ -11,8 +11,10 @@ namespace Symfony\Component\ClassLoader; -if (!defined('T_TRAIT')) { - define('T_TRAIT', 0); +if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + define('SYMFONY_TRAIT', T_TRAIT); +} else { + define('SYMFONY_TRAIT', 0); } /** @@ -113,7 +115,7 @@ class ClassMapGenerator break; case T_CLASS: case T_INTERFACE: - case T_TRAIT: + case SYMFONY_TRAIT: // Find the classname while (($t = $tokens[++$i]) && is_array($t)) { if (T_STRING === $t[0]) { diff --git a/src/Symfony/Component/Console/Input/InputDefinition.php b/src/Symfony/Component/Console/Input/InputDefinition.php index 8a989eed7c..a21c350792 100644 --- a/src/Symfony/Component/Console/Input/InputDefinition.php +++ b/src/Symfony/Component/Console/Input/InputDefinition.php @@ -11,11 +11,6 @@ namespace Symfony\Component\Console\Input; -if (!defined('JSON_UNESCAPED_UNICODE')) { - define('JSON_UNESCAPED_SLASHES', 64); - define('JSON_UNESCAPED_UNICODE', 256); -} - use Symfony\Component\Console\Descriptor\TextDescriptor; use Symfony\Component\Console\Descriptor\XmlDescriptor; diff --git a/src/Symfony/Component/Debug/ExceptionHandler.php b/src/Symfony/Component/Debug/ExceptionHandler.php index 74425cb760..acbeae6003 100644 --- a/src/Symfony/Component/Debug/ExceptionHandler.php +++ b/src/Symfony/Component/Debug/ExceptionHandler.php @@ -14,10 +14,6 @@ namespace Symfony\Component\Debug; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Debug\Exception\FlattenException; -if (!defined('ENT_SUBSTITUTE')) { - define('ENT_SUBSTITUTE', 8); -} - /** * ExceptionHandler converts an exception to a Response object. * @@ -292,6 +288,11 @@ EOF; */ private function formatArgs(array $args) { + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $flags = ENT_QUOTES | ENT_SUBSTITUTE; + } else { + $flags = ENT_QUOTES; + } $result = array(); foreach ($args as $key => $item) { if ('object' === $item[0]) { @@ -299,7 +300,7 @@ EOF; } elseif ('array' === $item[0]) { $formattedValue = sprintf("array(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]); } elseif ('string' === $item[0]) { - $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset)); + $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], $flags, $this->charset)); } elseif ('null' === $item[0]) { $formattedValue = 'null'; } elseif ('boolean' === $item[0]) { @@ -307,7 +308,7 @@ EOF; } elseif ('resource' === $item[0]) { $formattedValue = 'resource'; } else { - $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), true)); + $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], $flags, $this->charset), true)); } $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue); diff --git a/src/Symfony/Component/HttpKernel/Fragment/HIncludeFragmentRenderer.php b/src/Symfony/Component/HttpKernel/Fragment/HIncludeFragmentRenderer.php index e9bece0225..4f53f3cf5e 100644 --- a/src/Symfony/Component/HttpKernel/Fragment/HIncludeFragmentRenderer.php +++ b/src/Symfony/Component/HttpKernel/Fragment/HIncludeFragmentRenderer.php @@ -11,10 +11,6 @@ namespace Symfony\Component\HttpKernel\Fragment; -if (!defined('ENT_SUBSTITUTE')) { - define('ENT_SUBSTITUTE', 8); -} - use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Templating\EngineInterface; @@ -111,11 +107,16 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer } $renderedAttributes = ''; if (count($attributes) > 0) { + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $flags = ENT_QUOTES | ENT_SUBSTITUTE; + } else { + $flags = ENT_QUOTES; + } foreach ($attributes as $attribute => $value) { $renderedAttributes .= sprintf( ' %s="%s"', - htmlspecialchars($attribute, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false), - htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false) + htmlspecialchars($attribute, $flags, $this->charset, false), + htmlspecialchars($value, $flags, $this->charset, false) ); } } diff --git a/src/Symfony/Component/Templating/PhpEngine.php b/src/Symfony/Component/Templating/PhpEngine.php index 75a581e353..42eebebe1a 100644 --- a/src/Symfony/Component/Templating/PhpEngine.php +++ b/src/Symfony/Component/Templating/PhpEngine.php @@ -17,10 +17,6 @@ use Symfony\Component\Templating\Storage\StringStorage; use Symfony\Component\Templating\Helper\HelperInterface; use Symfony\Component\Templating\Loader\LoaderInterface; -if (!defined('ENT_SUBSTITUTE')) { - define('ENT_SUBSTITUTE', 8); -} - /** * PhpEngine is an engine able to render PHP templates. * @@ -466,6 +462,11 @@ class PhpEngine implements EngineInterface, \ArrayAccess protected function initializeEscapers() { $that = $this; + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $flags = ENT_QUOTES | ENT_SUBSTITUTE; + } else { + $flags = ENT_QUOTES; + } $this->escapers = array( 'html' => @@ -476,10 +477,10 @@ class PhpEngine implements EngineInterface, \ArrayAccess * * @return string the escaped value */ - function ($value) use ($that) { + function ($value) use ($that, $flags) { // Numbers and Boolean values get turned into strings which can cause problems // with type comparisons (e.g. === or is_int() etc). - return is_string($value) ? htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $that->getCharset(), false) : $value; + return is_string($value) ? htmlspecialchars($value, $flags, $that->getCharset(), false) : $value; }, 'js' => diff --git a/src/Symfony/Component/Yaml/Exception/ParseException.php b/src/Symfony/Component/Yaml/Exception/ParseException.php index ff01d6b9a6..eda6efae08 100644 --- a/src/Symfony/Component/Yaml/Exception/ParseException.php +++ b/src/Symfony/Component/Yaml/Exception/ParseException.php @@ -11,11 +11,6 @@ namespace Symfony\Component\Yaml\Exception; -if (!defined('JSON_UNESCAPED_UNICODE')) { - define('JSON_UNESCAPED_SLASHES', 64); - define('JSON_UNESCAPED_UNICODE', 256); -} - /** * Exception class thrown when an error occurs during parsing. * @@ -130,7 +125,12 @@ class ParseException extends RuntimeException } if (null !== $this->parsedFile) { - $this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; + } else { + $jsonOptions = 0; + } + $this->message .= sprintf(' in %s', json_encode($this->parsedFile, $jsonOptions)); } if ($this->parsedLine >= 0) { diff --git a/src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php b/src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php index 289965e8d9..a45fb76313 100644 --- a/src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php +++ b/src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php @@ -27,4 +27,16 @@ class ParseExceptionTest extends \PHPUnit_Framework_TestCase $this->assertEquals($message, $exception->getMessage()); } + + public function testGetMessageWithUnicodeInFilename() + { + $exception = new ParseException('Error message', 42, 'foo: bar', 'äöü.yml'); + if (version_compare(PHP_VERSION, '5.4.0', '>=')) { + $message = 'Error message in "äöü.yml" at line 42 (near "foo: bar")'; + } else { + $message = 'Error message in "\u00e4\u00f6\u00fc.yml" at line 42 (near "foo: bar")'; + } + + $this->assertEquals($message, $exception->getMessage()); + } }