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
44 * --script-path=/path/to/scripts:/another/path Run a script named
45 * example.php from specified paths
46 * @usage drush php:script
47 * List all available scripts.
48 * @usage drush php:script foo -- apple --cider
49 * Run foo.php script with argument 'apple' and option 'cider'. Note the
51 * @aliases scr,php-script
55 public function script(array $extra, $options = ['format' => 'var_export', 'script-path' => self::REQ])
58 $script = array_shift($extra);
61 return eval(stream_get_contents(STDIN));
62 } elseif (file_exists($script)) {
65 // Array of paths to search for scripts
66 $searchpath['cwd'] = $this->getConfig()->cwd();
68 // Additional script paths, specified by 'script-path' option
69 if ($script_path = $options['script-path']) {
70 foreach (explode(PATH_SEPARATOR, $script_path) as $path) {
71 $searchpath[] = $path;
74 $this->logger()->debug(dt('Searching for scripts in ') . implode(',', $searchpath));
78 // List all available scripts.
79 $files = Finder::create()
84 foreach ($files as $file) {
85 $all[] = $file->getRelativePathname();
87 return implode("\n", $all);
89 // Execute the specified script.
90 foreach ($searchpath as $path) {
91 $script_filename = $path . '/' . $script;
92 if (file_exists($script_filename . '.php')) {
93 $script_filename .= '.php';
95 if (file_exists($script_filename)) {
96 $found = $script_filename;
99 $all[] = $script_filename;
102 throw new \Exception(dt('Unable to find any of the following: @files', ['@files' => implode(', ', $all)]));
108 $return = include($found);
109 // 1 just means success so don't return it.
110 // http://us3.php.net/manual/en/function.include.php#example-120