2024-03-01 14:34:14 +01:00
# Command Bus for ReactPHP
2024-03-01 14:59:12 +01:00
## Features
2024-03-01 18:19:25 +01:00
* Can run monolithic (create a bus and use as is), or distributed (create bus and use clients), or in a hybrid setup. The CommandBus functions identical with or without listeners and clients.
2024-03-01 14:34:14 +01:00
* All commands called asynchronously using promises and deferreds.
2024-03-01 15:20:54 +01:00
* Push notifications from the bus to subscribers and listeners, such as progress or log/error messages.
2024-03-01 14:59:12 +01:00
### Potential caveats
* There is no security, nor encryption. This is intended to work between a daemon and a frontend, and is designed primarily considering all activity taking place on localhost. You can use ReactPHPs `SecureConnector` and `SecureServer` to encrypt the traffic, but this still allows anyone to connect and interact with the bus. If this sounds like something that should be changed, open a ticket.
2024-03-01 14:34:14 +01:00
## Installing
```shell
$ composer require noccylabs/react-command-bus:^0.1.0
```
## Usage
```php
2024-03-01 14:59:12 +01:00
// The registry holds the callable commands
$commands = new CommandRegistry ();
// Register some function to call. The name here is "hello", and it will
// receive a Context holding the call context. Any passed data will be
// available as properties on the Context object.
$commands -> register ( "hello" , function ( Context $context ) {
// You don't have to, but you should return a promise from your
// commands.
2024-03-01 14:34:14 +01:00
return new Promise ( function ( callable $resolve ) use ( $context ) {
2024-03-01 14:59:12 +01:00
Loop :: addTimer ( 1 , function () use ( $context , $resolve ) {
$resolve ( "Hello, { $context -> name } " );
});
2024-03-01 14:34:14 +01:00
});
});
2024-03-01 14:59:12 +01:00
// Create the bus
$bus = new CommandBus ( $commands );
2024-03-01 14:34:14 +01:00
2024-03-01 14:59:12 +01:00
// And execute some commands!
$bus -> execute ( 'hello' , [ 'name' => 'Bob' ]) -> then (
function ( $result ) {
var_dump ( $result );
}
);
2024-03-01 14:34:14 +01:00
```
2024-03-19 00:01:12 +01:00
## License
Copyright (C) 2024, NoccyLabs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.