ReactPHP Shell, based on the Symfony Console component.

Overview

Pecan

Event-driven, non-blocking shell for ReactPHP.

Pecan (/pɪˈkɑːn/) provides a non-blocking alternative to the shell provided in the Symfony Console component. Additionally, Pecan includes a basic, framework-agnostic shell component called Drupe that can be used as the basis for building custom shells.

Shells

Drupe

Pecan\Drupe is a standalone component for building event-driven console components. I like to think of it as Pecan without the Shell.

Pass an EventLoopInterface to start() listening for input.

$loop  = \React\EventLoop\Factory::create();
$shell = new \Pecan\Drupe();

// $shell->start() returns $loop to allow this chaining.
$shell->start($loop)->run();

Drupe Events

  • running - The shell is running.
  • data - Data was received from STDIN.
  • error - Indicates an I/O problem.
  • close - The shell was closed.

Shell

Pecan\Shell extends Drupe to provide an event-driven wrapper for a standard Symfony\Component\Console\Application. It can be used as a drop-in replacement for Symfony\Component\Console\Shell.

Note: To maintain a Symfony Console-like workflow, calling $shell->run() on Pecan\Shell starts the EventLoop, so make sure it gets called last.

Shell Events

Shell emits the same events as Drupe.

Readline

Pecan\Readline provides an interface for reading line-by-line input from STDIN. This component is heavily inspired by, and strives for parity with the NodeJS Readline Component.

Readline Events

  • line - A line has been read from STDIN.
  • pause - Reading from the stream has been paused.
  • resume - Reading from the stream has resumed.
  • error - The input stream encountered an error.
  • close - Then input stream was closed.

Console

Pecan\Console\Console provides a standard interface for working with STDOUT and STDERR. It is inspired heavily by the NodeJS Console Component and takes some functionality from the Symfony Console Component

Output

The Output classes extend the base Console Output. StreamOutput wraps a single stream resource, while ConsoleOutput contains both the STDOUT and STDERR streams.

  • StreamOutputInterface
  • ConsoleOutputInterface
  • PecanOutput

Using Pecan

Using Drupe as a Standalone Shell

use Pecan\Drupe;

$loop   = \React\EventLoop\Factory::create();
$shell  = new Drupe();

// Example one-time callback to write the initial prompt.
// This resumes reading from STDIN and kicks off the shell.
$shell->once('running', function (Drupe $shell) {
    $shell->setPrompt('drupe> ')->prompt();
});

// Example callback for the data event.
// By convention, any call to write() will be followed by a call to prompt() 
// once the data has been written to the output stream.
$shell->on('data', function ($line, Drupe $shell) {

    $command = (!$line && strlen($line) == 0) ? false : rtrim($line);

    if ('exit' === $command || false === $command) {
        $shell->close();
    } else {
        $shell->writeln(sprintf(PHP_EOL.'// in: %s', $line));
    }

});

// Example callback for the close event.
$shell->on('close', function ($code, Drupe $shell) {
    $shell->writeln([
        '// Goodbye.',
        sprintf('// Shell exits with code %d', $code),
    ]);
});

$shell->start($loop)->run();

Using Shell with Symfony Console Applications

Here is a shell that echoes back any input it receives, and then exits.

// Pecan\Shell wraps a standard Console Application.
use Symfony\Component\Console\Application;
use Pecan\Shell;

$shell = new Shell(new Application('pecan'));

$shell->on('data', function($line, Shell $shell) {
    $shell->write($line)->then(function($shell) {
        $shell->close();
    });
});

$shell->run();

Injecting An EventLoopInterface into Pecan\Shell

Unless you pass \Pecan\Shell an object implementing EventLoopInterface as its second constructor method, the Shell will get one from the EventLoop Factory. Keep this in mind if you want to integrate Pecan into an existing ReactPHP project.

use Symfony\Component\Console\Application;
use Pecan\Shell;

$loop  = \React\EventLoop\Factory::create();

// Do other things requiring $loop...

$shell = new Shell(new Application('pecan'), $loop);

// We must still let the shell run the EventLoop.
$shell->run();

Example exit callback

// Example callback for the exit event.
$shell->on('exit', function($code, \Pecan\Shell $shell) {
    $shell->emit('output', [
        [
            'Goodbye.',
            sprintf('// Shell exits with code %d', $code)
        ],
        true
    ]);
});
You might also like...
Web Shell Detector – is a php script that helps you find and identify php/cgi(perl)/asp/aspx shells.
Web Shell Detector – is a php script that helps you find and identify php/cgi(perl)/asp/aspx shells.

