I'm building a simple Symfony-shell script to test my Symfony app on interactive mode:
# bin/app_dev_cli.php
require __DIR__.'/../app/autoload.php';
$kernel = new AppKernel('dev', true);
// Initialize bundles and container
$kernel->boot();
// Useful global vars
$container = $kernel->getContainer();
$doctrine = $container->get('doctrine');
$em = $doctrine->getManager();
Later, opening PHP interactive mode and including the previous script I can do some tasks quickly:
/path/to/symfony/project$ php -a
Interactive mode enabled
# Booting the Symfony-shell app
php > require 'bin/app_dev_cli.php';
# Check if one service has been registered successfully
php > dump( $container->has('some_service') );
# Test some service
php > dump( $container->get('some_service')->run($param) );
# Manage some entities and DB data flow
php > $apple = new AppBundle\Entity\Fruit('Apple');
php > $em->persist($apple);
php > $em->flush();
php > dump( $em->getRepository('AppBundle\Entity\Fluit')->findAll() );
# etc.
The problem here is that the dump()
function shows nothing. I was expecting a colored command line output, however I tried this with echo
or var_dump()
and it works, but for objects mainly the output is dense and unreadable. In this direction the VarDumper Component documentation says:
By default, the output format and destination are selected based on your current PHP SAPI [...] * On the command line (CLI SAPI), the output is written on STDOUT. [...]
That's not working for me by default and I'm sure that PHP_SAPI
is cli
. Besides, I found a workaround setting debug.dump_destination
to php://stderr
, BUT:
# php interactive mode:
php > dump("test");
hp shell code on line 1: // <---- show the code line always
"test"
I don't like this output and neither change the config for this purpose only.
Any thoughts what happens with the dump()
function and why it shows nothing? Thanks.
For some reason, it looks like what you are trying to do is actually dependency injecting a specific handler into VarDumper
So the condition null === self::$handler
is always false in the below reproduced snippet of that class code, and, thus, the dumper you expect for 'cli' === PHP_SAPI
is not set.
public static function dump($var)
{
if (null === self::$handler) {
/** ^--- this one **/
$cloner = new VarCloner();
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
}
return call_user_func(self::$handler, $var);
}
source: Symfony/Component/VarDumper/VarDumper.php
Now, knowing this, your solution is as easy as just setting the handler of VarDumper
back to null
Working code:
# bin/app_dev_cli.php
<?php
use Symfony\Component\VarDumper\VarDumper;
require __DIR__.'/../app/autoload.php';
$kernel = new AppKernel('dev', true);
$kernel->boot();
/** This line (plus the use statement on top) does what you want **/
VarDumper::setHandler(null);
// Useful global vars
$container = $kernel->getContainer();
$doctrine = $container->get('doctrine');
$em = $doctrine->getManager();
When run:
$ php -a
Interactive shell
php > require 'bin/app_dev_cli.php';
# bin/app_dev_cli.php
php > dump('hi');
"hi"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With