Updated readme, examples
This commit is contained in:
		
							
								
								
									
										61
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								README.md
									
									
									
									
									
								
							@@ -1,7 +1,14 @@
 | 
				
			|||||||
# Command Bus for ReactPHP
 | 
					# Command Bus for ReactPHP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* Can run monolithic (create a bus and use as is), or distributed (create bus and use clients), or a hybrid.
 | 
					## Features
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					* 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 clients.
 | 
				
			||||||
* All commands called asynchronously using promises and deferreds.
 | 
					* All commands called asynchronously using promises and deferreds.
 | 
				
			||||||
 | 
					* Push notifications from the bus to subscribers and listeners, such as progress or log/error messages. (WIP)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### 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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Installing
 | 
					## Installing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -12,41 +19,29 @@ $ composer require noccylabs/react-command-bus:^0.1.0
 | 
				
			|||||||
## Usage
 | 
					## Usage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
// This is enough to setup a local bus.
 | 
					// The registry holds the callable commands
 | 
				
			||||||
$bus = new CommandBus();
 | 
					$commands = new CommandRegistry();
 | 
				
			||||||
$bus->register('hello', function (Context $context) {
 | 
					
 | 
				
			||||||
 | 
					// 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.
 | 
				
			||||||
    return new Promise(function (callable $resolve) use ($context) {
 | 
					    return new Promise(function (callable $resolve) use ($context) {
 | 
				
			||||||
        return $resolve([ 'message' => "Hello, {$context->name}" ]);
 | 
					        Loop::addTimer(1, function () use ($context, $resolve) {
 | 
				
			||||||
 | 
					            $resolve("Hello, {$context->name}");
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// You can call it as expected
 | 
					// Create the bus
 | 
				
			||||||
$bus->execute('hello', [ 'name' => "Bob" ])
 | 
					$bus = new CommandBus($commands);
 | 
				
			||||||
    ->then(function (array $result) {
 | 
					 | 
				
			||||||
        echo "Result: {$result['message']}\n";
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Add a listener, and you can now connect to it!
 | 
					 | 
				
			||||||
$bus->addServer($server);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// So using this in another script works as expected, if you consider
 | 
					 | 
				
			||||||
// the async flow. See the examples for working examples.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$client = new CommandBusClient();
 | 
					 | 
				
			||||||
$client->connect($socket);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
$client->execute('hello', [ 'name' => "Bob" ])
 | 
					 | 
				
			||||||
    ->then(function (array $result) {
 | 
					 | 
				
			||||||
        echo "Result: {$result['message']}\n";
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// The bus can also notify all clients about important events
 | 
					 | 
				
			||||||
$bus->notify('updateCompleted', [ 'info' => [] ]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Listening on the bus or client will yield the event
 | 
					 | 
				
			||||||
$bus->on('notify', function (string $event, array $data) {});
 | 
					 | 
				
			||||||
$client->on('notify', function (string $event, array $data) {});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// And execute some commands!
 | 
				
			||||||
 | 
					$bus->execute('hello', ['name'=>'Bob'])->then(
 | 
				
			||||||
 | 
					    function ($result) {
 | 
				
			||||||
 | 
					        var_dump($result);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,12 @@ use React\EventLoop\Loop;
 | 
				
			|||||||
use React\Promise\Promise;
 | 
					use React\Promise\Promise;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$commands = new CommandRegistry();
 | 
					$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) {
 | 
					$commands->register("hello", function (Context $context) {
 | 
				
			||||||
 | 
					    // You don't have to, but you should return a promise from your 
 | 
				
			||||||
 | 
					    // commands.
 | 
				
			||||||
    return new Promise(function (callable $resolve) use ($context) {
 | 
					    return new Promise(function (callable $resolve) use ($context) {
 | 
				
			||||||
        Loop::addTimer(1, function () use ($context, $resolve) {
 | 
					        Loop::addTimer(1, function () use ($context, $resolve) {
 | 
				
			||||||
            $resolve("Hello, {$context->name}");
 | 
					            $resolve("Hello, {$context->name}");
 | 
				
			||||||
@@ -17,17 +22,22 @@ $commands->register("hello", function (Context $context) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
$commands->register("hello2", function (Context $context) {
 | 
					$commands->register("hello2", function (Context $context) {
 | 
				
			||||||
 | 
					    // Just returning the value works just fine!
 | 
				
			||||||
    return "Hello2, {$context->name}";
 | 
					    return "Hello2, {$context->name}";
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Create the bus
 | 
				
			||||||
$bus = new CommandBus($commands);
 | 
					$bus = new CommandBus($commands);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$bus->execute('hello', ['name'=>'Bob'])
 | 
					// And execute some commands!
 | 
				
			||||||
->then(function ($result) {
 | 
					$bus->execute('hello', ['name'=>'Bob'])->then(
 | 
				
			||||||
 | 
					    function ($result) {
 | 
				
			||||||
        var_dump($result);
 | 
					        var_dump($result);
 | 
				
			||||||
});
 | 
					    }
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$bus->execute('hello2', ['name'=>'Bob'])
 | 
					$bus->execute('hello2', ['name'=>'Bob'])->then(
 | 
				
			||||||
->then(function ($result) {
 | 
					    function ($result) {
 | 
				
			||||||
        var_dump($result);
 | 
					        var_dump($result);
 | 
				
			||||||
});
 | 
					    }
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user