|
| 1 | +<?php |
| 2 | + |
| 3 | +if (!class_exists(Fiber::class)) { |
| 4 | + class Fiber |
| 5 | + { |
| 6 | + /** @var callable */ |
| 7 | + private $callback; |
| 8 | + |
| 9 | + /** @var mixed */ |
| 10 | + private $return; |
| 11 | + |
| 12 | + /** @var bool */ |
| 13 | + private $started = false; |
| 14 | + |
| 15 | + /** @var bool */ |
| 16 | + private $terminated = false; |
| 17 | + |
| 18 | + /** @var bool */ |
| 19 | + private static $halt = false; |
| 20 | + |
| 21 | + /** @var ?Fiber */ |
| 22 | + private static $suspended = null; |
| 23 | + |
| 24 | + public function __construct(callable $callback) |
| 25 | + { |
| 26 | + $this->callback = $callback; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param mixed ...$args |
| 31 | + * @return mixed |
| 32 | + * @throws \Throwable |
| 33 | + */ |
| 34 | + public function start(...$args) |
| 35 | + { |
| 36 | + if ($this->started) { |
| 37 | + throw new \FiberError(); |
| 38 | + } |
| 39 | + $this->started = true; |
| 40 | + |
| 41 | + if (self::$halt) { |
| 42 | + assert(self::$suspended === null); |
| 43 | + self::$suspended = $this; |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + return $this->return = ($this->callback)(...$args); |
| 49 | + } finally { |
| 50 | + $this->terminated = true; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param mixed $value |
| 56 | + * @return mixed |
| 57 | + * @throws \BadMethodCallException |
| 58 | + */ |
| 59 | + public function resume($value = null) |
| 60 | + { |
| 61 | + throw new \BadMethodCallException(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param Throwable $exception |
| 66 | + * @return mixed |
| 67 | + * @throws \BadMethodCallException |
| 68 | + */ |
| 69 | + public function throw(Throwable $exception) |
| 70 | + { |
| 71 | + throw new \BadMethodCallException(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return mixed |
| 76 | + * @throws FiberError |
| 77 | + */ |
| 78 | + public function getReturn() |
| 79 | + { |
| 80 | + if (!$this->terminated) { |
| 81 | + throw new \FiberError(); |
| 82 | + } |
| 83 | + |
| 84 | + return $this->return; |
| 85 | + } |
| 86 | + |
| 87 | + public function isStarted(): bool |
| 88 | + { |
| 89 | + return $this->started; |
| 90 | + } |
| 91 | + |
| 92 | + public function isSuspended(): bool |
| 93 | + { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + public function isRunning(): bool |
| 98 | + { |
| 99 | + return $this->started && !$this->terminated; |
| 100 | + } |
| 101 | + |
| 102 | + public function isTerminated(): bool |
| 103 | + { |
| 104 | + return $this->terminated; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param mixed $value |
| 109 | + * @return mixed |
| 110 | + * @throws \Throwable |
| 111 | + */ |
| 112 | + public static function suspend($value = null) |
| 113 | + { |
| 114 | + throw new \BadMethodCallException(); |
| 115 | + } |
| 116 | + |
| 117 | + public static function getCurrent(): ?Fiber |
| 118 | + { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @internal |
| 124 | + */ |
| 125 | + public static function mockSuspend(): void |
| 126 | + { |
| 127 | + assert(self::$halt === false); |
| 128 | + self::$halt = true; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @internal |
| 133 | + * @throws void |
| 134 | + */ |
| 135 | + public static function mockResume(): void |
| 136 | + { |
| 137 | + assert(self::$halt === true); |
| 138 | + assert(self::$suspended instanceof self); |
| 139 | + |
| 140 | + $fiber = self::$suspended; |
| 141 | + assert($fiber->started); |
| 142 | + assert(!$fiber->terminated); |
| 143 | + |
| 144 | + self::$halt = false; |
| 145 | + self::$suspended = null; |
| 146 | + |
| 147 | + /** @throws void */ |
| 148 | + $fiber->return = ($fiber->callback)(); |
| 149 | + $fiber->terminated = true; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + final class FiberError extends Error { |
| 154 | + |
| 155 | + } |
| 156 | +} |
0 commit comments