minor #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 | #11783
| License       | MIT
| Doc PR        |

Commits
-------

376cc03 don't override internal PHP constants
This commit is contained in:
Fabien Potencier 2014-11-16 19:04:21 +01:00
commit 8d18c98de0
9 changed files with 58 additions and 42 deletions

View File

@ -11,10 +11,6 @@
namespace Symfony\Bridge\Twig\Extension; 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. * 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"; $text = "$text at line $line";
if (false !== $link = $this->getFileLink($file, $line)) { if (false !== $link = $this->getFileLink($file, $line)) {
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); if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
} else {
$flags = ENT_QUOTES;
}
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
} }
return $text; return $text;

View File

@ -13,10 +13,6 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
use Symfony\Component\Templating\Helper\Helper; use Symfony\Component\Templating\Helper\Helper;
if (!defined('ENT_SUBSTITUTE')) {
define('ENT_SUBSTITUTE', 8);
}
/** /**
* CodeHelper. * CodeHelper.
* *
@ -170,7 +166,13 @@ class CodeHelper extends Helper
} }
if (false !== $link = $this->getFileLink($file, $line)) { if (false !== $link = $this->getFileLink($file, $line)) {
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); if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
} else {
$flags = ENT_QUOTES;
}
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, $flags, $this->charset), $text);
} }
return $text; return $text;

View File

@ -11,8 +11,10 @@
namespace Symfony\Component\ClassLoader; namespace Symfony\Component\ClassLoader;
if (!defined('T_TRAIT')) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
define('T_TRAIT', 0); define('SYMFONY_TRAIT', T_TRAIT);
} else {
define('SYMFONY_TRAIT', 0);
} }
/** /**
@ -113,7 +115,7 @@ class ClassMapGenerator
break; break;
case T_CLASS: case T_CLASS:
case T_INTERFACE: case T_INTERFACE:
case T_TRAIT: case SYMFONY_TRAIT:
// Find the classname // Find the classname
while (($t = $tokens[++$i]) && is_array($t)) { while (($t = $tokens[++$i]) && is_array($t)) {
if (T_STRING === $t[0]) { if (T_STRING === $t[0]) {

View File

@ -11,11 +11,6 @@
namespace Symfony\Component\Console\Input; 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\TextDescriptor;
use Symfony\Component\Console\Descriptor\XmlDescriptor; use Symfony\Component\Console\Descriptor\XmlDescriptor;

View File

@ -14,10 +14,6 @@ namespace Symfony\Component\Debug;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\Debug\Exception\FlattenException;
if (!defined('ENT_SUBSTITUTE')) {
define('ENT_SUBSTITUTE', 8);
}
/** /**
* ExceptionHandler converts an exception to a Response object. * ExceptionHandler converts an exception to a Response object.
* *
@ -292,6 +288,11 @@ EOF;
*/ */
private function formatArgs(array $args) private function formatArgs(array $args)
{ {
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
} else {
$flags = ENT_QUOTES;
}
$result = array(); $result = array();
foreach ($args as $key => $item) { foreach ($args as $key => $item) {
if ('object' === $item[0]) { if ('object' === $item[0]) {
@ -299,7 +300,7 @@ EOF;
} elseif ('array' === $item[0]) { } elseif ('array' === $item[0]) {
$formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]); $formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
} elseif ('string' === $item[0]) { } 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]) { } elseif ('null' === $item[0]) {
$formattedValue = '<em>null</em>'; $formattedValue = '<em>null</em>';
} elseif ('boolean' === $item[0]) { } elseif ('boolean' === $item[0]) {
@ -307,7 +308,7 @@ EOF;
} elseif ('resource' === $item[0]) { } elseif ('resource' === $item[0]) {
$formattedValue = '<em>resource</em>'; $formattedValue = '<em>resource</em>';
} else { } 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); $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);

View File

@ -11,10 +11,6 @@
namespace Symfony\Component\HttpKernel\Fragment; namespace Symfony\Component\HttpKernel\Fragment;
if (!defined('ENT_SUBSTITUTE')) {
define('ENT_SUBSTITUTE', 8);
}
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface; use Symfony\Component\Templating\EngineInterface;
@ -111,11 +107,16 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
} }
$renderedAttributes = ''; $renderedAttributes = '';
if (count($attributes) > 0) { 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) { foreach ($attributes as $attribute => $value) {
$renderedAttributes .= sprintf( $renderedAttributes .= sprintf(
' %s="%s"', ' %s="%s"',
htmlspecialchars($attribute, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false), htmlspecialchars($attribute, $flags, $this->charset, false),
htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false) htmlspecialchars($value, $flags, $this->charset, false)
); );
} }
} }

View File

@ -17,10 +17,6 @@ use Symfony\Component\Templating\Storage\StringStorage;
use Symfony\Component\Templating\Helper\HelperInterface; use Symfony\Component\Templating\Helper\HelperInterface;
use Symfony\Component\Templating\Loader\LoaderInterface; use Symfony\Component\Templating\Loader\LoaderInterface;
if (!defined('ENT_SUBSTITUTE')) {
define('ENT_SUBSTITUTE', 8);
}
/** /**
* PhpEngine is an engine able to render PHP templates. * PhpEngine is an engine able to render PHP templates.
* *
@ -466,6 +462,11 @@ class PhpEngine implements EngineInterface, \ArrayAccess
protected function initializeEscapers() protected function initializeEscapers()
{ {
$that = $this; $that = $this;
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$flags = ENT_QUOTES | ENT_SUBSTITUTE;
} else {
$flags = ENT_QUOTES;
}
$this->escapers = array( $this->escapers = array(
'html' => 'html' =>
@ -476,10 +477,10 @@ class PhpEngine implements EngineInterface, \ArrayAccess
* *
* @return string the escaped value * @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 // Numbers and Boolean values get turned into strings which can cause problems
// with type comparisons (e.g. === or is_int() etc). // 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' => 'js' =>

View File

@ -11,11 +11,6 @@
namespace Symfony\Component\Yaml\Exception; 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. * Exception class thrown when an error occurs during parsing.
* *
@ -130,7 +125,12 @@ class ParseException extends RuntimeException
} }
if (null !== $this->parsedFile) { 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) { if ($this->parsedLine >= 0) {

View File

@ -27,4 +27,16 @@ class ParseExceptionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($message, $exception->getMessage()); $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());
}
} }