2 namespace Drush\Commands\core;
4 use Drush\Commands\DrushCommands;
6 use Drush\SiteAlias\SiteAliasManagerAwareInterface;
7 use Drush\SiteAlias\SiteAliasManagerAwareTrait;
9 class EditCommands extends DrushCommands implements SiteAliasManagerAwareInterface
11 use SiteAliasManagerAwareTrait;
14 * Edit drushrc, site alias, and Drupal settings.php files.
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
34 public function edit($filter = null)
38 // Apply any filter that was supplied.
40 foreach ($all as $file => $display) {
41 if (strpos($file, $filter) === false) {
47 $exec = drush_get_editor();
48 if (count($all) == 1) {
49 $filepath = current($all);
51 $choice = $this->io()->choice(dt("Choose a file to edit"), $all);
53 // We don't yet support launching editor at a start line.
54 if ($pos = strpos($filepath, ':')) {
55 $filepath = substr($filepath, 0, $pos);
58 return drush_shell_exec_interactive($exec, $filepath, $filepath);
61 public function load($headers = true)
63 $php_header = $php = $rcs_header = $rcs = $aliases_header = $aliases = $drupal_header = $drupal = [];
64 $php = $this->phpIniFiles();
67 $php_header = ['phpini' => '-- PHP ini files --'];
71 $bash = $this->bashFiles();
74 $bash_header = ['bash' => '-- Bash files --'];
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);
82 $rcs_header = ['drushrc' => '-- Drushrc --'];
86 if ($aliases = $this->siteAliasManager()->listAllFilePaths()) {
88 $aliases = array_combine($aliases, $aliases);
90 $aliases_header = ['aliases' => '-- Aliases --'];
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;
100 $path = realpath(DRUPAL_ROOT . '/.htaccess');
101 $drupal[$path] = $path;
103 $drupal_header = ['drupal' => '-- Drupal --'];
107 return array_merge($php_header, $php, $bash_header, $bash, $rcs_header, $rcs, $aliases_header, $aliases, $drupal_header, $drupal);
110 public static function phpIniFiles()
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;
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;
129 public static function bashFiles()
132 $home = Drush::config()->home();
133 if ($bashrc = self::findBashrc($home)) {
134 $bashFiles[$bashrc] = $bashrc;
136 $prompt = $home . '/.drush/drush.prompt.sh';
137 if (file_exists($prompt)) {
138 $bashFiles[$prompt] = $prompt;
144 * Determine which .bashrc file is best to use on this platform.
146 * TODO: Also exists as InitCommands::findBashrc. Decide on class-based
147 * way to share code like this.
149 public static function findBashrc($home)
151 return $home . "/.bashrc";
154 public function complete()
156 return ['values' => $this->load(false)];