bug #15128 DbalLogger: Small nonutf8 array fix (vpetrovych, weaverryan)

This PR was merged into the 2.3 branch.

Discussion
----------

DbalLogger: Small nonutf8 array fix

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9096
| License       | MIT
| Doc PR        | n/a

Hi guys!

Just a small triage of #9096. Actually, @vpetrovych did all the work, and I was able to rebase and keep his original commit for authorship :).

This is obviously a very minor, edge issue - but the patch is simple. The new `normalizeParams` function is an exact copy-and-paste except for the changes in sha: 1882a2ae47

Thanks!

Commits
-------

1882a2a Normalizing recursively - see #9096
8edd4c6 No change - the normalizeParams is a copy-and-paste of the earlier logic
eae907c fixes issue with logging array of non-utf8 data
This commit is contained in:
Fabien Potencier 2015-06-28 09:09:31 +02:00
commit 0cd30622d3
2 changed files with 68 additions and 24 deletions

View File

@ -50,30 +50,7 @@ class DbalLogger implements SQLLogger
}
if (is_array($params)) {
foreach ($params as $index => $param) {
if (!is_string($params[$index])) {
continue;
}
// non utf-8 strings break json encoding
if (!preg_match('//u', $params[$index])) {
$params[$index] = self::BINARY_DATA_VALUE;
continue;
}
// 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;
}
}
}
$params = $this->normalizeParams($params);
}
if (null !== $this->logger) {
@ -101,4 +78,40 @@ class DbalLogger implements SQLLogger
{
$this->logger->debug($message, $params);
}
private function normalizeParams(array $params)
{
foreach ($params as $index => $param) {
// normalize recursively
if (is_array($param)) {
$params[$index] = $this->normalizeParams($param);
continue;
}
if (!is_string($params[$index])) {
continue;
}
// non utf-8 strings break json encoding
if (!preg_match('//u', $params[$index])) {
$params[$index] = self::BINARY_DATA_VALUE;
continue;
}
// 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;
}
}
}
return $params;
}
}

View File

@ -73,6 +73,37 @@ class DbalLoggerTest extends \PHPUnit_Framework_TestCase
));
}
public function testLogNonUtf8Array()
{
$logger = $this->getMock('Psr\\Log\\LoggerInterface');
$dbalLogger = $this
->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
->setConstructorArgs(array($logger, null))
->setMethods(array('log'))
->getMock()
;
$dbalLogger
->expects($this->once())
->method('log')
->with('SQL', array(
'utf8' => 'foo',
array(
'nonutf8' => DbalLogger::BINARY_DATA_VALUE,
)
)
)
;
$dbalLogger->startQuery('SQL', array(
'utf8' => 'foo',
array(
'nonutf8' => "\x7F\xFF",
)
));
}
public function testLogLongString()
{
$logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface');