2 namespace Drush\Commands\core;
4 use Drush\Commands\DrushCommands;
5 use Symfony\Component\Finder\Finder;
7 class PhpCommands extends DrushCommands
11 * Evaluate arbitrary php code after bootstrapping Drupal (if available).
14 * @param $code PHP code
15 * @usage drush php:eval 'variable_set("hello", "world");'
16 * Sets the hello variable using Drupal API.'
17 * @usage drush php:eval '$node = node_load(1); print $node->title;'
18 * Loads node with nid 1 and then prints its title.
19 * @usage drush php:eval "file_unmanaged_copy(\'$HOME/Pictures/image.jpg\', \'public://image.jpg\');"
20 * Copies a file whose path is determined by an environment\'s variable. Note the use of double quotes so the variable $HOME gets replaced by its value.
21 * @usage drush php:eval "node_access_rebuild();"
22 * Rebuild node access permissions.
23 * @aliases eval,ev,php-eval
26 public function evaluate($code, $options = ['format' => 'var_export'])
28 return eval($code . ';');
32 * Run php a script after a full Drupal bootstrap.
34 * A useful alternative to eval command when your php is lengthy or you
35 * can't be bothered to figure out bash quoting. If you plan to share a
36 * script with others, consider making a full Drush command instead, since
37 * that's more self-documenting. Drush provides commandline options to the
38 * script via a variable called $extra.
41 * @option script-path Additional paths to search for scripts, separated by
42 * : (Unix-based systems) or ; (Windows).
43 * @usage drush php:script example --script-path=/path/to/scripts:/another/path
44 * Run a script named example.php from specified paths
45 * @usage drush php:script
46 * List all available scripts.
47 * @usage drush php:script foo -- apple --cider
48 * Run foo.php script with argument 'apple' and option 'cider'. Note the
50 * @aliases scr,php-script
54 public function script(array $extra, $options = ['format' => 'var_export', 'script-path' => self::REQ])
57 $script = array_shift($extra);
60 return eval(stream_get_contents(STDIN));
61 } elseif (file_exists($script)) {
64 // Array of paths to search for scripts
65 $searchpath['cwd'] = $this->getConfig()->cwd();
67 // Additional script paths, specified by 'script-path' option
68 if ($script_path = $options['script-path']) {
69 foreach (explode(PATH_SEPARATOR, $script_path) as $path) {
70 $searchpath[] = $path;
73 $this->logger()->debug(dt('Searching for scripts in ') . implode(',', $searchpath));
77 // List all available scripts.
78 $files = Finder::create()
83 foreach ($files as $file) {
84 $all[] = $file->getRelativePathname();
86 return implode("\n", $all);
88 // Execute the specified script.
89 foreach ($searchpath as $path) {
90 $script_filename = $path . '/' . $script;
91 if (file_exists($script_filename . '.php')) {
92 $script_filename .= '.php';
94 if (file_exists($script_filename)) {
95 $found = $script_filename;
98 $all[] = $script_filename;
101 throw new \Exception(dt('Unable to find any of the following: @files', ['@files' => implode(', ', $all)]));
107 $return = include($found);
108 // 1 just means success so don't return it.
109 // http://us3.php.net/manual/en/function.include.php#example-120