1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Async; |
||
5 | |||
6 | use Exception; |
||
7 | use SuperClosure\Serializer; |
||
8 | use Symfony\Component\Console\Command\Command; |
||
9 | use Symfony\Component\Console\Input\InputArgument; |
||
10 | use Symfony\Component\Console\Input\InputInterface; |
||
11 | use Symfony\Component\Console\Output\OutputInterface; |
||
12 | |||
13 | class AsyncChildCommand extends Command |
||
14 | { |
||
15 | public const COMMAND_NAME = 'app:run-child-process'; |
||
16 | private const PARAM_NAME_JOB = 'job'; |
||
17 | private $serializer; |
||
18 | |||
19 | public function __construct(?string $name = null) |
||
20 | { |
||
21 | parent::__construct($name); |
||
22 | |||
23 | $this->serializer = new Serializer(); |
||
24 | } |
||
25 | |||
26 | public function execute(InputInterface $input, OutputInterface $output): int |
||
27 | { |
||
28 | try { |
||
29 | $job = $this->serializer->unserialize(base64_decode($input->getArgument(self::PARAM_NAME_JOB))); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
30 | |||
31 | ob_start(); |
||
32 | $jobResults = $job(); |
||
33 | $ob = ob_get_clean(); |
||
34 | $error = null; |
||
35 | } catch (Exception $exception) { |
||
36 | $jobResults = null; |
||
37 | $ob = null; |
||
38 | $error = $exception; |
||
39 | } |
||
40 | |||
41 | $output->writeln(base64_encode(serialize(new AsyncChildResponse($jobResults, $ob, $error)))); |
||
42 | |||
43 | return 0; |
||
44 | } |
||
45 | |||
46 | protected function configure(): void |
||
47 | { |
||
48 | $this |
||
49 | ->setName(self::COMMAND_NAME) |
||
50 | ->setDescription('Runs a child process.') |
||
51 | ->addArgument(self::PARAM_NAME_JOB, InputArgument::REQUIRED, 'Serialized callback job param.'); |
||
52 | } |
||
53 | } |