From c7cf6cfefe0367867d60f7b3ac384d2b39fc86a6 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 22 Oct 2014 08:47:25 +0200 Subject: [PATCH 1/3] [VarDumper] fix control chars styling --- src/Symfony/Component/VarDumper/Dumper/CliDumper.php | 10 +++------- .../Component/VarDumper/Tests/CliDumperTest.php | 2 +- .../Component/VarDumper/Tests/Fixtures/dumb-var.php | 2 +- .../Component/VarDumper/Tests/HtmlDumperTest.php | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php index 3b9ce6acdc98e..a48d88bf09f2f 100644 --- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php @@ -41,7 +41,7 @@ class CliDumper extends AbstractDumper 'meta' => '38;5;27', ); - protected static $controlCharsRx = "/\\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F/"; + protected static $controlCharsRx = '/[\x00-\x1F\x7F]/'; /** * Enables/disables colored output. @@ -321,17 +321,13 @@ protected function style($style, $value, $attr = array()) $this->colors = $this->supportsColors($this->outputStream); } - if (!$this->colors || '' === $value) { - return $value; - } - $style = $this->styles[$style]; - $cchr = "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m"; + $cchr = $this->colors ? "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m" : '%s'; $value = preg_replace_callback(self::$controlCharsRx, function ($r) use ($cchr) { return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0]))); }, $value); - return sprintf("\033[%sm%s\033[m", $style, $value); + return $this->colors ? sprintf("\033[%sm%s\033[m", $style, $value) : $value; } /** diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index 2c7aefe203620..c781212763452 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -57,7 +57,7 @@ public function testGet() 5 => -INF 6 => {$intMax} "str" => "déjà" - 7 => b"é" + 7 => b"é@" "[]" => [] "res" => :stream {@{$res1} wrapper_type: "plainfile" diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php index 6ffc8dd214388..380369a3c836d 100644 --- a/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php @@ -19,7 +19,7 @@ class DumbFoo $var = array( 'number' => 1, null, 'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX, - 'str' => "déjà", "\xE9", + 'str' => "déjà", "\xE9\x00", '[]' => array(), 'res' => $g, $h, diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index dcc7710efb51b..999c098601191 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -62,7 +62,7 @@ public function testGet() 5 => -INF 6 => {$intMax} "str" => "déjà" - 7 => b"é" + 7 => b"é@" "[]" => [] "res" => :stream {@{$res1} wrapper_type: "plainfile" From 5af0f899fe885a7dc18e03a61f47425727454ef4 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 22 Oct 2014 11:16:21 +0200 Subject: [PATCH 2/3] [EventDispatcher] perf optim --- .../ContainerAwareEventDispatcher.php | 27 +++++++++---------- .../ContainerAwareEventDispatcherTest.php | 2 -- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php b/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php index e97d427ea15eb..f19dc5d33b79b 100644 --- a/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php @@ -76,21 +76,18 @@ public function removeListener($eventName, $listener) { $this->lazyLoad($eventName); - if (isset($this->listeners[$eventName])) { - foreach ($this->listeners[$eventName] as $key => $l) { - foreach ($this->listenerIds[$eventName] as $i => $args) { - list($serviceId, $method, $priority) = $args; - if ($key === $serviceId.'.'.$method) { - if ($listener === array($l, $method)) { - unset($this->listeners[$eventName][$key]); - if (empty($this->listeners[$eventName])) { - unset($this->listeners[$eventName]); - } - unset($this->listenerIds[$eventName][$i]); - if (empty($this->listenerIds[$eventName])) { - unset($this->listenerIds[$eventName]); - } - } + if (isset($this->listenerIds[$eventName])) { + foreach ($this->listenerIds[$eventName] as $i => $args) { + list($serviceId, $method, $priority) = $args; + $key = $serviceId.'.'.$method; + if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) { + unset($this->listeners[$eventName][$key]); + if (empty($this->listeners[$eventName])) { + unset($this->listeners[$eventName]); + } + unset($this->listenerIds[$eventName][$i]); + if (empty($this->listenerIds[$eventName])) { + unset($this->listenerIds[$eventName]); } } } diff --git a/src/Symfony/Component/EventDispatcher/Tests/ContainerAwareEventDispatcherTest.php b/src/Symfony/Component/EventDispatcher/Tests/ContainerAwareEventDispatcherTest.php index fb3b4caa26624..eabde3da8e12b 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/ContainerAwareEventDispatcherTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/ContainerAwareEventDispatcherTest.php @@ -232,8 +232,6 @@ class SubscriberService implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - 'onEvent' => 'onEvent', - 'onEvent' => array('onEvent', 10), 'onEvent' => array('onEvent'), ); } From ac8efd92d1243e629834eda4621c291105d85412 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 22 Oct 2014 11:31:42 +0200 Subject: [PATCH 3/3] [HttpKernel] fix DebugHandlersListener --- .../EventListener/DebugHandlersListener.php | 53 +++++++++---------- .../DebugHandlersListenerTest.php | 2 - 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php b/src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php index 9d4258d54aaea..f50e4d6c3b9c1 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php @@ -15,7 +15,6 @@ use Symfony\Component\Debug\ErrorHandler; use Symfony\Component\Debug\ExceptionHandler; use Symfony\Component\EventDispatcher\Event; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\KernelEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -36,6 +35,7 @@ class DebugHandlersListener implements EventSubscriberInterface private $throwAt; private $scream; private $fileLinkFormat; + private $firstCall = true; /** * @param callable|null $exceptionHandler A handler that will be called on Exception @@ -58,38 +58,37 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $ /** * Configures the error handler. * - * @param Event|null $event The triggering event - * @param string|null $eventName The triggering event name - * @param EventDispatcherInterface|null $eventDispatcher The dispatcher used to trigger $event + * @param Event|null $event The triggering event */ - public function configure(Event $event = null, $eventName = null, EventDispatcherInterface $eventDispatcher = null) + public function configure(Event $event = null) { - if (null !== $eventDispatcher) { - foreach (array_keys(static::getSubscribedEvents()) as $name) { - $eventDispatcher->removeListener($name, array($this, 'configure')); - } + if (!$this->firstCall) { + return; } - $handler = set_error_handler('var_dump', 0); - $handler = is_array($handler) ? $handler[0] : null; - restore_error_handler(); - if ($handler instanceof ErrorHandler) { - if ($this->logger) { - $handler->setDefaultLogger($this->logger, $this->levels); - if (is_array($this->levels)) { - $scream = 0; - foreach ($this->levels as $type => $log) { - $scream |= $type; + $this->firstCall = false; + if ($this->logger || null !== $this->throwAt) { + $handler = set_error_handler('var_dump', 0); + $handler = is_array($handler) ? $handler[0] : null; + restore_error_handler(); + if ($handler instanceof ErrorHandler) { + if ($this->logger) { + $handler->setDefaultLogger($this->logger, $this->levels); + if (is_array($this->levels)) { + $scream = 0; + foreach ($this->levels as $type => $log) { + $scream |= $type; + } + } else { + $scream = null === $this->levels ? E_ALL | E_STRICT : $this->levels; } - } else { - $scream = null === $this->levels ? E_ALL | E_STRICT : $this->levels; + if ($this->scream) { + $handler->screamAt($scream); + } + $this->logger = $this->levels = null; } - if ($this->scream) { - $handler->screamAt($scream); + if (null !== $this->throwAt) { + $handler->throwAt($this->throwAt, true); } - $this->logger = $this->levels = null; - } - if (null !== $this->throwAt) { - $handler->throwAt($this->throwAt, true); } } if (!$this->exceptionHandler) { diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/DebugHandlersListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/DebugHandlersListenerTest.php index 976d1e938c41e..f3cd05e239dd3 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/DebugHandlersListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/DebugHandlersListenerTest.php @@ -95,8 +95,6 @@ public function testConsoleEvent() throw $exception; } - $this->assertSame(array(), $dispatcher->getListeners()); - $xHandler = $eHandler->setExceptionHandler('var_dump'); $this->assertInstanceOf('Closure', $xHandler);