php – Symfony CLI中的空转储()输出

前端之家收集整理的这篇文章主要介绍了php – Symfony CLI中的空转储()输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个简单的Symfony- shell脚本,以便在交互模式下测试我的Symfony应用程序:
# 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();

稍后,打开PHP交互模式并包含以前的脚本我可以快速完成一些任务:

/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.

这里的问题是dump()函数什么都没显示.我期待一个彩色的命令行输出,但是我试着用echo和var_dump()向我展示期望值,但对于对象,主要是输出密集且不可读.在这方面,VarDumper Component documentation说:

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. […]

默认情况下,这对我不起作用,我确信PHP_SAPI是cli.此外,I found a workaround将debug.dump_destination设置为PHP:// stderr,但是:

# PHP interactive mode:
PHP > dump("test");
hp shell code on line 1: // <---- show the code line always
"test"

我不喜欢这个输出,也没有为此目的更改配置.

有什么想法dump()函数会发生什么,为什么它什么都没显示?谢谢.

出于某种原因,看起来你要做的实际上是将特定处理程序注入VarDumper

因此条件null === self :: $handler在该类代码的波纹管重现片段中始终为false,因此,未设置“cli”=== PHP_SAPI所需的转储器.

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);
}

来源:Symfony/Component/VarDumper/VarDumper.php

现在,了解这一点,您的解决方案就像将VarDumper的处理程序设置回null一样简单

工作代码

# 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();

运行时:

$PHP -a
Interactive shell

PHP > require 'bin/app_dev_cli.PHP';
# bin/app_dev_cli.PHP 
PHP > dump('hi'); 
"hi"
原文链接:https://www.f2er.com/php/137325.html

猜你在找的PHP相关文章