mirror of
https://github.com/danog/loop.git
synced 2024-12-02 09:17:51 +01:00
90 lines
1.6 KiB
PHP
90 lines
1.6 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
require 'vendor/autoload.php';
|
|
|
|
use danog\Loop\Loop;
|
|
|
|
use function Amp\delay;
|
|
|
|
class MyLoop extends Loop
|
|
{
|
|
/**
|
|
* Callable.
|
|
*
|
|
* @var callable
|
|
*/
|
|
private $callable;
|
|
/**
|
|
* Loop name.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $name;
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param callable $callable Callable
|
|
* @param string $name Loop name
|
|
*/
|
|
public function __construct(callable $callable, string $name)
|
|
{
|
|
$this->callable = $callable;
|
|
$this->name = $name;
|
|
}
|
|
/**
|
|
* Main loop.
|
|
*/
|
|
public function loop(): void
|
|
{
|
|
$callable = $this->callable;
|
|
|
|
$number = 0;
|
|
while (true) {
|
|
$number = $callable($number);
|
|
echo "$this: $number".PHP_EOL;
|
|
}
|
|
}
|
|
|
|
// Optionally, we can also define logging methods
|
|
/**
|
|
* Started loop.
|
|
*
|
|
*/
|
|
protected function startedLoop(): void
|
|
{
|
|
parent::startedLoop();
|
|
echo "Started loop $this!".PHP_EOL;
|
|
}
|
|
/**
|
|
* Exited loop.
|
|
*
|
|
*/
|
|
protected function exitedLoop(): void
|
|
{
|
|
parent::exitedLoop();
|
|
echo "Exited loop $this!".PHP_EOL;
|
|
}
|
|
// End of logging methods
|
|
|
|
/**
|
|
* Get loop name.
|
|
*
|
|
*/
|
|
public function __toString(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
}
|
|
|
|
$function = function (int $number) {
|
|
delay(1);
|
|
return $number + 1;
|
|
};
|
|
$loops = [];
|
|
for ($x = 0; $x < 10; $x++) {
|
|
$loop = new MyLoop($function, "Loop number $x");
|
|
$loop->start();
|
|
delay(0.1);
|
|
$loops []= $loop;
|
|
}
|