Drush REPL, or Drupal, Drush, PsySH for easier development

By:

on

May 08, 2015

Compared to languages such as Ruby or Python, PHP lacks a good REPL. REPL stands for read-eval-print loop, and no, php -a doesn't count. A good REPL will, for one, Print the result of each operation, which the standard CLI SAPI doesn't do (and it had trouble with the Eval part until recently). What the php -a client is, however, is an interactive scripting environement. You can type code, statements, as if you were within a php script, and they'll be evaluate the same way.

Enter boris. It is a quick, simple, tiny, powerful REPL for php, that will not die. The way boris works is to fork itself and run your code in a separate process. This way, the main REPL is not affected by the code you type into it, and crashes in the code-running process doesn't affect the main process. For a while, boris sufficed for my needs, but I kept expecting more out of my REPL.

Enter PsySH. PsySH was recently shown to me by a coworker (there's also a presentation by its author). PsySH takes the basic concept of a REPL, and adds tons of useful features:

  • PsySH knows about the documentation of the code (from the manual for userland core php and docblocks for typed php), and makes it available through a doc command (we recommend installing the manual locally);
  • It does reflection for all code through the list command;
  • It allows you to see the source of the code through the source command;
  • It keeps a history of your input, available through the history command; and
  • It lets you start a shell anywhere in your code with the handy eval(\Psy\sh()); shorthand.

Installing PsySH is rather straight-forward. First, make sure that you have composer available globally (locally can work in a pinch, but global is preferred) and run composer g require psy/psysh:@stable. If you've setup your $PATH to include your COMPOSER_PATH, you'll have the psysh command available anywhere!

What about Drupal?

OpenConcept is a Drupal shop, so it doesn't help us if awesome tools don't work with it. Luckily for us, Giuseppe Rota wrote a drush plugin that integrates PsySH into drupal (if you don't use drush, find it, learn it, love it). Just follow the instructions to install the plugin (unfortunately there is not d.o project yet) and once you're done, you can run drush repl from any site to start PsySH within a bootstraped drupal environment.

Edit (2015/05/11): Giuseppe has applied to get his drush plugin promoted to a full drupal project, so hopefully, installation will soon be a simple drush dl away!

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.