Web Shell Detector – is a php script that helps you find and identify php/cgi(perl)/asp/aspx shells. Web Shell Detector has a “web shells” signature database that helps to identify “web shell” up to 99%. By using the latest javascript and css technologies, web shell detector has a light weight and friendly interface.

The Hoa\Console library.
The Hoa\Console library.

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds. Hoa\Con

Display your Laravel routes in the console, but make it pretty. 😎
Display your Laravel routes in the console, but make it pretty. 😎

Pretty Routes for Laravel Display your Laravel routes in the console, but make it pretty. 😎 Installation You can install the package via composer: co

Customized loading ⌛ spinner for Laravel Artisan Console.

Laravel Console Spinner Laravel Console Spinner was created by Rahul Dey. It is just a custom Progress Bar inspired by icanhazstring/symfony-console-s

It's like Tailwind CSS, but for the console.

Tailcli allows building unique, beautiful command-line applications, using tailwind classes. It's like Tailwind CSS, but for the console. Installation

Laravel Console Toolkit
Laravel Console Toolkit

This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.

Simple and customizable console log output for CLI apps.
Simple and customizable console log output for CLI apps.

Console Pretty Print Simple and customizable console log output for CLI apps. Highlights Simple installation (Instalação simples) Very easy to customi

A powerful command line application framework for PHP. It's an extensible, flexible component, You can build your command-based application in seconds!
A powerful command line application framework for PHP. It's an extensible, flexible component, You can build your command-based application in seconds!

CLIFramework CLIFramework is a command-line application framework, for building flexiable, simple command-line applications. Commands and Subcommands

Render the symfony profiler toolbar in your terminal.
Render the symfony profiler toolbar in your terminal.

sourceability/console-toolbar-bundle Render the symfony profiler toolbar in your terminal. Each panel links to the corresponding web profiler page.

Comments
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @mcrumm requested this pull request.

    This patch was automatically generated as part of the following inspection: https://scrutinizer-ci.com/g/mcrumm/pecan/inspections/7f4bd882-29da-4501-8e57-6bb9345d32fd

    Enabled analysis tools:

    • PHP Analyzer
    • PHP PDepend
    • PHP Similarity Analyzer
    • PHP Change Tracking Analyzer
    opened by scrutinizer-auto-fixer 0
  • waffle.io Badge

    waffle.io Badge

    Merge this to receive a badge indicating the number of issues in the ready column on your waffle.io board at https://waffle.io/mcrumm/pecan

    This was requested by a real person (user mcrumm) on waffle.io, we're not trying to spam you.

    opened by waffle-iron 0
Owner
Michael Crumm
Michael Crumm
Console - The Console component eases the creation of beautiful and testable command line interfaces.

Console Component The Console component eases the creation of beautiful and testable command line interfaces. Sponsor The Console component for Symfon

Symfony 9.4k Jan 7, 2023
Console component from Zend Framework

zend-console Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-console. Zend\Console is a component to design and implement

Zend Framework 47 Mar 16, 2021
Supercharge your Symfony console commands!

zenstruck/console-extra A modular set of features to reduce configuration boilerplate for your commands: /** * Creates a user in the database. * *

Kevin Bond 29 Nov 19, 2022
🤖 GitHub Action to run symfony console commands.

Symfony Console GitHub Action Usage You can use it as a Github Action like this: # .github/workflows/lint.yml name: "Lint" on: pull_request: push

Nucleos 3 Oct 20, 2022
A developer-friendly wrapper around execution of shell commands.

ptlis/shell-command A developer-friendly wrapper around execution of shell commands. There were several goals that inspired the creation of this packa

brian ridley 18 Dec 31, 2022
YAPS - Yet Another PHP Shell

YAPS - Yet Another PHP Shell Yeah, I know, I know... But that's it. =) As the name reveals, this is yet another PHP reverse shell, one more among hund

Nicholas Ferreira 60 Dec 14, 2022
An Interactive Shell to Lumen Framework.

ABANDONED Please consider to use the official Laravel Tinker, it is also compatible with Lumen: laravel/tinker Lumen Artisan Tinker An Interactive She

Vagner Luz do Carmo 112 Aug 17, 2022
I gues i tried to make a shell that's looks like a terminal in single php file

php-shell-gui Terms of service This tool can only be used for legal purposes. You take full responsibility for any actions performed using this. The o

Squar3 4 Aug 23, 2022
A simple object oriented interface to execute shell commands in PHP

php-shellcommand php-shellcommand provides a simple object oriented interface to execute shell commands. Installing Prerequisites Your php version mus

Michael Härtl 283 Dec 10, 2022