Download the PHP package nashgao/interactive-shell without Composer
On this page you can find all versions of the php package nashgao/interactive-shell. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nashgao/interactive-shell
More information about nashgao/interactive-shell
Files in nashgao/interactive-shell
Package interactive-shell
Short Description Interactive shell library with pluggable transports and streaming support
License MIT
Informations about the package interactive-shell
Interactive Shell
Interactive shell library with pluggable transports and streaming support. Build MySQL-like CLI interfaces for your PHP applications with built-in command parsing, history, formatting, and real-time message streaming capabilities.
Documentation
- Visual Architecture Guide - ASCII diagrams, command flows, and troubleshooting
Features
- Standard Shell (REPL): Traditional request/response interactive shell with readline support
- Streaming Shell: Bidirectional streaming for real-time message processing (Swoole coroutines)
- Pluggable Transports: Unix Socket (Swoole), or implement your own via
TransportInterface - Multiple Output Formats: Table (ASCII), JSON, CSV, Vertical (MySQL
\Gstyle) - Advanced Command Parsing: Quote handling, escape sequences, options (
--format=json), and\Gterminator - Shell Features: Command history with persistence, configurable aliases, multi-line input
- Client-Side Filtering: Real-time message filtering in streaming mode
- Built-in Commands: Help, status, history, aliases, screen clearing
Requirements
- PHP 8.1 or higher
- ext-swoole (coroutine-based async I/O)
- Symfony Console ^6.0|^7.0
Optional Extensions
ext-readline: Recommended for better input handling and history navigation
Installation
Quick Start
Standard Shell (Request/Response)
Example Session:
Streaming Shell (Real-time Messages)
Example Streaming Session:
Output Formats
The shell supports four output formats that can be selected via --format option or \G terminator:
Table Format (Default)
Output:
JSON Format
Output:
CSV Format
Output:
Vertical Format (MySQL \G style)
Output:
Built-in Commands
These commands work offline without server connection:
| Command | Description | Example |
|---|---|---|
help |
Display help message with available commands | help |
exit, quit |
Exit the shell and save session | exit |
status |
Show connection status and session metrics | status |
clear |
Clear the terminal screen | clear |
history |
Display command history | history |
alias [name=cmd] |
Show all aliases or set a new alias | alias ls=list |
unalias <name> |
Remove an alias | unalias ls |
Status Output Example:
Streaming Commands
Additional commands available in StreamingShell:
| Command | Description | Example |
|---|---|---|
filter <pattern> |
Set message filter expression | filter sensor/temperature |
filter show |
Show current filter | filter show |
filter clear |
Clear filter (show all messages) | filter clear |
pause |
Pause message streaming | pause |
resume |
Resume message streaming | resume |
stats |
Show streaming statistics | stats |
Filter Patterns:
Configuration
Shell Configuration Options
Transport Configuration
Swoole Socket Transport
Custom Transport Implementation
Implement the TransportInterface to create custom transport backends:
Usage:
Server-Side Command Handlers
Create custom command handlers for your application by implementing CommandHandlerInterface:
Creating a Handler
Registering Handlers
Use CommandRegistry to register your handlers:
Hyperf Framework Integration
Automatic Setup
The library provides a ConfigProvider for Hyperf auto-discovery:
Configuration
Edit config/autoload/interactive_shell.php:
Connecting to a Running Server
Once the Hyperf server is running, connect using a client:
Built-in Hyperf Handlers
When running in Hyperf, these additional commands are available:
| Command | Description |
|---|---|
config <key> |
Get configuration values using dot notation |
routes |
List all registered HTTP routes |
container <abstract> |
Inspect container bindings |
command <name> |
Execute Hyperf console commands |
API Reference
Core Classes
| Class | Description |
|---|---|
Shell |
Standard interactive shell with request/response model |
StreamingShell |
Bidirectional streaming shell for real-time messages |
ShellParser |
Command parser with quote handling and options |
OutputFormatter |
Formats command results in table/JSON/CSV/vertical |
MessageFormatter |
Formats streaming messages with timestamps |
HistoryManager |
Manages command history with persistence |
AliasManager |
Manages command aliases |
ShellState |
Tracks session state and metrics |
Transport Classes
| Class | Description |
|---|---|
SwooleSocketTransport |
Swoole coroutine-based Unix socket transport (with streaming support) |
TransportInterface |
Interface for custom transport implementations |
StreamingTransportInterface |
Extended interface for streaming transports |
Data Classes
| Class | Description |
|---|---|
ParsedCommand |
Parsed command with arguments and options |
CommandResult |
Command execution result with data and metadata |
Message |
Streaming message with topic and payload |
FilterExpression |
Message filter for streaming mode |
OutputFormat |
Enum for output format types |
Shell Methods
StreamingShell Methods
Advanced Features
Multi-line Input
Use \ at the end of a line to continue input on the next line:
History Navigation
- Up/Down arrows: Navigate through command history
- Ctrl+R: Reverse search through history (with readline)
- History persistence: Commands saved between sessions
Alias Expansion
Session Persistence
The shell automatically saves:
- Command history
- Session metrics (start time, command count, etc.)
- Connection state
Performance Considerations
Streaming Mode Performance
The streaming shell uses Swoole coroutines for true concurrent I/O — message receiving, display, and user input each run in their own coroutine.
Output Format Performance
| Format | Speed | Use Case |
|---|---|---|
| JSON | Fastest | Machine-readable, API integration |
| CSV | Fast | Data export, spreadsheet import |
| Table | Moderate | Human-readable terminal output |
| Vertical | Moderate | Detailed single-record inspection |
Testing
Test Structure
| Suite | Directory | Purpose |
|---|---|---|
| Unit | tests/Unit/ |
Component isolation tests |
| Integration | tests/Integration/ |
Cross-component interactions |
| Specification | tests/Specification/ |
Consumer-perspective behavior specs |
| E2E | tests/E2E/ |
Full system scenarios (requires Swoole) |
Running Tests
Key Test Areas
- Parser (
tests/Unit/Parser/): Command parsing, quote handling, escape sequences - Handlers (
tests/Unit/Server/Handler/): Built-in command handlers - Transport (
tests/Integration/Transport/): Socket transport communication - Formatters (
tests/Unit/Formatter/): Table, JSON, CSV, Vertical output - Shell (
tests/Integration/): Full shell lifecycle and command flow
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Write tests for new functionality
- Ensure all tests pass (
composer test) - Ensure PHPStan passes at level max (
composer phpstan) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Code Standards
- Follow PSR-12 coding style
- Use strict types (
declare(strict_types=1)) - Add type hints for all parameters and return types
- Write PHPDoc blocks for public methods
- Keep methods focused and concise
License
This library is licensed under the MIT License. See the LICENSE file for details.
Built with ❤️ by Nash Gao