997d6c592ebfa388def886268283d0e52a4fb9a5
[yaffs-website] / vendor / drush / drush / src / Commands / core / EditCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Drush\Commands\DrushCommands;
5 use Drush\Drush;
6 use Drush\SiteAlias\SiteAliasManagerAwareInterface;
7 use Drush\SiteAlias\SiteAliasManagerAwareTrait;
8
9 class EditCommands extends DrushCommands implements SiteAliasManagerAwareInterface
10 {
11     use SiteAliasManagerAwareTrait;
12
13     /**
14      * Edit drushrc, site alias, and Drupal settings.php files.
15      *
16      * @command core:edit
17      * @bootstrap max
18      * @param $filter A substring for filtering the list of files. Omit this argument to choose from loaded files.
19      * @optionset_get_editor
20      * @usage drush core:config
21      *   Pick from a list of config/alias/settings files. Open selected in editor.
22      * @usage drush --bg core-config
23      *   Return to shell prompt as soon as the editor window opens.
24      * @usage drush core:config etc
25      *   Edit the global configuration file.
26      * @usage drush core:config demo.alia
27      * Edit a particular alias file.
28      * @usage drush core:config sett
29      *   Edit settings.php for the current Drupal site.
30      * @usage drush core:config --choice=2
31      *  Edit the second file in the choice list.
32      * @aliases conf,config,core-edit
33      */
34     public function edit($filter = null)
35     {
36         $all = $this->load();
37
38         // Apply any filter that was supplied.
39         if ($filter) {
40             foreach ($all as $file => $display) {
41                 if (strpos($file, $filter) === false) {
42                     unset($all[$file]);
43                 }
44             }
45         }
46
47         $exec = drush_get_editor();
48         if (count($all) == 1) {
49             $filepath = current($all);
50         } else {
51             $choice = $this->io()->choice(dt("Choose a file to edit"), $all);
52             $filepath = $choice;
53             // We don't yet support launching editor at a start line.
54             if ($pos = strpos($filepath, ':')) {
55                 $filepath = substr($filepath, 0, $pos);
56             }
57         }
58         return drush_shell_exec_interactive($exec, $filepath, $filepath);
59     }
60
61     public function load($headers = true)
62     {
63         $php_header = $php = $rcs_header = $rcs = $aliases_header = $aliases = $drupal_header = $drupal = [];
64         $php = $this->phpIniFiles();
65         if (!empty($php)) {
66             if ($headers) {
67                 $php_header = ['phpini' => '-- PHP ini files --'];
68             }
69         }
70
71         $bash = $this->bashFiles();
72         if (!empty($bash)) {
73             if ($headers) {
74                 $bash_header = ['bash' => '-- Bash files --'];
75             }
76         }
77
78         if ($rcs = Drush::config()->get('runtime.config.paths')) {
79             // @todo filter out any files that are within Drush.
80             $rcs = array_combine($rcs, $rcs);
81             if ($headers) {
82                 $rcs_header = ['drushrc' => '-- Drushrc --'];
83             }
84         }
85
86         if ($aliases = $this->siteAliasManager()->listAllFilePaths()) {
87             sort($aliases);
88             $aliases = array_combine($aliases, $aliases);
89             if ($headers) {
90                 $aliases_header = ['aliases' => '-- Aliases --'];
91             }
92         }
93         if ($site_root = Drush::bootstrap()->confPath()) {
94             $path = realpath($site_root . '/settings.php');
95             $drupal[$path] = $path;
96             if (file_exists($site_root . '/settings.local.php')) {
97                 $path = realpath($site_root . '/settings.local.php');
98                 $drupal[$path] = $path;
99             }
100             $path = realpath(DRUPAL_ROOT . '/.htaccess');
101             $drupal[$path] = $path;
102             if ($headers) {
103                 $drupal_header = ['drupal' => '-- Drupal --'];
104             }
105         }
106
107         return array_merge($php_header, $php, $bash_header, $bash, $rcs_header, $rcs, $aliases_header, $aliases, $drupal_header, $drupal);
108     }
109
110     public static function phpIniFiles()
111     {
112         $ini_files = [];
113         $path = php_ini_loaded_file();
114         $ini_files[$path] = $path;
115         if ($drush_ini = getenv('DRUSH_INI')) {
116             if (file_exists($drush_ini)) {
117                 $ini_files[$drush_ini] = $drush_ini;
118             }
119         }
120         foreach ([DRUSH_BASE_PATH, '/etc/drush', Drush::config()->user() . '/.drush'] as $ini_dir) {
121             if (file_exists($ini_dir . "/drush.ini")) {
122                 $path = realpath($ini_dir . "/drush.ini");
123                 $ini_files[$path] = $path;
124             }
125         }
126         return $ini_files;
127     }
128
129     public static function bashFiles()
130     {
131         $bashFiles = [];
132         $home = Drush::config()->home();
133         if ($bashrc = self::findBashrc($home)) {
134             $bashFiles[$bashrc] = $bashrc;
135         }
136         $prompt = $home . '/.drush/drush.prompt.sh';
137         if (file_exists($prompt)) {
138             $bashFiles[$prompt] = $prompt;
139         }
140         return $bashFiles;
141     }
142
143     /**
144      * Determine which .bashrc file is best to use on this platform.
145      *
146      * TODO: Also exists as InitCommands::findBashrc. Decide on class-based
147      * way to share code like this.
148      */
149     public static function findBashrc($home)
150     {
151         return $home . "/.bashrc";
152     }
153
154     public function complete()
155     {
156         return ['values' => $this->load(false)];
157     }
158 }