| 
									
										
										
										
											2024-02-27 02:22:49 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use NoccyLabs\React\Shell\CommandHandler; | 
					
						
							|  |  |  | use React\EventLoop\Loop; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once __DIR__."/../vendor/autoload.php"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $shell = new NoccyLabs\React\Shell\Shell(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $commands = new CommandHandler(); | 
					
						
							| 
									
										
										
										
											2024-02-28 18:40:26 +01:00
										 |  |  | // Make it possible to use as few characters as possible as long as they match
 | 
					
						
							|  |  |  | // a single command. For example with the commands foo, bar and baz, foo could
 | 
					
						
							|  |  |  | // be executed with 'f', 'fo', or 'foo', while both bar and baz would require
 | 
					
						
							|  |  |  | // the full name.
 | 
					
						
							|  |  |  | $commands->setAllowAbbreviatedCommands(true); | 
					
						
							|  |  |  | // Add commands
 | 
					
						
							| 
									
										
										
										
											2024-02-27 02:22:49 +01:00
										 |  |  | $commands->add('help', function ($args, $shell) { | 
					
						
							|  |  |  |     $shell->write("This could be usage help :)\n"); | 
					
						
							| 
									
										
										
										
											2024-02-28 18:40:26 +01:00
										 |  |  |     $shell->write("Exit by typing quit or pressing ^C\n"); | 
					
						
							| 
									
										
										
										
											2024-02-27 02:22:49 +01:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2024-02-28 18:40:26 +01:00
										 |  |  | // This is how you terminate the shell. Obviously, you close it :) Using end()
 | 
					
						
							|  |  |  | // on the shell differs somewhat between ReactPHP; close() will exit with
 | 
					
						
							|  |  |  | // code 0, while end will exit with code 1, and print a friendly message.
 | 
					
						
							|  |  |  | $commands->add('quit', function ($args, $shell) { | 
					
						
							|  |  |  |     $shell->close(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | // The command handler gets everything that was not matched. Resolve your own
 | 
					
						
							|  |  |  | // commands here, or print an error.
 | 
					
						
							| 
									
										
										
										
											2024-02-27 15:03:40 +01:00
										 |  |  | $commands->on('command', function ($command, $args, $shell) { | 
					
						
							|  |  |  |     $shell->write("Bad command '{$command}', try help.\n"); | 
					
						
							|  |  |  |     $shell->write("Arguments passed: ".json_encode($args)."\n"); | 
					
						
							| 
									
										
										
										
											2024-02-27 02:22:49 +01:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-28 18:40:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // The prompt event is invoked before every full redraw. Call redrawPrompt()
 | 
					
						
							|  |  |  | // to trigger it manually. Set the prompt or the style here.
 | 
					
						
							| 
									
										
										
										
											2024-02-28 12:44:09 +01:00
										 |  |  | $shell->on('prompt', function ($shell) { | 
					
						
							|  |  |  |     $shell->setPrompt(">> "); | 
					
						
							| 
									
										
										
										
											2024-02-27 02:22:49 +01:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2024-02-28 18:40:26 +01:00
										 |  |  | // This is where we hook the CommandHandler to the shell.
 | 
					
						
							| 
									
										
										
										
											2024-02-27 02:22:49 +01:00
										 |  |  | $shell->on('input', $commands); | 
					
						
							| 
									
										
										
										
											2024-02-28 18:40:26 +01:00
										 |  |  | // And finally, the end event gives you a chance to shut down cleanly. After
 | 
					
						
							|  |  |  | // returning from here, the shell will exit.
 | 
					
						
							|  |  |  | $shell->on('end', function ($shell) { | 
					
						
							|  |  |  |     $shell->write("Shutting down...\n"); | 
					
						
							|  |  |  | }); |