dpbz14739 2013-08-07 14:10
浏览 128
已采纳

登录Symfony2后运行控制台命令后台

I want to run a custom symfony2 console command background after login. I make a listener and try to use the process to run the command at background but the function not work well. Here is my code

class LoginListener
{
    protected $doctrine;
    private $RecommendJobService;
    public function __construct(Doctrine $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    public function onLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();

        if($user)
        {
        $process = new Process('ls -lsa');
        $process->start(function ($type, $buffer) {
                $command = $this->RecommendJobService;
                $input = new ArgvInput();
                $output = new ConsoleOutput();
                $command->execute($input, $output);
                echo "1";

        });
        }
    }
    public function setRecommendJobService($RecommendJobService) {
      $this->RecommendJobService = $RecommendJobService;
    }
}

Is there something wrong with my code? Thx for helping.

  • 写回答

1条回答 默认 最新

  • douxuanjie2692 2013-08-07 17:22
    关注

    Any variables that you need to access from inside your anonymous function you have to use a use statement. Further $this could conflict due to scope.

    $that = $this;
    $process->start(function ($type, $buffer) use ($that) {
        $command = $that->RecommendJobService;
        $input = new ArgvInput();
        $output = new ConsoleOutput();
        $command->execute($input, $output);
        echo "1";
    });
    

    Also you can take your anonymous function and test it outside of the start() method like this.

    $closure = function ($type, $buffer) use ($that) {
        $command = $that->RecommendJobService;
        $input = new ArgvInput();
        $output = new ConsoleOutput();
        $command->execute($input, $output);
        echo "1";
    };
    $closure();
    

    Then you can put some debugging in there and see if it runs. I'm not sure if echo is a good way to deal with the console. I would recommend Monolog or the $output->writeln($text); command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。