55namespace Parallite ;
66
77use Closure ;
8- use Parallite \Service \ConfigService ;
9- use Parallite \Service \DaemonService ;
10- use Parallite \Service \SocketService ;
11- use Parallite \Service \TaskService ;
8+ use Parallite \Service \Parallite \ ConfigService ;
9+ use Parallite \Service \Parallite \ DaemonService ;
10+ use Parallite \Service \Parallite \ SocketService ;
11+ use Parallite \Service \Parallite \ TaskService ;
1212use RuntimeException ;
1313use Socket ;
1414use Throwable ;
1515
1616/**
1717 * Parallite Client - Standalone PHP Client for Parallite Daemon
18- *
18+ *
1919 * This class provides a simple interface to communicate with Parallite daemon
2020 * and execute PHP closures in parallel.
21- *
21+ *
2222 * Usage:
2323 * ```php
2424 * use Parallite\ParalliteClient;
25- *
25+ *
2626 * // Option 1: Automatic daemon management (recommended)
2727 * $client = new ParalliteClient(autoManageDaemon: true);
28- *
28+ *
2929 * // Option 2: Manual daemon management (you start daemon yourself)
3030 * $client = new ParalliteClient('/tmp/parallite-custom.sock', autoManageDaemon: false);
31- *
31+ *
3232 * // Submit tasks
3333 * $future1 = $client->async(fn() => sleep(1) && 'Task 1');
3434 * $future2 = $client->async(fn() => sleep(2) && 'Task 2');
35- *
35+ *
3636 * // Await results
3737 * $result1 = $client->await($future1);
3838 * $result2 = $client->await($future2);
39- *
39+ *
4040 * // Daemon is automatically stopped on script end if autoManageDaemon=true
4141 * ```
42- *
42+ *
4343 * Required dependencies:
4444 * - PHP 8.2+
4545 * - opis/closure
4646 * - ext-sockets
47- *
47+ *
4848 * Configuration (parallite.json in project root):
4949 * - php_includes: Files loaded by worker processes
5050 * - go_overrides: Daemon configuration (timeout, workers, etc)
5151 */
5252class ParalliteClient
5353{
5454 private string $ socketPath ;
55- private bool $ autoManageDaemon = true ;
56- private bool $ enableBenchmark = false ;
57-
55+ private bool $ autoManageDaemon ;
56+ private bool $ enableBenchmark ;
57+
5858 private ConfigService $ configService ;
5959 private DaemonService $ daemonService ;
6060 private SocketService $ socketService ;
6161 private TaskService $ taskService ;
6262
6363 /**
6464 * Create a new Parallite client
65- *
66- * @param string $socketPath Path to socket (Unix: /tmp/file.sock, Windows: \\.\pipe\name)
67- * @param bool $autoManageDaemon If true, automatically starts/stops daemon
68- * @param string|null $projectRoot Project root directory (auto-detected if null)
69- * @param bool $enableBenchmark If true, includes benchmark data in responses
65+ *
66+ * @param string $socketPath Path to socket (Unix: /tmp/file.sock, Windows: \\.\pipe\name)
67+ * @param bool $autoManageDaemon If true, automatically starts/stops daemon
68+ * @param string|null $projectRoot Project root directory (auto-detected if null)
69+ * @param bool $enableBenchmark If true, includes benchmark data in responses
7070 */
7171 public function __construct (
72- string $ socketPath = '' ,
73- bool $ autoManageDaemon = true ,
72+ string $ socketPath = '' ,
73+ bool $ autoManageDaemon = true ,
7474 ?string $ projectRoot = null ,
75- bool $ enableBenchmark = false
75+ bool $ enableBenchmark = false
7676 )
7777 {
7878 $ this ->socketPath = $ socketPath !== '' ? $ socketPath : ConfigService::getDefaultSocketPath ();
@@ -92,7 +92,7 @@ public function __construct(
9292
9393 /**
9494 * Create a Promise for chainable async execution
95- *
95+ *
9696 * @template TReturn
9797 * @param Closure(): TReturn $closure The closure to execute
9898 * @return Promise<TReturn> Promise that supports then/catch/finally chaining
@@ -104,10 +104,10 @@ public function promise(Closure $closure): Promise
104104
105105 /**
106106 * Submit a task for parallel execution
107- *
107+ *
108108 * This method sends the task to Parallite daemon and returns a future
109109 * that can be awaited later. The socket is kept open to allow parallel execution.
110- *
110+ *
111111 * @param Closure $closure The closure to execute
112112 * @return array{socket: Socket, task_id: string} Future containing socket and task_id
113113 * @throws RuntimeException If connection or send fails
@@ -119,13 +119,13 @@ public function async(Closure $closure): array
119119
120120 /**
121121 * Await the result of a previously submitted task
122- *
122+ *
123123 * This method reads the response from the open socket and returns the result.
124124 * The socket is automatically closed after reading.
125125 * If the future parameter is passed by reference, benchmark data will be stored in it.
126- *
126+ *
127127 * @template TReturn
128- * @param array{socket: Socket|null, task_id: string, benchmark?: array<string, mixed>}|Promise<TReturn>|null $future The future returned by async() or a Promise
128+ * @param array{socket: Socket|null, task_id: string, benchmark?: array<string, mixed>}|Promise<TReturn>|null $future The future returned by async() or a Promise
129129 * @param-out array{socket: Socket|null, task_id: string, benchmark?: array<string, mixed>}|Promise<TReturn>|null $future
130130 * @return mixed The result of the task execution
131131 * @throws RuntimeException|Throwable If reading fails or task failed
@@ -142,16 +142,16 @@ public function await(array|Promise|null &$future = null): mixed
142142
143143 return $ this ->socketService ->awaitTask ($ future );
144144 }
145-
145+
146146 /**
147147 * Enable benchmark mode
148- *
148+ *
149149 * When enabled, task responses will include benchmark data with:
150150 * - execution_time_ms: Task execution time in milliseconds
151151 * - memory_delta_mb: Memory change during task execution (MB)
152152 * - memory_peak_mb: Peak memory usage during task (MB)
153153 * - cpu_time_ms: Total CPU time (user + system) in milliseconds
154- *
154+ *
155155 * @return self
156156 */
157157 public function enableBenchmark (): self
@@ -161,10 +161,10 @@ public function enableBenchmark(): self
161161 $ this ->taskService = new TaskService ($ this ->socketService );
162162 return $ this ;
163163 }
164-
164+
165165 /**
166166 * Disable benchmark mode
167- *
167+ *
168168 * @return self
169169 */
170170 public function disableBenchmark (): self
@@ -174,10 +174,10 @@ public function disableBenchmark(): self
174174 $ this ->taskService = new TaskService ($ this ->socketService );
175175 return $ this ;
176176 }
177-
177+
178178 /**
179179 * Check if benchmark mode is enabled
180- *
180+ *
181181 * @return bool
182182 */
183183 public function isBenchmarkEnabled (): bool
@@ -187,10 +187,10 @@ public function isBenchmarkEnabled(): bool
187187
188188 /**
189189 * Await multiple closures in parallel
190- *
190+ *
191191 * This is a convenience method that combines async() and await()
192192 * for multiple tasks, similar to Promise.all() in JavaScript.
193- *
193+ *
194194 * @param array<Closure> $closures Array of closures to execute
195195 * @return array<mixed> Array of results in the same order
196196 */
@@ -216,7 +216,7 @@ public function awaitMultiple(array $promises): array
216216
217217 /**
218218 * Get default socket path for the current platform
219- *
219+ *
220220 * @return string Socket path (Unix socket or Windows named pipe)
221221 */
222222 public static function getDefaultSocketPath (): string
0 commit comments