11<?php
2+
23namespace Async \Http ;
34
45class MultiAsyncHandler
56{
67 private array $ streams = [];
8+
79 private array $ callbacks = [];
810
9- public function add (callable $ generatorCallback , callable $ onDone ): void {
11+ public function add (callable $ generatorCallback , callable $ onDone ): void
12+ {
1013 $ gen = $ generatorCallback ();
1114 $ gen ->rewind ();
1215 $ socket = $ this ->extractSocket ($ gen );
1316 if ($ socket ) {
14- $ this ->streams [(int )$ socket ] = $ socket ;
15- $ this ->callbacks [(int )$ socket ] = [$ gen , $ onDone ];
17+ $ this ->streams [(int ) $ socket ] = $ socket ;
18+ $ this ->callbacks [(int ) $ socket ] = [$ gen , $ onDone ];
1619 }
1720 }
1821
19- public function run (): void {
22+ public function run (): void
23+ {
2024 while ($ this ->streams ) {
2125 $ read = array_values ($ this ->streams );
22- $ write = null ; $ except = null ;
26+ $ write = null ;
27+ $ except = null ;
2328 stream_select ($ read , $ write , $ except , 5 );
2429
2530 foreach ($ read as $ socket ) {
26- $ id = (int )$ socket ;
31+ $ id = (int ) $ socket ;
2732 [$ gen , $ callback ] = $ this ->callbacks [$ id ];
2833 if ($ gen ->valid ()) {
2934 $ response = $ gen ->current ();
@@ -34,7 +39,8 @@ public function run(): void {
3439 }
3540 }
3641
37- private function extractSocket (\Generator $ gen ): mixed {
42+ private function extractSocket (\Generator $ gen ): mixed
43+ {
3844 $ r = new \ReflectionObject ($ gen );
3945 foreach ($ r ->getProperties () as $ prop ) {
4046 $ prop ->setAccessible (true );
@@ -43,6 +49,7 @@ private function extractSocket(\Generator $gen): mixed {
4349 return $ val ;
4450 }
4551 }
52+
4653 return null ;
4754 }
4855}
0 commit comments