8000 [VarDumper] Some tweaks after Nicolas' commit & ServerDumperPlacehold… · symfony/http-kernel@138dad6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 138dad6

Browse files
committed
[VarDumper] Some tweaks after Nicolas' commit & ServerDumperPlaceholderCommand
1 parent 088c52e commit 138dad6

File tree

12 files changed

+95
-92
lines changed

12 files changed

+95
-92
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\DebugBundle\Command;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Console\Style\SymfonyStyle;
17+
use Symfony\Component\VarDumper\Command\ServerDumpCommand;
18+
use Symfony\Component\VarDumper\Server\DumpServer;
19+
20+
/**
21+
* A placeholder command easing VarDumper server discovery.
22+
*
23+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
24+
*
25+
* @internal
26+
*/
27+
class ServerDumpPlaceholderCommand extends ServerDumpCommand
28+
{
29+
public function __construct(DumpServer $server = null, array $descriptors = array())
30+
{
31+
parent::__construct(new class() extends DumpServer {
32+
public function __construct()
33+
{
34+
}
35+
}, $descriptors);
36+
}
37+
38+
protected function execute(InputInterface $input, OutputInterface $output)
39+
{
40+
(new SymfonyStyle($input, $output))->getErrorStyle()->warning('In order to use the VarDumper server, set the "debug.dump_destination" config option to "tcp://%env(VAR_DUMPER_SERVER)%"');
41+
42+
return 8;
43+
}
44+
}

src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function getConfigTreeBuilder()
4848
->end()
4949
->scalarNode('dump_destination')
5050
->info('A stream URL where dumps should be written to')
51-
->example('php://stderr, or tcp://%env(VAR_DUMPER_SERVER)% when using the `server:dump` command')
51+
->example('php://stderr, or tcp://%env(VAR_DUMPER_SERVER)% when using the "server:dump" command')
5252
->defaultNull()
5353
->end()
5454
->end()

src/Symfony/Bundle/DebugBundle/DependencyInjection/DebugExtension.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Symfony\Bundle\DebugBundle\DependencyInjection;
1313

14+
use Symfony\Bundle\DebugBundle\Command\ServerDumpPlaceholderCommand;
1415
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Extension\Extension;
1718
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1819
use Symfony\Component\DependencyInjection\Reference;
20+
use Symfony\Component\VarDumper\Dumper\ServerDumper;
1921

