From 02ba802d6b5010390894b162177c67939afaa14f Mon Sep 17 00:00:00 2001 From: Allen Taylor Date: Thu, 31 Oct 2019 19:09:30 +0100 Subject: [PATCH] configured var dump server --- DEVELOPMENT.md | 21 +++++ config/app.php | 1 + config/config.default.php | 7 ++ src/Helpers/DumpServerServiceProvider.php | 55 ++++++++++++ .../Helpers/DumpServerServiceProviderTest.php | 89 +++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 src/Helpers/DumpServerServiceProvider.php create mode 100644 tests/Unit/Helpers/DumpServerServiceProviderTest.php diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index cdac8699..b657dc97 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -54,6 +54,27 @@ at least `phpdbg -qrr`(which has problems with switch case statements) as using php -d pcov.enabled=1 vendor/bin/phpunit --testsuite Unit --coverage-text ``` +### Var Dump server +Symfony Var Dump server is configured to allow for easier debugging. It is not meant as a replacement for xdebug but can actually be used together with xdebug. +This Var Dump Server is especially useful for when you want to debug a request without messing up the output e.g API calls ot HTML layout. + +To use simply call the method `dump` and pass the arguments in exactly the same way you would when using `var_dump`. + +This will send the output to the Var Dump server which can be viewed in the terminal. +This does however require that you start the var-dump-server otherwise the output will be printed in your browser + +You can also `dump` and `die` if you wish to not let your code continue any further by calling the `dd` method + +To view the output of `dump` call the following commands: + +```bash +vendor/bin/var-dump-server +# or for running in docker +docker exec -it engelsystem_dev_es_php_fpm_1 vendor/bin/var-dump-server +``` + +For more information check out the Var Dump Server documentation: [Symfony VarDumper](https://symfony.com/components/VarDumper) + ## Translation We use gettext. You may use POEdit to extract new texts from the sourcecode. Please config POEdit to extract also the twig template files using the following settings: https://gist.github.com/jlambe/a868d9b63d70902a12254ce47069d0e6 diff --git a/config/app.php b/config/app.php index f05a85d1..b64baec6 100644 --- a/config/app.php +++ b/config/app.php @@ -33,6 +33,7 @@ return [ \Engelsystem\Helpers\VersionServiceProvider::class, \Engelsystem\Mail\MailerServiceProvider::class, \Engelsystem\Http\HttpClientServiceProvider::class, + \Engelsystem\Helpers\DumpServerServiceProvider::class ], // Application middleware diff --git a/config/config.default.php b/config/config.default.php index 8b279c65..06c282c6 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -263,4 +263,11 @@ return [ . 'you want to to contribute, have found any [bugs](https://github.com/engelsystem/engelsystem/issues) ' . 'or need help.' ], + + // var dump server + 'var_dump_server' => [ + 'host' => '127.0.0.1', + 'port' => '9912', + 'enable' => false, + ], ]; diff --git a/src/Helpers/DumpServerServiceProvider.php b/src/Helpers/DumpServerServiceProvider.php new file mode 100644 index 00000000..2580a963 --- /dev/null +++ b/src/Helpers/DumpServerServiceProvider.php @@ -0,0 +1,55 @@ +app; + + /** @var Config $config */ + $config = $app->get('config'); + // setup var dump server to use for easier debugging + $varDumpServerConfig = $config->get('var_dump_server'); + + if ( + !$varDumpServerConfig['enable'] + || $config->get('environment') !== 'development' + || !class_exists(ServerDumper::class) + ) { + return; + } + + $dumper = new ServerDumper( + 'tcp://' . $varDumpServerConfig['host'] . ':' . $varDumpServerConfig['port'], + in_array(PHP_SAPI, ['cli', 'phpdbg']) ? $app->get(CliDumper::class) : $app->get(HtmlDumper::class), + [ + 'cli' => new CliContextProvider(), + 'source' => new SourceContextProvider(), + ] + ); + + $cloner = $app->get(VarCloner::class); + + VarDumper::setHandler( + // @codeCoverageIgnoreStart + static function ($var) use ($cloner, $dumper) { + $dumper->dump($cloner->cloneVar($var)); + } + // @codeCoverageIgnoreEnd + ); + } +} diff --git a/tests/Unit/Helpers/DumpServerServiceProviderTest.php b/tests/Unit/Helpers/DumpServerServiceProviderTest.php new file mode 100644 index 00000000..3b494702 --- /dev/null +++ b/tests/Unit/Helpers/DumpServerServiceProviderTest.php @@ -0,0 +1,89 @@ + 'localhost', + 'port' => 80, + 'enable' => true + ]; + + $config = new Config(); + $config->set('var_dump_server', $varDumpServerConfig); + $config->set('environment', 'development'); + + // mock to test that the code has passed the enabled checks and started to configure the var dump server + $app = $this->getApp(['get']); + + $app->expects(self::exactly(3)) + ->method('get') + ->withConsecutive( + ['config'], + [CliDumper::class], + [VarCloner::class] + )->willReturnOnConsecutiveCalls( + $config, + new CliDumper(), + new VarCloner() + ); + + $dumpServiceProvider = new DumpServerServiceProvider($app); + $dumpServiceProvider->register(); + } + + public function notEnabledDataProvider(): array + { + return [ + [false, 'development'], + [false, 'production'], + [true, 'production'], + ]; + } + + /** + * @covers \Engelsystem\Helpers\DumpServerServiceProvider::register + * @dataProvider notEnabledDataProvider + * + * @param bool $enable + * @param string $environment + */ + public function testRegisterShouldNotEnable(bool $enable, string $environment): void + { + $varDumpServerConfig = [ + 'host' => 'localhost', + 'port' => 80, + 'enable' => $enable + ]; + + $config = new Config(); + $config->set('var_dump_server', $varDumpServerConfig); + $config->set('environment', $environment); + + // asset get is called once only + $app = $this->getApp(['get']); + $app->expects(self::once()) + ->method('get') + ->willReturn($config); + + $dumpServiceProvider = new DumpServerServiceProvider($app); + $dumpServiceProvider->register(); + } +}