Skip to content

Commit 3061e8e

Browse files
authored
Merge pull request #3 from rcalicdan/http-improvement
Custom HTTP client without external dependencies
2 parents f5fe7df + 43695f8 commit 3061e8e

33 files changed

+1243
-1313
lines changed

README.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -484,20 +484,6 @@ $promise = wrap_sync(function() {
484484
$result = Async::run($promise);
485485
```
486486

487-
## Quick Utilities
488-
489-
### Quick HTTP Fetch
490-
491-
For simple HTTP requests with automatic loop management:
492-
493-
```php
494-
// Returns response data directly
495-
$data = Async::quickFetch('https://api.example.com/data');
496-
497-
// Using helper function
498-
$data = quick_fetch('https://api.example.com/data');
499-
```
500-
501487
### Simple Tasks
502488

503489
For quick async tasks:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"files": [
1111
"src/Helpers/async_helper.php",
1212
"src/Helpers/loop_helper.php",
13-
"src/Helpers/file_helper.php"
13+
"src/Helpers/file_helper.php",
14+
"src/Helpers/http_helper.php"
1415
]
1516
},
1617
"authors": [

src/AsyncEventLoop.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ private function __construct()
9797
$this->tickHandler = new TickHandler;
9898
$this->activityHandler = new ActivityHandler;
9999
$this->stateHandler = new StateHandler;
100-
$this->fileManager = new FileManager();
100+
$this->fileManager = new FileManager;
101101

102102
// Initialize handlers that depend on managers
103103
$this->workHandler = new WorkHandler(
104-
$this->timerManager,
105-
$this->httpRequestManager,
106-
$this->streamManager,
107-
$this->fiberManager,
108-
$this->tickHandler,
109-
$this->fileManager,
104+
timerManager: $this->timerManager,
105+
httpRequestManager: $this->httpRequestManager,
106+
streamManager: $this->streamManager,
107+
fiberManager: $this->fiberManager,
108+
tickHandler: $this->tickHandler,
109+
fileManager: $this->fileManager,
110110
);
111111

112112
$this->sleepHandler = new SleepHandler(

src/AsyncFileOperations.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Rcalicdan\FiberAsync;
4+
5+
use Rcalicdan\FiberAsync\Contracts\PromiseInterface;
6+
use Rcalicdan\FiberAsync\Handlers\File\FileHandler;
7+
8+
class AsyncFileOperations
9+
{
10+
/**
11+
* @var FileHandler Handles asynchronous file operations
12+
*/
13+
private FileHandler $fileHandler;
14+
15+
public function __construct()
16+
{
17+
$this->fileHandler = new FileHandler;
18+
}
19+
20+
/**
21+
* Read a file asynchronously.
22+
*
23+
* @param string $path The file path to read
24+
* @return PromiseInterface Promise that resolves with file contents
25+
*/
26+
public function readFile(string $path, array $options = []): PromiseInterface
27+
{
28+
return $this->fileHandler->readFile($path, $options);
29+
}
30+
31+
/**
32+
* Write to a file asynchronously.
33+
*
34+
* @param string $path The file path to write to
35+
* @param string $data The data to write
36+
* @param bool $append Whether to append or overwrite
37+
* @return PromiseInterface Promise that resolves with bytes written
38+
*/
39+
public function writeFile(string $path, string $data, array $options = []): PromiseInterface
40+
{
41+
return $this->fileHandler->writeFile($path, $data, $options);
42+
}
43+
44+
/**
45+
* Append to a file asynchronously.
46+
*
47+
* @param string $path The file path to append to
48+
* @param string $data The data to append
49+
* @return PromiseInterface Promise that resolves with bytes written
50+
*/
51+
public function appendFile(string $path, string $data): PromiseInterface
52+
{
53+
return $this->fileHandler->appendFile($path, $data);
54+
}
55+
56+
/**
57+
* Check if file exists asynchronously.
58+
*/
59+
public function fileExists(string $path): PromiseInterface
60+
{
61+
return $this->fileHandler->fileExists($path);
62+
}
63+
64+
/**
65+
* Get file information asynchronously.
66+
*/
67+
public function getFileStats(string $path): PromiseInterface
68+
{
69+
return $this->fileHandler->getFileStats($path);
70+
}
71+
72+
/**
73+
* Delete a file asynchronously.
74+
*/
75+
public function deleteFile(string $path): PromiseInterface
76+
{
77+
return $this->fileHandler->deleteFile($path);
78+
}
79+
80+
/**
81+
* Create a directory asynchronously.
82+
*/
83+
public function createDirectory(string $path, array $options = []): PromiseInterface
84+
{
85+
return $this->fileHandler->createDirectory($path, $options);
86+
}
87+
88+
/**
89+
* Remove a directory asynchronously.
90+
*/
91+
public function removeDirectory(string $path): PromiseInterface
92+
{
93+
return $this->fileHandler->removeDirectory($path);
94+
}
95+
96+
/**
97+
* Copy a file asynchronously.
98+
*/
99+
public function copyFile(string $source, string $destination): PromiseInterface
100+
{
101+
return $this->fileHandler->copyFile($source, $destination);
102+
}
103+
104+
/**
105+
* Rename a file asynchronously.
106+
*/
107+
public function renameFile(string $oldPath, string $newPath): PromiseInterface
108+
{
109+
return $this->fileHandler->renameFile($oldPath, $newPath);
110+
}
111+
112+
/**
113+
* Watch a file for changes asynchronously.
114+
*/
115+
public function watchFile(string $path, callable $callback, array $options = []): string
116+
{
117+
return $this->fileHandler->watchFile($path, $callback, $options);
118+
}
119+
120+
/**
121+
* Unwatch a file for changes asynchronously.
122+
*/
123+
public function unwatchFile(string $watcherId): bool
124+
{
125+
return $this->fileHandler->unwatchFile($watcherId);
126+
}
127+
}

src/AsyncOperations.php

Lines changed: 0 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\AwaitHandler;
99
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\ConcurrencyHandler;
1010
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\FiberContextHandler;
11-
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\FileHandler;
12-
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\HttpHandler;
1311
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\PromiseCollectionHandler;
1412
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\PromiseHandler;
1513
use Rcalicdan\FiberAsync\Handlers\AsyncOperations\TimerHandler;
@@ -51,11 +49,6 @@ class AsyncOperations implements AsyncOperationsInterface
5149
*/
5250
private TimerHandler $timerHandler;
5351

54-
/**
55-
* @var HttpHandler Handles HTTP request operations
56-
*/
57-
private HttpHandler $httpHandler;
58-
5952
/**
6053
* @var PromiseCollectionHandler Manages collections of promises (all, race)
6154
*/
@@ -66,11 +59,6 @@ class AsyncOperations implements AsyncOperationsInterface
6659
*/
6760
private ConcurrencyHandler $concurrencyHandler;
6861

69-
/**
70-
* @var FileHandler Handles asynchronous file operations
71-
*/
72-
private FileHandler $fileHandler;
73-
7462
/**
7563
* Initialize the async operations system with all required handlers.
7664
*
@@ -84,10 +72,8 @@ public function __construct()
8472
$this->executionHandler = new AsyncExecutionHandler;
8573
$this->awaitHandler = new AwaitHandler($this->contextHandler);
8674
$this->timerHandler = new TimerHandler;
87-
$this->httpHandler = new HttpHandler;
8875
$this->collectionHandler = new PromiseCollectionHandler;
8976
$this->concurrencyHandler = new ConcurrencyHandler($this->executionHandler);
90-
$this->fileHandler = new FileHandler();
9177
}
9278

9379
/**
@@ -194,55 +180,6 @@ public function delay(float $seconds): PromiseInterface
194180
return $this->timerHandler->delay($seconds);
195181
}
196182

197-
/**
198-
* Perform an HTTP request and return a promise.
199-
*
200-
* @param string $url The URL to request
201-
* @param array $options Request options (method, headers, body, etc.)
202-
* @return PromiseInterface A promise that resolves with the response
203-
*/
204-
public function fetch(string $url, array $options = []): PromiseInterface
205-
{
206-
return $this->httpHandler->fetch($url, $options);
207-
}
208-
209-
/**
210-
* Perform an HTTP request using Guzzle HTTP client.
211-
*
212-
* @param string $method HTTP method (GET, POST, etc.)
213-
* @param string $url The URL to request
214-
* @param array $options Guzzle-specific request options
215-
* @return PromiseInterface A promise that resolves with the response
216-
*/
217-
public function guzzle(string $method, string $url, array $options = []): PromiseInterface
218-
{
219-
return $this->httpHandler->guzzle($method, $url, $options);
220-
}
221-
222-
/**
223-
* Get the HTTP handler for advanced HTTP operations.
224-
*
225-
* @return mixed The HTTP handler instance for direct access
226-
*/
227-
public function http()
228-
{
229-
return $this->httpHandler->http();
230-
}
231-
232-
/**
233-
* Wrap a synchronous operation in a promise.
234-
*
235-
* This is useful for integrating blocking operations into
236-
* async workflows without blocking the event loop.
237-
*
238-
* @param callable $syncCall The synchronous operation to wrap
239-
* @return PromiseInterface A promise that resolves with the operation result
240-
*/
241-
public function wrapSync(callable $syncCall): PromiseInterface
242-
{
243-
return $this->httpHandler->wrapSync($syncCall);
244-
}
245-
246183
/**
247184
* Wait for all promises to resolve and return their results.
248185
*
@@ -285,113 +222,4 @@ public function concurrent(array $tasks, int $concurrency = 10): PromiseInterfac
285222
{
286223
return $this->concurrencyHandler->concurrent($tasks, $concurrency);
287224
}
288-
289-
/**
290-
* Read a file asynchronously.
291-
*
292-
* @param string $path The file path to read
293-
* @param array $options
294-
* @return PromiseInterface Promise that resolves with file contents
295-
*/
296-
public function readFile(string $path, array $options = []): PromiseInterface
297-
{
298-
return $this->fileHandler->readFile($path, $options);
299-
}
300-
301-
/**
302-
* Write to a file asynchronously.
303-
*
304-
* @param string $path The file path to write to
305-
* @param string $data The data to write
306-
* @param bool $append Whether to append or overwrite
307-
* @return PromiseInterface Promise that resolves with bytes written
308-
*/
309-
public function writeFile(string $path, string $data, array $options = []): PromiseInterface
310-
{
311-
return $this->fileHandler->writeFile($path, $data, $options);
312-
}
313-
314-
/**
315-
* Append to a file asynchronously.
316-
*
317-
* @param string $path The file path to append to
318-
* @param string $data The data to append
319-
* @return PromiseInterface Promise that resolves with bytes written
320-
*/
321-
public function appendFile(string $path, string $data): PromiseInterface
322-
{
323-
return $this->fileHandler->appendFile($path, $data);
324-
}
325-
326-
/**
327-
* Check if file exists asynchronously.
328-
*/
329-
public function fileExists(string $path): PromiseInterface
330-
{
331-
return $this->fileHandler->fileExists($path);
332-
}
333-
334-
/**
335-
* Get file information asynchronously.
336-
*/
337-
public function getFileStats(string $path): PromiseInterface
338-
{
339-
return $this->fileHandler->getFileStats($path);
340-
}
341-
342-
/**
343-
* Delete a file asynchronously.
344-
*/
345-
public function deleteFile(string $path): PromiseInterface
346-
{
347-
return $this->fileHandler->deleteFile($path);
348-
}
349-
350-
/**
351-
* Create a directory asynchronously.
352-
*/
353-
public function createDirectory(string $path, array $options = []): PromiseInterface
354-
{
355-
return $this->fileHandler->createDirectory($path, $options);
356-
}
357-
358-
/**
359-
* Remove a directory asynchronously.
360-
*/
361-
public function removeDirectory(string $path): PromiseInterface
362-
{
363-
return $this->fileHandler->removeDirectory($path);
364-
}
365-
366-
/**
367-
* Copy a file asynchronously.
368-
*/
369-
public function copyFile(string $source, string $destination): PromiseInterface
370-
{
371-
return $this->fileHandler->copyFile($source, $destination);
372-
}
373-
374-
/**
375-
* Rename a file asynchronously.
376-
*/
377-
public function renameFile(string $oldPath, string $newPath): PromiseInterface
378-
{
379-
return $this->fileHandler->renameFile($oldPath, $newPath);
380-
}
381-
382-
/**
383-
* Watch a file for changes asynchronously.
384-
*/
385-
public function watchFile(string $path, callable $callback, array $options = []): string
386-
{
387-
return $this->fileHandler->watchFile($path, $callback, $options);
388-
}
389-
390-
/**
391-
* Unwatch a file for changes asynchronously.
392-
*/
393-
public function unwatchFile(string $watcherId): bool
394-
{
395-
return $this->fileHandler->unwatchFile($watcherId);
396-
}
397225
}

0 commit comments

Comments
 (0)