bug #12285 Various fixes (nicolas-grekas)

This PR was merged into the 2.6-dev branch.

Discussion
----------

Various fixes

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

A 3 in one PR (diff is best viewed [with whitespaces ignored](https://github.com/symfony/symfony/pull/12285/files?w=1)):
- changed the way the DebugHandlerListeners desactivates itself
- reduced a N×N iteration to a N one in ContainerAwareEventListener::removeListener
- fixed an issue in VarDumper

Commits
-------

ac8efd9 [HttpKernel] fix DebugHandlersListener
5af0f89 [EventDispatcher] perf optim
c7cf6cf [VarDumper] fix control chars styling
This commit is contained in:
Fabien Potencier 2014-10-26 08:50:57 +01:00
commit 96210c2e1f
8 changed files with 44 additions and 56 deletions

View File

@ -76,21 +76,18 @@ class ContainerAwareEventDispatcher extends EventDispatcher
{
$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]);
}
}
}

View File

@ -232,8 +232,6 @@ class SubscriberService implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
'onEvent' => 'onEvent',
'onEvent' => array('onEvent', 10),
'onEvent' => array('onEvent'),
);
}

View File

@ -15,7 +15,6 @@ use Psr\Log\LoggerInterface;
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 @@ class DebugHandlersListener implements EventSubscriberInterface
/**
* 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) {

View File

@ -95,8 +95,6 @@ class DebugHandlersListenerTest extends \PHPUnit_Framework_TestCase
throw $exception;
}
$this->assertSame(array(), $dispatcher->getListeners());
$xHandler = $eHandler->setExceptionHandler('var_dump');
$this->assertInstanceOf('Closure', $xHandler);

View File

@ -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 @@ class CliDumper extends AbstractDumper
$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;
}
/**

View File

@ -57,7 +57,7 @@ array:25 [
5 => -INF
6 => {$intMax}
"str" => "déjà"
7 => b"é"
7 => b"é@"
"[]" => []
"res" => :stream {@{$res1}
wrapper_type: "plainfile"

View File

@ -19,7 +19,7 @@ fclose($h);
$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,

View File

@ -62,7 +62,7 @@ class HtmlDumperTest extends \PHPUnit_Framework_TestCase
<span class=sf-dump-meta>5</span> => <span class=sf-dump-num>-INF</span>
<span class=sf-dump-meta>6</span> => <span class=sf-dump-num>{$intMax}</span>
"<span class=sf-dump-meta>str</span>" => "<span class=sf-dump-str title="4 characters">d&#233;j&#224;</span>"
<span class=sf-dump-meta>7</span> => b"<span class=sf-dump-str>&#233;</span>"
<span class=sf-dump-meta>7</span> => b"<span class=sf-dump-str title="2 binary or non-UTF-8 characters">&#233;<span class=sf-dump-cchr title=\\x00>@</span></span>"
"<span class=sf-dump-meta>[]</span>" => []
"<span class=sf-dump-meta>res</span>" => <abbr title="`stream` resource" class=sf-dump-note>:stream</abbr> {<a class=sf-dump-solo-ref>@{$res1}</a><samp>
<span class=sf-dump-meta>wrapper_type</span>: "<span class=sf-dump-str title="9 characters">plainfile</span>"