src/Async/functions.php source

1 <?php
2 namespace mini;
3
4 use mini\Async\AsyncInterface;
5
6 /**
7 * Async Feature - Global Helper Functions
8 *
9 * These functions provide the public API for the mini\Async feature.
10 */
11
12 // Register AsyncInterface service
13 Mini::$mini->addService(AsyncInterface::class, Lifetime::Singleton, fn() => Mini::$mini->loadServiceConfig(AsyncInterface::class));
14
15 /**
16 * I/O wait mode: wait until stream is readable
17 */
18 const READABLE = 1;
19
20 /**
21 * I/O wait mode: wait until stream is writable
22 */
23 const WRITABLE = 2;
24
25 /**
26 * I/O wait mode: wait until stream has exception/OOB data
27 */
28 const EXCEPTION = 4;
29
30 /**
31 * Get the configured async runtime
32 *
33 * Returns the registered AsyncInterface implementation. Async runtimes
34 * (phasync, Swoole, ReactPHP) provide implementations via config file:
35 *
36 * _config/mini/Async/AsyncInterface.php
37 *
38 * The config file should return an AsyncInterface instance.
39 *
40 * @return AsyncInterface
41 * @throws \LogicException If no async runtime is configured
42 */
43 function async(): AsyncInterface
44 {
45 return Mini::$mini->get(AsyncInterface::class);
46 }
47