Skip to content

Commit

Permalink
minor #12372 [Yaml] don't override internal PHP constants (xabbuh)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Nov 16, 2014
2 parents 83c6ead + 376cc03 commit 8d18c98
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 42 deletions.
12 changes: 7 additions & 5 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -176,7 +172,13 @@ public function formatFile($file, $line, $text = null)
$text = "$text at line $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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

use Symfony\Component\Templating\Helper\Helper;

if (!defined('ENT_SUBSTITUTE')) {
define('ENT_SUBSTITUTE', 8);
}

/**
* CodeHelper.
*
Expand Down Expand Up @@ -170,7 +166,13 @@ public function formatFile($file, $line, $text = null)
}

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;
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/ClassLoader/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -113,7 +115,7 @@ private static function findClasses($path)
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]) {
Expand Down
5 changes: 0 additions & 5 deletions src/Symfony/Component/Console/Input/InputDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 7 additions & 6 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
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.
*
Expand Down Expand Up @@ -292,22 +288,27 @@ private function abbrClass($class)
*/
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]) {
$formattedValue = sprintf("<em>object</em>(%s)", $this->abbrClass($item[1]));
} elseif ('array' === $item[0]) {
$formattedValue = sprintf("<em>array</em>(%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 = '<em>null</em>';
} elseif ('boolean' === $item[0]) {
$formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
} elseif ('resource' === $item[0]) {
$formattedValue = '<em>resource</em>';
} 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,11 +107,16 @@ public function render($uri, Request $request, array $options = array())
}
$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)
);
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/Symfony/Component/Templating/PhpEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
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.
*
Expand Down Expand Up @@ -466,6 +462,11 @@ public function getGlobals()
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' =>
Expand All @@ -476,10 +477,10 @@ protected function initializeEscapers()
*
* @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' =>
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Yaml/Exception/ParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -130,7 +125,12 @@ private function updateRepr()
}

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) {
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParseExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,16 @@ public function testGetMessage()

$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());
}
}

0 comments on commit 8d18c98

Please sign in to comment.