<?php
namespace App\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'Sname',
description: 'Add a short description for your command',
)]
class SnameCommand extends Command
{
protected function configure(): void
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$t1 = false;
$t2 = false;
$reg = [];
$fId = 1;
$reg[] = new \Fiber(function () use ($fId) {
for ($i = 1; $i < 10; $i++) {
echo $fId . ':' . $i;
echo PHP_EOL;
if ($i % 3 == 0) {
\Fiber::suspend(new SuspendData(Status::Running));
}
}
\Fiber::suspend(new SuspendData(Status::Stop));
});
$fId++;
$reg[] = new \Fiber(function () use ($fId) {
for ($i = 1; $i < 10; $i++) {
echo $fId . ':' . $i;
echo PHP_EOL;
\Fiber::suspend(new SuspendData(Status::Running));
}
\Fiber::suspend(new SuspendData(Status::Stop));
});
$startTag = true;
while (count($reg) > 0) {
if ($startTag) foreach ($reg as $pI) {
$pI->start();
$startTag = false;
}
foreach ($reg as $key => $pI) {
$r = $pI->resume();
if ($r->status === Status::Stop) {
unset($reg[$key]);
}
}
}
return Command::SUCCESS;
}
}
class SuspendData
{
public readonly Status $status;
public function __construct($status)
{
$this->status = $status;
}
}
enum Status
{
case Stop;
case Running;
}
|