Drush and PsySH, a Drupal REPL

At Twinbit we rely heavily on Drupal to provide full featured web applications to our clients, and in "Drupal land" there are a handful of tools that you really can't live without if you really want to ship features in a sustainable and efficient way.

Estimated time reading
4 m 0 sec

TL;DR: I wrote a small integration between Drush and PsySH that implements a REPL for Drupal, it enables you to do something like this:

drush-psysh in action.

What's a REPL anyway?

Wikipedia states that a REPL is:

a simple interactive computer programming environment where the user enters one or more expressions which are then evaluated, and the results displayed.

Ruby has (a very good) one, Python has one, PHP really has not. And no, php -a is not a real REPL.

Using a REPL is usually faster than setting a breakpoint just after a line whose result you want to check, or var_dumping its contents; not to mention the fact that if your development tools all live in the shell, it avoids the friction of leaving your terminal to do just that.

A REPL, besides improving the "discoverability" of your codebase, enables you to do this interactively, so that if you make a mistake you can immediately correct for it.

A REPL for PHP

The Opensource community has managed to fill the gap of a missing REPL for PHP, for example in projects like Boris, "A tiny REPL for PHP".
In the past, I and other people already integrated Boris in Drupal to provide a REPL.

PsySH is a newer and more featured Opensource project that "aims to provide a runtime developer console, interactive debugger and REPL for PHP."

For more information please head to the homepage of the project and check out the author's awesome presentation.
What really sold me on this project, is

  • that it supports readline.
  • that it can list the set of defined classes/functions/constants
  • that it can show the source code of any function available to the current environment.

Development tools in Drupal land

At Twinbit we rely heavily on Drupal to provide full featured web applications to our clients, and in "Drupal land" there are a handful of tools that you really can't live without if you really want to ship features in a sustainable and efficient way.

It's really worth mentioning a contrib module aptly named features which truly can be considered a basic building block for any Drupal developer. Features lets you define and store in code all the "assets" that you configured using the web UI (your content types, fields, views, system variables, almost anything).
Features manages to overcome one of Drupal's main issues which is, by default, to store everything in the database. At least with features the main properties of your site can be grepped, which is very important, not to mention it makes possible to easily deploy and maintain over time an existing installation.

Another indispensable tool is drush, which makes available to the commandline a gargantuan amount of tasks that otherwise must be carried out using the web UI. Some notable examples include:

  • Clearing the cache (drush cc)
  • Downloading/enabling/disabling modules (drush dl/en/dis)
  • Launching code updates (drush updb)
  • Getting/Setting system variables (drush vget/vset)

It's easy to see how the scriptability of your webapp really helps to easily deploy your code to multiple development environments, staging servers (for CI purposes), and production.

drush-psysh, a Drupal REPL

What sometimes is useful to have, something that improves the DX (Developer eXperience), is to be able to call one of Drupal's functions against your current database, and see the result, exactly what a REPL provides.

Also, as we have shown above, Drush is already the de facto standard for commandline applications that interface with Drupal.
It was only a matter of time before the two concepts collided, and as you can imagine the integration between the two took me very little time and effort.

The project lives on github, it's installed using Composer and it's a very thin integration between Drush and the aforementioned PsySH.

Installation is very easy, here's an excerpt from the README:

mkdir ~/.drush
cd $_
git clone https://github.com/grota/drush-psysh.git
composer install
drush cc drush

Latest posts from Giuseppe Rota

Keep in touch

Enter your email address below to receive all our latest articles and announcements via email.