5bad94b8de404fcf8294b97c3bb4f6bcf8c7fbe3
[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 --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
49      *   -- separator.
50      * @aliases scr,php-script
51      * @bootstrap max
52      * @throws \Exception
53      */
54     public function script(array $extra, $options = ['format' => 'var_export', 'script-path' => self::REQ])
55     {
56         $found = false;
57         $script = array_shift($extra);
58
59         if ($script == '-') {
60             return eval(stream_get_contents(STDIN));
61         } elseif (file_exists($script)) {
62             $found = $script;
63         } else {
64             // Array of paths to search for scripts
65             $searchpath['cwd'] = $this->getConfig()->cwd();
66
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;
71                 }
72             }
73             $this->logger()->debug(dt('Searching for scripts in ') . implode(',', $searchpath));
74
75             if (empty($script)) {
76                 $all = [];
77                 // List all available scripts.
78                 $files = Finder::create()
79                     ->files()
80                     ->name('*.php')
81                     ->depth(0)
82                     ->in($searchpath);
83                 foreach ($files as $file) {
84                     $all[] = $file->getRelativePathname();
85                 }
86                 return implode("\n", $all);
87             } else {
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';
93                     }
94                     if (file_exists($script_filename)) {
95                         $found = $script_filename;
96                         break;
97                     }
98                     $all[] = $script_filename;
99                 }
100                 if (!$found) {
101                     throw new \Exception(dt('Unable to find any of the following: @files', ['@files' => implode(', ', $all)]));
102                 }
103             }
104         }
105
106         if ($found) {
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
110             if ($return !== 1) {
111                 return $return;
112             }
113         }
114     }
115 }