2022
/**
2123
* DebugExtension.
@@ -43,11 +45,18 @@ public function load(array $configs, ContainerBuilder $container)
4345
if (null === $config['dump_destination']) {
4446
//no-op
4547
} elseif (0 === strpos($config['dump_destination'], 'tcp://')) {
48+
$serverDumperHost = $config['dump_destination'];
4649
$container->getDefinition('debug.dump_listener')
4750
->replaceArgument(1, new Reference('var_dumper.server_dumper'))
4851
;
49-
$container->getDefinition('var_dumper.command.server_dump')
50-
->replaceArgument(1, new Reference('var_dumper.server_dumper'))
52+
$container->getDefinition('data_collector.dump')
53+
->replaceArgument(4, new Reference('var_dumper.server_dumper'))
54+
;
55+
$container->getDefinition('var_dumper.dump_server')
56+
->replaceArgument(0, $serverDumperHost)
57+
;
58+
$container->getDefinition('var_dumper.server_dumper')
59+
->replaceArgument(0, $serverDumperHost)
5160
;
5261
} else {
5362
$container->getDefinition('var_dumper.cli_dumper')
@@ -57,6 +66,13 @@ public function load(array $configs, ContainerBuilder $container)
5766
->replaceArgument(4, new Reference('var_dumper.cli_dumper'))
5867
;
5968
}
69+
70+
if (!isset($serverDumperHost)) {
71+
$container->getDefinition('var_dumper.command.server_dump')->setClass(ServerDumpPlaceholderCommand::class);
72+
if (!class_exists(ServerDumper::class)) {
73+
$container->removeDefinition('var_dumper.command.server_dump');
74+
}
75+
}
6076
}
6177

6278
/**

src/Symfony/Bundle/DebugBundle/Resources/config/services.xml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

7+
<parameters>
8+
<parameter key="env(VAR_DUMPER_SERVER)">127.0.0.1:9912</parameter>
9+
</parameters>
10+
711
<services>
812
<defaults public="false" />
913

10-
<parameter key="env(VAR_DUMPER_SERVER)">127.0.0.1:9912</parameter>
11-
1214
<service id="twig.extension.dump" class="Symfony\Bridge\Twig\Extension\DumpExtension">
1315
<tag name="twig.extension" />
1416
<argument type="service" id="var_dumper.cloner" />
@@ -70,7 +72,14 @@
7072
</argument>
7173
</service>
7274

75+
<service id="var_dumper.dump_server" class="Symfony\Component\VarDumper\Server\DumpServer">
76+
<argument /> <!-- server host -->
77+
<argument type="service" id="logger" on-invalid="null" />
78+
<tag name="monolog.logger" channel="debug" />
79+
</service>
80+
7381
<service id="var_dumper.command.server_dump" class="Symfony\Component\VarDumper\Command\ServerDumpCommand">
82+
<argument type="service" id="var_dumper.dump_server" />
7483
<argument type="collection">
7584
<argument type="service" key="cli">
7685
<service class="Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor">
@@ -83,9 +92,7 @@
8392
</service>
8493
</argument>
8594
</argument>
86-
<argument type="service" id="logger" on-invalid="null" />
8795
<tag name="console.command" command="server:dump" />
88-
<tag name="monolog.logger" channel="debug" />
8996
</service>
9097
</services>
9198
</container>

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null,
5858
&$this->clonesCount,
5959
);
6060

61-
$this->sourceContextProvider = $dumper instanceof ServerDumper ? $dumper->getContextProviders()['source'] : new SourceContextProvider($this->charset);
61+
$this->sourceContextProvider = $dumper instanceof ServerDumper && isset($dumper->getContextProviders()['source']) ? $dumper->getContextProviders()['source'] : new SourceContextProvider($this->charset);
6262
}
6363

6464
public function __clone()

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
namespace Symfony\Component\HttpKernel\Tests\DataCollector;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
1615
use Symfony\Component\HttpFoundation\Request;
1716
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
1818
use Symfony\Component\VarDumper\Cloner\Data;
19-
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
2019
use Symfony\Component\VarDumper\Dumper\ServerDumper;
2120

2221
/**
@@ -62,33 +61,11 @@ public function testDumpWithServerDumper()
6261
$data = new Data(array(array(123)));
6362

6463
// Server is up, server dumper is used
65-
$serverDumper = $this->getMockBuilder(ServerDumper::class)->getMock();
64+
$serverDumper = $this->getMockBuilder(ServerDumper::class)->disableOriginalConstructor()->getMock();
6665
$serverDumper->expects($this->once())->method('dump');
6766
$serverDumper->method('isServerListening')->willReturn(true);
6867

69-
// configured dumper is never used
70-
$dumper = $this->getMockBuilder(DataDumperInterface::class)->getMock();
71-
$dumper->expects($this->never())->method('dump');
72-
73-
$collector = new DumpDataCollector(null, null, null, null, $dumper, $serverDumper);
74-
$collector->dump($data);
75-
76-
// Collect doesn't re-trigger dump
77-
ob_start();
78-
$collector->collect(new Request(), new Response());
79-
$this->assertEmpty(ob_get_clean());
80-
$this->assertStringMatchesFormat('a:3:{i:0;a:5:{s:4:"data";%c:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
81-
82-
// Server is down, server dumper is never used
83-
$serverDumper = $this->getMockBuilder(ServerDumper::class)->getMock();
84-
$serverDumper->expects($this->never())->method('dump');
85-
$serverDumper->method('isServerListening')->willReturn(false);
86-
87-
// configured dumper is used
88-
$dumper = $this->getMockBuilder(DataDumperInterface::class)->getMock();
89-
$dumper->expects($this->exactly(2))->method('dump'); // called twice in doDump if not a CLiDumper instance
90-
91-
$collector = new DumpDataCollector(null, null, null, null, $dumper, $serverDumper);
68+
$collector = new DumpDataCollector(null, null, null, null, $serverDumper);
9269
$collector->dump($data);
9370

9471
// Collect doesn't re-trigger dump
@@ -155,34 +132,4 @@ public function testFlush()
155132
$collector->__destruct();
156133
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
157134
}
158-
159-
public function testFlushWithServerDumper()
160-
{
161-
$data = new Data(array(array(456)));
162-
163-
// Server is up, server dumper is used
164-
$serverDumper = $this->getMockBuilder(ServerDumper::class)->getMock();
165-
$serverDumper->expects($this->once())->method('dump');
166-
$serverDumper->method('isServerListening')->willReturn(true);
167-
168-
$collector = new DumpDataCollector(null, null, null, null, null, $serverDumper);
169-
$collector->dump($data);
170-
171-
ob_start();
172-
$collector->__destruct();
173-
$this->assertEmpty(ob_get_clean());
174-
175-
// Server is down, buffered dump is flushed on destruct
176-
$serverDumper = $this->getMockBuilder(ServerDumper::class)->getMock();
177-
$serverDumper->expects($this->never())->method('dump');
178-
$serverDumper->method('isServerListening')->willReturn(false);
179-
180-
$collector = new DumpDataCollector(null, null, null, null, null, $serverDumper);
181-
$collector->dump($data);
182-
$line = __LINE__ - 1;
183-
184-
ob_start();
185-
$collector->__destruct();
186-
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
187-
}
188135
}

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* added a `ServerDumper` to send serialized Data clones to a server
88
* added a `ServerDumpCommand` and `DumpServer` to run a server collecting
99
and displaying dumps on a single place with multiple formats support
10-
* added `CliDescriptor` and `HtmlDescriptor` descriptors for `server:dump` cli and html formats support
10+
* added `CliDescriptor` and `HtmlDescriptor` descriptors for `server:dump` CLI and HTML formats support
1111

1212
4.0.0
1313
-----

src/Symfony/Component/VarDumper/Command/Descriptor/CliDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(CliDumper $dumper)
3636

3737
public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
3838
{
39-
$io = new SymfonyStyle(new ArrayInput(array()), $output);
39+
$io = $output instanceof SymfonyStyle ? $output : new SymfonyStyle(new ArrayInput(array()), $output);
4040

4141
$rows = array(array('date', date('r', $context['timestamp'])));
4242
$lastIdentifier = $this->lastIdentifier;

src/Symfony/Component/VarDumper/Command/ServerDumpCommand.php

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

1212
namespace Symfony\Component\VarDumper\Command;
1313

14-
use Psr\Log\LoggerInterface;
1514
use Symfony\Component\Console\Command\Command;
1615
use Symfony\Component\Console\Exception\InvalidArgumentException;
1716
use Symfony\Component\Console\Input\InputInterface;
@@ -37,14 +36,14 @@ class ServerDumpCommand extends Command
3736
{
3837
protected static $defaultName = 'server:dump';
3938

40-
private $logger;
39+
private $server;
4140

4241
/** @var DumpDescriptorInterface[] */
4342
private $descriptors;
4443

