Interactive Drupal Programming

If you like REPL (AKA interactive programming as in "php -a"), you might love this script as well. It loads the whole of Drupal and then waits for
you to type in any php statements. This statements can ALSO be any function defined in your Drupal instance. Here's a demo:


[ju@ju ~]$ php php/inter-drupal.php
Go...
echo drupal_strlen('hihi hoho haha'), PHP_EOL;
14

echo base_path(), PHP_E0L;
/./

echo 3;
3

$return_value = arg(0);
echo $return_value;
node

exit;
[ju@ju ~]$

<?php

/**
 * PHP script for interactive Drupal programming.
 *
 * Run the script using the command line version of PHP.
 * Once you see 'Go...', start typing php and drupal statements.
 * Type 'exit;' to exit.  Anything that causes a PHP fatal error
 * will also end the script execution.
 *
 * All statements must end in a single line.  In other words,
 * "while (1) { echo 1; }" is allowed, but
 * "while (1)
 *  {
 *       echo 1;
 *  }"
 *   - is not allowed.  Sorry.
 *
 *  Keep reading the comments.
 */

// Fill in the next two variables.
$drup_base_path = '/home/ju/public_html/intel_fp/';
$path = 'node';

// No need to touch the following unless you understand what you are doing.


/**
 * HTTP Server pretension begins.
 */
$_SERVER = array(
    'HTTP_HOST' => 'localhost',
    'REMOTE_ADDR' => '127.0.0.1',
    'REQUEST_METHOD' => 'GET',
    'SCRIPT_NAME' => 'index.php',
    'SCRIPT_FILENAME' => 'index.php',
    'QUERY_STRING' => "q=$path",
);

$_GET = array(
    'q' => $path,
);
// HTTP Server pretension ends.



chdir($drup_base_path);
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
ob_get_clean();

echo 'Go...', PHP_EOL;
while ($line = fgets(STDIN))
{
    eval($line);
}

echo 'Bye';

We are actually using a different version of the script (attached below) which stores the command history in a text file. But this one requires the PHP readline module which is unavailable on MSWindows.