Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef53456

Browse files
jfsimonfabpot
authored andcommitted
[DoctrineBridge] Avoids blob values to be logged by doctrine
1 parent 7241be9 commit ef53456

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
class DbalLogger implements SQLLogger
2424
{
25+
const MAX_STRING_LENGTH = 32;
26+
const BINARY_DATA_VALUE = '(binary value)';
27+
2528
protected $logger;
2629
protected $stopwatch;
2730

@@ -46,6 +49,26 @@ public function startQuery($sql, array $params = null, array $types = null)
4649
$this->stopwatch->start('doctrine', 'doctrine');
4750
}
4851

52+
if (is_array($params)) {
53+
foreach ($params as $index => $param) {
54+
if (!is_string($params[$index])) {
55+
continue;
56+
}
57+
58+
// non utf-8 strings break json encoding
59+
if (null === preg_match('#[^\p{L}\p{N} ]#u', $params[$index])) {
60+
$params[$index] = self::BINARY_DATA_VALUE;
61+
continue;
62+
}
63+
64+
// too long string must be shorten
65+
if (self::MAX_STRING_LENGTH < strlen($params[$index])) {
66+
$params[$index] = substr($params[$index], self::MAX_STRING_LENGTH - 6).' [...]';
67+
continue;
68+
}
69+
}
70+
}
71+
4972
if (null !== $this->logger) {
5073
$this->log($sql, null === $params ? array() : $params);
5174
}

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypePerformanceTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ class EntityTypePerformanceTest extends FormPerformanceTestCase
3333
protected function getExtensions()
3434
{
3535
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
36+
3637
$manager->expects($this->any())
3738
->method('getManager')
3839
->will($this->returnValue($this->em));
3940

41+
$manager->expects($this->any())
42+
->method('getManagerForClass')
43+
->will($this->returnValue($this->em));
44+
4045
return array(
4146
new CoreExtension(),
4247
new DoctrineOrmExtension($manager)

src/Symfony/Bridge/Doctrine/Tests/Logger/DbalLoggerTest.php

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

1212
namespace Symfony\Bridge\Doctrine\Tests\Logger;
1313

14+
use Symfony\Bridge\Doctrine\Logger\DbalLogger;
15+
1416
class DbalLoggerTest extends \PHPUnit_Framework_TestCase
1517
{
1618
/**
@@ -59,12 +61,38 @@ public function testLogNonUtf8()
5961
$dbalLogger
6062
->expects($this->once())
6163
->method('log')
62-
->with('SQL', array('utf8' => 'foo', 'nonutf8' => "\x7F\xFF"))
64+
->with('SQL', array('utf8' => 'foo', 'nonutf8' => DbalLogger::BINARY_DATA_VALUE))
6365
;
6466

6567
$dbalLogger->startQuery('SQL', array(
6668
'utf8' => 'foo',
67-
'nonutf8' => "\x7F\xFF"
69+
'nonutf8' => "\x7F\xFF",
70+
));
71+
}
72+
73+
public function testLogLongString()
74+
{
75+
$logger = $this->getMock('Symfony\\Component\\HttpKernel\\Log\\LoggerInterface');
76+
77+
$dbalLogger = $this
78+
->getMockBuilder('Symfony\\Bridge\\Doctrine\\Logger\\DbalLogger')
79+
->setConstructorArgs(array($logger, null))
80+
->setMethods(array('log'))
81+
->getMock()
82+
;
83+
84+
$shortString = str_repeat('a', DbalLogger::MAX_STRING_LENGTH);
85+
$longString = str_repeat('a', DbalLogger::MAX_STRING_LENGTH + 1);
86+
87+
$dbalLogger
88+
->expects($this->once())
89+
->method('log')
90+
->with('SQL', array('short' => $shortString, 'long' => substr($longString, DbalLogger::MAX_STRING_LENGTH - 6).' [...]'))
91+
;
92+
93+
$dbalLogger->startQuery('SQL', array(
94+
'short' => $shortString,
95+
'long' => $longString,
6896
));
6997
}
7098
}

0 commit comments

Comments
 (0)