From c92fcdb2739105f6e4b25d05e9233a5f6c52ada2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Fri, 16 Oct 2015 16:24:48 +0200 Subject: [PATCH] Added placeholders to all log messages instead of hardcoded values --- .../Debug/TraceableEventDispatcher.php | 6 +++--- .../Tests/Debug/TraceableEventDispatcherTest.php | 10 +++++----- .../HttpKernel/EventListener/RouterListener.php | 3 ++- .../Tests/EventListener/RouterListenerTest.php | 8 ++++---- .../Component/Routing/Generator/UrlGenerator.php | 12 +++++------- .../Guard/Firewall/GuardAuthenticationListener.php | 2 +- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php index 35df8162b1..cbe6ff656a 100644 --- a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php @@ -259,7 +259,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface $info = $this->getListenerInfo($listener->getWrappedListener(), $eventName); if ($listener->wasCalled()) { if (null !== $this->logger) { - $this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty'])); + $this->logger->debug('Notified event "{event}" to listener "{listener}".', array('event' => $eventName, 'listener' => $info['pretty'])); } if (!isset($this->called[$eventName])) { @@ -270,12 +270,12 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface } if (null !== $this->logger && $skipped) { - $this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName)); + $this->logger->debug('Listener "{listener}" was not called for event "{event}".', array('listener' => $info['pretty'], 'event' => $eventName)); } if ($listener->stoppedPropagation()) { if (null !== $this->logger) { - $this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName)); + $this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', array('listener' => $info['pretty'], 'event' => $eventName)); } $skipped = true; diff --git a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php index 1d4a8c8506..e58dbe1be8 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php @@ -110,8 +110,8 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase $tdispatcher->addListener('foo', $listener1 = function () {}); $tdispatcher->addListener('foo', $listener2 = function () {}); - $logger->expects($this->at(0))->method('debug')->with('Notified event "foo" to listener "closure".'); - $logger->expects($this->at(1))->method('debug')->with('Notified event "foo" to listener "closure".'); + $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure')); + $logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure')); $tdispatcher->dispatch('foo'); } @@ -125,9 +125,9 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); }); $tdispatcher->addListener('foo', $listener2 = function () {}); - $logger->expects($this->at(0))->method('debug')->with('Notified event "foo" to listener "closure".'); - $logger->expects($this->at(1))->method('debug')->with('Listener "closure" stopped propagation of the event "foo".'); - $logger->expects($this->at(2))->method('debug')->with('Listener "closure" was not called for event "foo".'); + $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure')); + $logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', array('event' => 'foo', 'listener' => 'closure')); + $logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', array('event' => 'foo', 'listener' => 'closure')); $tdispatcher->dispatch('foo'); } diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php index 01a51b13a1..dbf6bff15f 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php @@ -104,7 +104,8 @@ class RouterListener implements EventSubscriberInterface } if (null !== $this->logger) { - $this->logger->info(sprintf('Matched route "%s".', isset($parameters['_route']) ? $parameters['_route'] : 'n/a'), array( + $this->logger->info('Matched route "{route}".', array( + 'route' => isset($parameters['_route']) ? $parameters['_route'] : 'n/a', 'route_parameters' => $parameters, 'request_uri' => $request->getUri(), )); diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php index d0bc61a441..1e6eb1ca1f 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php @@ -132,7 +132,7 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase /** * @dataProvider getLoggingParameterData */ - public function testLoggingParameter($parameter, $log) + public function testLoggingParameter($parameter, $log, $parameters) { $requestMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface'); $requestMatcher->expects($this->once()) @@ -142,7 +142,7 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase $logger = $this->getMock('Psr\Log\LoggerInterface'); $logger->expects($this->once()) ->method('info') - ->with($this->equalTo($log)); + ->with($this->equalTo($log), $this->equalTo($parameters)); $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); $request = Request::create('http://localhost/'); @@ -154,8 +154,8 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase public function getLoggingParameterData() { return array( - array(array('_route' => 'foo'), 'Matched route "foo".'), - array(array(), 'Matched route "n/a".'), + array(array('_route' => 'foo'), 'Matched route "{route}".', array('route' => 'foo', 'route_parameters' => array('_route' => 'foo'), 'request_uri' => 'http://localhost/')), + array(array(), 'Matched route "{route}".', array('route' => 'n/a', 'route_parameters' => array(), 'request_uri' => 'http://localhost/')), ); } } diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index 3b80b52b2c..b7ec786b4e 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -153,18 +153,18 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt $url = ''; $optional = true; + $message = 'Parameter "{parameter}" for route "{route}" must match "{expected}" ("{given}" given) to generate a corresponding URL.'; foreach ($tokens as $token) { if ('variable' === $token[0]) { if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) { // check requirement if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) { - $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); if ($this->strictRequirements) { - throw new InvalidParameterException($message); + throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]]))); } if ($this->logger) { - $this->logger->error($message); + $this->logger->error($message, array('parameter' => $token[3], 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$token[3]])); } return; @@ -222,14 +222,12 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt foreach ($hostTokens as $token) { if ('variable' === $token[0]) { if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) { - $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]); - if ($this->strictRequirements) { - throw new InvalidParameterException($message); + throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]]))); } if ($this->logger) { - $this->logger->error($message); + $this->logger->error($message, array('parameter' => $token[3], 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$token[3]])); } return; diff --git a/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php b/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php index ed0a36e9dc..59d5d29c27 100644 --- a/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php +++ b/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php @@ -78,7 +78,7 @@ class GuardAuthenticationListener implements ListenerInterface if ($event->hasResponse()) { if (null !== $this->logger) { - $this->logger->debug(sprintf('The "%s" authenticator set the response. Any later authenticator will not be called', get_class($guardAuthenticator))); + $this->logger->debug('The "{authenticator}" authenticator set the response. Any later authenticator will not be called', array('authenticator' => get_class($guardAuthenticator))); } break;