GithubHelp home page GithubHelp logo

gakowalski / php-interactive-shell Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 4.0 20 KB

Simple PHP interactive shell for Windows.

License: GNU General Public License v3.0

PHP 100.00%
php windows interactive-shell shell command-line

php-interactive-shell's Introduction

php-interactive-shell

Simple PHP interactive shell for Windows.

Background

As you may know, there exists special parameter -a in PHP command line, which is supposed to execute PHP in some kind of "interactive run". In practice, one of two possible modes of execution is possible: "interactive mode" and "interactive shell". Interactive mode simply waits for data from standard input device terminated with CTRL+Z (Windows) or maybe CTRL+D (probably Linux) and then tries to interpret that data as PHP code. Interactive shell works different: every line of data from standard input device is interpreted immediately.

After issuing php -a command PHP checks if there is readline extension enabled. If not, interactive mode is chosen. Otherwise, interactive shell is executed. As readline extension isn't available on Windows, only interactive mode is possible in that system.

This little script is trying to simulate some very simple interactive shell. It can be easily extended and I've already used this script with proper modifications in some of my projects.

This code is based on anynomous comment under PHP online documentation.

Usage

To run it:

php shell.php

Then try to execute some commands, e.g. echo "Hello\n". Last ; is optional to simplify issuing of single commands. All other semicolons are mandatory.

At this time there is no command prompt, you can add it manually in the code.

To stop the program, simply execute:

exit

Special properties of Windows console

Although this script doesn't implement any special features itself, there are some useful functionalities inherited from Windows DOSKEY utility, which is now merged into Windows command-line processor:

  • Line editing
    • You can change cursor position within edited line with arrow keys, HOME, END and other keys;
    • Insert and replace mode switched with INSERT key;
  • Command history
    • Use up and down arrow keys to browse recent commands while editing;
    • Use F7 key to open list of all recent commands in text window;
    • Use last command as a template for new one;

For complete listing please take a look at TechNet article about DOSKEY.

ANSI colors

On Windows 10 output comments are colored red.

Ideas taken from PsySH

  • $_ contains result of last operation or NULL.
  • $_e contains last uncatched exception (uncatched by your code).

Other info

  • break command stops the shell but not he whole script (so you can call shell.php from other scripts - e.g. instead of die() while debugging).

php-interactive-shell's People

Contributors

gakowalski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.