{"id":16655993,"url":"https://github.com/mcrumm/pecan","last_synced_at":"2025-03-21T16:31:59.059Z","repository":{"id":15288647,"uuid":"18018198","full_name":"mcrumm/pecan","owner":"mcrumm","description":"ReactPHP Shell, based on the Symfony Console component.","archived":false,"fork":false,"pushed_at":"2014-09-12T17:58:04.000Z","size":299,"stargazers_count":44,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2024-10-13T09:55:55.028Z","etag":null,"topics":["reactphp","readline","symfony-console"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcrumm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-22T19:42:57.000Z","updated_at":"2023-11-11T05:14:36.000Z","dependencies_parsed_at":"2022-08-04T03:00:17.179Z","dependency_job_id":null,"html_url":"https://github.com/mcrumm/pecan","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcrumm%2Fpecan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcrumm%2Fpecan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcrumm%2Fpecan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcrumm%2Fpecan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcrumm","download_url":"https://codeload.github.com/mcrumm/pecan/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221817058,"owners_count":16885453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["reactphp","readline","symfony-console"],"created_at":"2024-10-12T09:55:55.839Z","updated_at":"2024-10-28T10:29:29.189Z","avatar_url":"https://github.com/mcrumm.png","language":"PHP","funding_links":[],"categories":["命令行","Command Line","命令行( Command Line )","命令行 Command Line"],"sub_categories":[],"readme":"# Pecan\n\nEvent-driven, non-blocking shell for [ReactPHP](http://reactphp.org).\n\nPecan (`/pɪˈkɑːn/`) provides a non-blocking alternative to the shell provided in the [Symfony Console](https://github.com/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.\n\n## Shells\n\n### Drupe\n\n`Pecan\\Drupe` is a standalone component for building event-driven console components.  I like to think of it as Pecan without the Shell.\n\nPass an `EventLoopInterface` to `start()` listening for input.\n\n```\n$loop  = \\React\\EventLoop\\Factory::create();\n$shell = new \\Pecan\\Drupe();\n\n// $shell-\u003estart() returns $loop to allow this chaining.\n$shell-\u003estart($loop)-\u003erun();\n```\n\n### Drupe Events\n\n - `running` - The shell is running.\n - `data` - Data was received from `STDIN`.\n - `error` - Indicates an I/O problem.\n - `close` - The shell was closed.\n\n### Shell\n\n`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`.\n\n\u003e**Note**: To maintain a Symfony Console-like workflow, calling `$shell-\u003erun()` on `Pecan\\Shell` starts the EventLoop, so make sure it gets called last.\n\n### Shell Events\n\n`Shell` emits the same events as `Drupe`.\n\n## Readline\n\n`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](http://nodejs.org/api/readline.html).\n\n### Readline Events\n\n - `line` - A line has been read from `STDIN`.\n - `pause` - Reading from the stream has been paused.\n - `resume` - Reading from the stream has resumed.\n - `error` - The input stream encountered an error.\n - `close` - Then input stream was closed.\n\n## Console\n\n`Pecan\\Console\\Console` provides a standard interface for working with `STDOUT` and `STDERR`.  It is inspired heavily by the [NodeJS Console Component](http://nodejs.org/api/console.html) and takes some functionality from the [Symfony Console Component](http://symfony.com)\n\n### Output\n\nThe Output classes extend the base Console Output. `StreamOutput` wraps a single stream resource, while `ConsoleOutput` contains both the `STDOUT` and `STDERR` streams.\n\n- `StreamOutputInterface`\n- `ConsoleOutputInterface`\n- `PecanOutput`\n\n## Using Pecan\n\n### Using Drupe as a Standalone Shell\n\n```php\nuse Pecan\\Drupe;\n\n$loop   = \\React\\EventLoop\\Factory::create();\n$shell  = new Drupe();\n\n// Example one-time callback to write the initial prompt.\n// This resumes reading from STDIN and kicks off the shell.\n$shell-\u003eonce('running', function (Drupe $shell) {\n    $shell-\u003esetPrompt('drupe\u003e ')-\u003eprompt();\n});\n\n// Example callback for the data event.\n// By convention, any call to write() will be followed by a call to prompt() \n// once the data has been written to the output stream.\n$shell-\u003eon('data', function ($line, Drupe $shell) {\n\n    $command = (!$line \u0026\u0026 strlen($line) == 0) ? false : rtrim($line);\n\n    if ('exit' === $command || false === $command) {\n        $shell-\u003eclose();\n    } else {\n        $shell-\u003ewriteln(sprintf(PHP_EOL.'// in: %s', $line));\n    }\n\n});\n\n// Example callback for the close event.\n$shell-\u003eon('close', function ($code, Drupe $shell) {\n    $shell-\u003ewriteln([\n        '// Goodbye.',\n        sprintf('// Shell exits with code %d', $code),\n    ]);\n});\n\n$shell-\u003estart($loop)-\u003erun();\n```\n\n### Using Shell with Symfony Console Applications\n\nHere is a shell that echoes back any input it receives, and then exits.\n\n```php\n// Pecan\\Shell wraps a standard Console Application.\nuse Symfony\\Component\\Console\\Application;\nuse Pecan\\Shell;\n\n$shell = new Shell(new Application('pecan'));\n\n$shell-\u003eon('data', function($line, Shell $shell) {\n    $shell-\u003ewrite($line)-\u003ethen(function($shell) {\n        $shell-\u003eclose();\n    });\n});\n\n$shell-\u003erun();\n```\n\n### Injecting An `EventLoopInterface` into `Pecan\\Shell`\n\nUnless 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.\n\n```php\nuse Symfony\\Component\\Console\\Application;\nuse Pecan\\Shell;\n\n$loop  = \\React\\EventLoop\\Factory::create();\n\n// Do other things requiring $loop...\n\n$shell = new Shell(new Application('pecan'), $loop);\n\n// We must still let the shell run the EventLoop.\n$shell-\u003erun();\n```\n\n### Example `exit` callback\n\n```php\n// Example callback for the exit event.\n$shell-\u003eon('exit', function($code, \\Pecan\\Shell $shell) {\n    $shell-\u003eemit('output', [\n        [\n            'Goodbye.',\n            sprintf('// Shell exits with code %d', $code)\n        ],\n        true\n    ]);\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcrumm%2Fpecan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcrumm%2Fpecan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcrumm%2Fpecan/lists"}