0d32e183e063c31919da92c8d5b6e39b20c80585
[yaffs-website] / vendor / drush / drush / src / Commands / core / PhpCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Drush\Commands\DrushCommands;
5 use Symfony\Component\Finder\Finder;
6
7 class PhpCommands extends DrushCommands
8 {
9
10     /**
11      * Evaluate arbitrary php code after bootstrapping Drupal (if available).
12      *
13      * @command php:eval
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
24      * @bootstrap max
25      */
26     public function evaluate($code, $options = ['format' => 'var_export'])
27     {
28         return eval($code . ';');
29     }
30
31     /**
32      * Run php a script after a full Drupal bootstrap.
33      *
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.
39      *
40      * @command php:script
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
50      *   -- separator.
51      * @aliases scr,php-script
52      * @bootstrap max
53      * @throws \Exception
54      */
55     public function script(array $extra, $options = ['format' => 'var_export', 'script-path' => self::REQ])
56     {
57         $found = false;
58         $script = array_shift($extra);
59
60         if ($script == '-') {
61             return eval(stream_get_contents(STDIN));
62         } elseif (file_exists($script)) {
63             $found = $script;
64         } else {
65             // Array of paths to search for scripts
66             $searchpath['cwd'] = $this->getConfig()->cwd();
67
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;
72                 }
73             }
74             $this->logger()->debug(dt('Searching for scripts in ') . implode(',', $searchpath));
75
76             if (empty($script)) {
77                 $all = [];
78                 // List all available scripts.
79                 $files = Finder::create()
80                     ->files()
81                     ->name('*.php')
82                     ->depth(0)
83                     ->in($searchpath);
84                 foreach ($files as $file) {
85                     $all[] = $file->getRelativePathname();
86                 }
87                 return implode("\n", $all);
88             } else {
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';
94                     }
95                     if (file_exists($script_filename)) {
96                         $found = $script_filename;
97                         break;
98                     }
99                     $all[] = $script_filename;
100                 }
101                 if (!$found) {
102                     throw new \Exception(dt('Unable to find any of the following: @files', ['@files' => implode(', ', $all)]));
103                 }
104             }
105         }
106
107         if ($found) {
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
111             if ($return !== 1) {
112                 return $return;
113             }
114         }
115     }
116 }