Skip to main content
drush scr

Use cases for "drush scr" (a) "drush php-script"

Drush is one of the most powerful weapon for Drupal developers, which contains a host of useful shortcuts for common tasks.

drush logo

"php-script" (scr) is a great utility that comes packaged with Drush to allow a piece of code to be run within a Drupal environment (runs the given php script(s) after a full Drupal bootstrap). It essentially voids the need to manually prepare statements for a full Drupal Bootstrap and does more than that, by printing any watchdog warnings/errors reported right after the execution of script(s).

So what could be some of the real use cases of this tiny utility?

Case #1: Execute a complex piece of code independently & reduce development time

Let's say you have a piece of complex code (eg: An algorithm to parse all posts by an author and assign him a rating) as below:

<?php

//load all blogs by an author
$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'blog')
  ->propertyCondition('status', 1)
  ->propertyCondition('uid', {your uid})
  ->execute();
$nodes = node_load_multiple(array_keys($result['node'])); //let's say 100-500 nodes for a user
$score = //your algorithm here
print "Score calculated for the user: {$score}" . PHP_EOL;

Traditionally this piece of code may be tested when the page with this block/content is triggered. If you are finding that your algorithm is having some wired bugs, you would have to load the full page (and even clear cache) to iteratively test & develop the piece of code. Instead, save this as a PHP file and run "drush scr {your-file-name}". You will see instant results & test faster than ever.

Case #2: Testing the Code to be executed after a major event (eg: Upon Successful Payment of a Commerce Order)

You'd want to mark a user "premium" user just after he completes a successful payment on your commerce site. Traditionally you would implement a payment successful hook, setup a dummy payment method and go through the whole process of creating an order -> making payment for the same. Testing and debugging takes a hell lot of time in this process.

Instead, with "drush scr" you could test the actual piece of code and then simply wrap it in the hook for successful payment. That's it.

Case #3: Testing CRON queue callback

This is even more complex case than the earlier two, where "drush scr" is a real gift. You want to do some business processing onto all nodes in your system, with a hook_cron. The cron implementation uses queue for the processing. If you want to test that through usual routine, you will have to wait forever for the cron to execute and the results won't appear on your screen instantly either. You might have to scan through watchdog to know the result of callback. Even worse, if your code is having some obvious bugs - the watchdog events won't prove useful either.

"drush scr" can used in this case to directly trigger the "queue callback", with an example node for the callback to process. You could trigger the callback as many times as you want, till you get satisfaction with the code you've written.

There may be numerous other cases where "drush scr" could come for the rescue. But, this is what I could figure out as most common use cases.