45-
public function __construct(array $descriptors = array(), LoggerInterface $logger = null)
44+
public function __construct(DumpServer $server, array $descriptors = array())
4645
{
47-
$this->logger = $logger;
46+
$this->server = $server;
4847
$this->descriptors = $descriptors + array(
4948
'cli' => new CliDescriptor(new CliDumper()),
5049
'html' => new HtmlDescriptor(new HtmlDumper()),
@@ -58,7 +57,7 @@ protected function configure()
5857
$availableFormats = implode(', ', array_keys($this->descriptors));
5958

6059
$this
61-
->addOption('format', null, InputOption::VALUE_REQUIRED, "The output format ($availableFormats)", 'cli')
60+
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', $availableFormats), 'cli')
6261
->setDescription('Starts a dump server that collects and displays dumps in a single place')
6362
->setHelp(<<<'EOF'
6463
<info>%command.name%</info> starts a dump server that collects and displays
@@ -88,14 +87,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
8887
$errorIo = $io->getErrorStyle();
8988
$errorIo->title('Symfony Var Dumper Server');
9089

91-
$server = new DumpServer(null, $this->logger);
92-
$server->start();
90+
$this->server->start();
9391

94-
$errorIo->success(sprintf('Server listening on %s', $server->getHost()));
92+
$errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
9593
$errorIo->comment('Quit the server with CONTROL-C.');
9694

97-
$server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $output) {
98-
$descriptor->describe($output, $data, $context, $clientId);
95+
$this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
96+
$descriptor->describe($io, $data, $context, $clientId);
9997
});
10098
}
10199
}

src/Symfony/Component/VarDumper/Dumper/ServerDumper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\VarDumper\Cloner\Data;
1515
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
16-
use Symfony\Component\VarDumper\Server\DumpServer;
1716

1817
/**
1918
* ServerDumper forwards serialized Data clones to a server.
@@ -43,7 +42,7 @@ public function __construct(string $host, DataDumperInterface $wrappedDumper = n
4342
$this->contextProviders = $contextProviders;
4443
}
4544

46-
public function getContextProviders(): ?array
45+
public function getContextProviders(): array
4746
{
4847
return $this->contextProviders;
4948
}

0 commit comments

Comments
 (0)
0