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

前端之家收集整理的这篇文章主要介绍了php – Symfony CLI中的空转储()输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个简单的Symfony- shell脚本,以便在交互模式下测试我的Symfony应用程序:
  1. # bin/app_dev_cli.PHP
  2.  
  3. require __DIR__.'/../app/autoload.PHP';
  4.  
  5. $kernel = new AppKernel('dev',true);
  6. // Initialize bundles and container
  7. $kernel->boot();
  8.  
  9. // Useful global vars
  10. $container = $kernel->getContainer();
  11. $doctrine = $container->get('doctrine');
  12. $em = $doctrine->getManager();

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

  1. /path/to/symfony/project$PHP -a
  2. Interactive mode enabled
  3.  
  4. # Booting the Symfony-shell app
  5. PHP > require 'bin/app_dev_cli.PHP';
  6.  
  7. # Check if one service has been registered successfully
  8. PHP > dump( $container->has('some_service') );
  9.  
  10. # Test some service
  11. PHP > dump( $container->get('some_service')->run($param) );
  12.  
  13. # Manage some entities and DB data flow
  14. PHP > $apple = new AppBundle\Entity\Fruit('Apple');
  15. PHP > $em->persist($apple);
  16. PHP > $em->flush();
  17.  
  18. PHP > dump( $em->getRepository('AppBundle\Entity\Fluit')->findAll() );
  19.  
  20. # 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,但是:

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

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

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

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

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

  1. public static function dump($var)
  2. {
  3.  
  4. if (null === self::$handler) {
  5. /** ^--- this one **/
  6. $cloner = new VarCloner();
  7. $dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
  8. self::$handler = function ($var) use ($cloner,$dumper) {
  9. $dumper->dump($cloner->cloneVar($var));
  10. };
  11. }
  12. return call_user_func(self::$handler,$var);
  13. }

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

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

工作代码

  1. # bin/app_dev_cli.PHP
  2. <?PHP
  3. use Symfony\Component\VarDumper\VarDumper;
  4.  
  5. require __DIR__.'/../app/autoload.PHP';
  6.  
  7. $kernel = new AppKernel('dev',true);
  8. $kernel->boot();
  9.  
  10. /** This line (plus the use statement on top) does what you want **/
  11. VarDumper::setHandler(null);
  12.  
  13. // Useful global vars
  14. $container = $kernel->getContainer();
  15. $doctrine = $container->get('doctrine');
  16. $em = $doctrine->getManager();

运行时:

  1. $PHP -a
  2. Interactive shell
  3.  
  4. PHP > require 'bin/app_dev_cli.PHP';
  5. # bin/app_dev_cli.PHP
  6. PHP > dump('hi');
  7. "hi"

猜你在找的PHP相关文章