Hi,
I use a small PHP script as a read-eval-print-loop for Drupal. It offers some convenience while testing/debugging Drupal. I have turned it into a Drush command. This form's file upload field is not allowing me to upload it, so I am pasting the code below. Thanks.

<?php

/**
 * @file
 * Drush-based Read-Eval-Print-Loop (REPL) for Drupal.
 *
 * Sample session:
 *         $ drush repl
 *          Go...
 *          > $no = node_load(1);
 *          > print_r($no);
 *          > bye
 *  
 * After typing "drush repl", wait until "Go..." appears in the screen.
 * Type "bye" to quit the shell.  'exit;' will also work.
 *
 * The command history is saved in a file named "drush_repl_history.txt" inside
 * the Drupal site root.
 *
 * The shell canNOT tolerate PHP fatal errors and will exit in such cases :-(
 *
 * This REPL is only useful for Windows users as Unix users can use the
 * excellent Facebook PHP shell (http://phpsh.org) for similar purposes.
 */


/**
 * Implementation of hook_drush_command().
 *
 * Provides the 'repl' drush command.
 */
function repl_drush_command() {
  $items = array();

  $items['repl'] = array(
    'description' => "An REPL for Drupal",
    'arguments' => array(),
    'options' => array(),
    'examples' => array(
      'drush repl',
    ),
    'aliases' => array('repl'),
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  );

  return $items;
}

/**
 * Implementation of hook_drush_help().
 */
function repl_drush_help($section) {
  switch ($section) {
    case 'drush:repl':
      return dt("This command will start a read-eval-print-loop where PHP commands can be executed (e.g. node_load().");
  }
}


/*
 * Implementation of drush_hook_COMMAND_validate().
 */
function drush_repl_validate() {
}


/**
 * Command callback.
 */
function drush_repl() {

  error_reporting(-1);

  $_SERVER = array(
    'HTTP_HOST' => 'localhost',
    'REMOTE_ADDR' => '127.0.0.1',
    'REQUEST_METHOD' => 'GET',
    'SCRIPT_NAME' => 'index.php',
    'SCRIPT_FILENAME' => 'index.php',
    'QUERY_STRING' => '',
  );


  ob_get_clean();


  echo 'Go...', PHP_EOL;

  $fp = fopen('drush_repl_history.txt', 'a');

  while (TRUE)
  {
    echo '> '; $line = rtrim(fgets(STDIN));

    if ('bye' === $line)
    {
        break;
    }

    fputs($fp, "$line\n");

    eval($line);
  }
}

Comments

greg.1.anderson’s picture

Status: Active » Closed (duplicate)

Duplicate of #600556: Interactive PHP Shell; please review existing submission and comment there.