Matrix is a cutting-edge PHP library for asynchronous task management, inspired by JavaScript’s async
/await
paradigm but leveraging PHP's native Fibers. Matrix provides a streamlined, non-blocking API to run tasks, manage errors, and handle results—all without the need for explicit task starting.
Matrix also supports manual task management through the Task
class and robust error handling through a customizable ErrorHandler
.
Matrix brings a JavaScript-like async experience to PHP, providing developers with the simplicity and power of managing asynchronous tasks without complexity.
- JavaScript-like async API: Matrix introduces a simple and elegant API inspired by JavaScript’s async functions.
- Built on PHP Fibers: Using PHP 8.1+ Fibers, Matrix delivers true non-blocking concurrency.
- Customizable Error Handling: Handle task failures gracefully with an extensible error handler.
- Manage Task Lifecycles: Direct control over task status with methods for pausing, resuming, and canceling tasks.
- JavaScript-like async/await API for seamless asynchronous task execution.
- Non-blocking concurrency: Built on top of PHP 8.1+ Fibers.
- Error handling with custom error recovery using the
Handler
class. - Task lifecycle management: Start, pause, resume, cancel, or retry tasks.
Install Matrix via Composer:
composer require jerome/matrix
Matrix requires PHP 8.1 or above.
Matrix brings the familiarity of JavaScript's async
/await
into PHP, making it incredibly easy to work with asynchronous tasks. Here's how you can use it:
use Matrix\AsyncHelper;
use function Matrix\async;
// Execute an asynchronous task with success and error handling
async(fn () => 'Task result')
->then(function ($result) {
echo $result; // Output: Task result
})
->catch(function ($e) {
echo $e->getMessage(); // Handle any errors
});
This JavaScript-like API allows you to define tasks, handle success, and catch errors seamlessly—without needing to call a start
method explicitly.
Matrix also makes it easy to handle errors in asynchronous tasks:
async(fn () => throw new \RuntimeException('Error occurred'))
->catch(function ($e) {
echo "Caught error: " . $e->getMessage(); // Output: Caught error: Error occurred
});
The catch()
method allows you to define an error handler, making it straightforward to manage exceptions during task execution.
If you prefer more manual control over your tasks, Matrix provides the Task
class, allowing you to directly manage task lifecycles.
use Matrix\Task;
// Define a task that performs an operation
$task = new Task(function () {
for ($i = 0; $i < 3; $i++) {
echo "Working...\n";
\Fiber::suspend(); // Pause execution and yield control
}
return "Task completed";
});
// Start the task
$task->start();
// Resume the task until it completes
while (!$task->isCompleted()) {
$task->resume();
}
echo $task->getResult(); // Output: Task completed
Matrix allows you to pause and resume tasks at will:
$task->pause(); // Pause the task
$task->resume(); // Resume the task
Each task has a status (PENDING
, RUNNING
, PAUSED
, COMPLETED
, FAILED
, CANCELED
) that can be queried using the getStatus()
method.
Matrix provides robust error handling through the Handler
class. This class allows you to define retry logic, error logging, and final failure handling.
use Matrix\Task;
use Matrix\Exceptions\Handler;
// Define an error handler
$errorHandler = new Handler(3, [\RuntimeException::class]);
// Create a task that throws an exception
$task = new Task(function () {
throw new \RuntimeException('Something went wrong');
}, $errorHandler);
try {
$task->start();
} catch (\Throwable $e) {
$errorHandler->handle('task_1', $task, $e);
}
// Retry the task if it failed
if ($task->getStatus() === TaskStatus::FAILED) {
$task->retry();
}
The Handler
class can automatically retry tasks or log errors, making it highly customizable.
then(callable $callback)
: Registers a success callback and starts the task.catch(callable $callback)
: Registers an error callback and starts the task.start()
: Manually starts the task (called automatically when usingthen
orcatch
).
start()
: Starts the task execution.pause()
: Pauses the task if it’s running.resume()
: Resumes a paused task.cancel()
: Cancels the task.retry()
: Retries a task if it has failed or completed.getStatus()
: Returns the task status (PENDING
,RUNNING
,PAUSED
,COMPLETED
, etc.).getResult()
: Retrieves the result of the task once completed.
handle($contextId, $context, Throwable $e)
: Handles task errors.retryTask($taskId, Task $task)
: Retries a task.handleFinalFailure($taskId, Task $task)
: Handles a task’s final failure after all retries are exhausted.
We welcome contributions! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/new-feature
). - Make your changes and commit (
git commit -m 'Add new feature'
). - Push your branch (
git push origin feature/new-feature
). - Open a pull request!
Matrix is licensed under the MIT License. See the LICENSE file for more information.
- [Jerome Thayananthajothy] - Initial work - Thavarshan
See also the list of contributors who participated in this project.
- Special thanks to the PHP community for their support and inspiration for this project.
Matrix offers a unique PHP async experience, bringing true concurrency and fiber-based task management to PHP developers. Star the repository on GitHub to help Matrix grow and to stay updated on new features.