All Projects → mcrumm → pecan

mcrumm / pecan

Licence: MIT license
ReactPHP Shell, based on the Symfony Console component.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to pecan

Reactphp
Event-driven, non-blocking I/O with PHP.
Stars: ✭ 8,211 (+18995.35%)
Mutual labels:  reactphp
Php Console Spinner
Colorful highly configurable spinner for php cli applications (suitable for async apps)
Stars: ✭ 225 (+423.26%)
Mutual labels:  reactphp
php-backtrace
Show nice equivalent to debug_backtrace(), with caller, code preview etc.
Stars: ✭ 18 (-58.14%)
Mutual labels:  symfony-console
Filesystem
Evented filesystem access.
Stars: ✭ 102 (+137.21%)
Mutual labels:  reactphp
Promise
Promises/A implementation for PHP.
Stars: ✭ 2,078 (+4732.56%)
Mutual labels:  reactphp
Http Client
[Deprecated] Event-driven, streaming HTTP client for ReactPHP.
Stars: ✭ 228 (+430.23%)
Mutual labels:  reactphp
Supervisord
Async first supervisord HTTP API Client for PHP 7
Stars: ✭ 14 (-67.44%)
Mutual labels:  reactphp
net-mqtt-client-react
Asynchronous MQTT client built on React
Stars: ✭ 45 (+4.65%)
Mutual labels:  reactphp
Hhvmcraft
📦 Minecraft Beta 1.7.3 Server implemented in PHP/HHVM, powered by ReactPHP.
Stars: ✭ 180 (+318.6%)
Mutual labels:  reactphp
framework
Cygnite PHP Framework- A Modern Toolkit For Web Developers
Stars: ✭ 43 (+0%)
Mutual labels:  symfony-console
Zanzara
Asynchronous PHP Telegram Bot Framework built on top of ReactPHP
Stars: ✭ 107 (+148.84%)
Mutual labels:  reactphp
Demo
Demo for DriftPHP
Stars: ✭ 117 (+172.09%)
Mutual labels:  reactphp
terminable-loop-command
A Shell+PHP wrapper to run Symfony console commands in loop under a daemon or Kubernetes
Stars: ✭ 16 (-62.79%)
Mutual labels:  symfony-console
React Restify
A ReactPHP framework to create RESTfull api
Stars: ✭ 87 (+102.33%)
Mutual labels:  reactphp
readline-and-ncurses
Example demonstrating combining of readline and ncurses
Stars: ✭ 50 (+16.28%)
Mutual labels:  readline
Event Loop
ReactPHP's core reactor event loop that libraries can use for evented I/O.
Stars: ✭ 945 (+2097.67%)
Mutual labels:  reactphp
Child Process
Event-driven library for executing child processes with ReactPHP.
Stars: ✭ 225 (+423.26%)
Mutual labels:  reactphp
fancyline
Readline-esque library with fancy features
Stars: ✭ 72 (+67.44%)
Mutual labels:  readline
prompt-list
This repository has been archived, use Enquirer instead.
Stars: ✭ 13 (-69.77%)
Mutual labels:  readline
console
Menu helper for Symfony Console component
Stars: ✭ 19 (-55.81%)
Mutual labels:  symfony-console

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
    ]);
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].