context.inc

  1. 8.0.x includes/context.inc
  2. 6.x includes/context.inc
  3. 7.x includes/context.inc
  4. 3.x includes/context.inc
  5. 4.x includes/context.inc
  6. 5.x includes/context.inc
  7. master includes/context.inc

The Drush context API implementation.

This API acts as a storage mechanism for all options, arguments and configuration settings that are loaded into drush.

This API also acts as an IPC mechanism between the different drush commands, and provides protection from accidentally overriding settings that are needed by other parts of the system.

It also avoids the necessity to pass references through the command chain and allows the scripts to keep track of whether any settings have changed since the previous execution.

This API defines several contexts that are used by default.

Argument contexts : These contexts are used by Drush to store information on the command. They have their own access functions in the forms of drush_set_arguments(), drush_get_arguments(), drush_set_command(), drush_get_command().

command : The drush command being executed. arguments : Any additional arguments that were specified.

Setting contexts : These contexts store options that have been passed to the drush.php script, either through the use of any of the config files, directly from the command line through --option='value' or through a JSON encoded string passed through the STDIN pipe.

These contexts are accessible through the drush_get_option() and drush_set_option() functions. See drush_context_names() for a description of all of the contexts.

Drush commands may also choose to save settings for a specific context to the matching configuration file through the drush_save_config() function.

Functions

Namesort descending Description
drush_context_names Return a list of the valid drush context names.
drush_expand_short_form_options For all global options with a short form, convert all options in the option array that use the short form into the long form.
drush_get_arguments Gets the command line arguments passed to Drush.
drush_get_command Return the command being executed.
drush_get_context Return a specific context, or the whole context cache
drush_get_context_options Get all of the values for an option in every context.
drush_get_merged_options Retrieves a collapsed list of all options.
drush_get_merged_prefixed_options Retrieves a collapsed list of all options with a specified prefix.
drush_get_option Get the value for an option.
drush_get_option_list Get the value for an option and return it as a list. If the option in question is passed on the command line, its value should be a comma-separated list (e.g. --flag=1,2,3). If the option was set in a drushrc.php file, then its value may be either…
drush_get_option_override Get the value for an option, but first checks the provided option overrides.
drush_load_config Load drushrc files (if available) from several possible locations.
drush_load_config_file
drush_save_config Save the settings in a specific context to the applicable configuration file This is useful is you want certain settings to be available automatically the next time a command is executed.
drush_set_arguments Set the arguments passed to the drush.php script.
drush_set_command Set the command being executed.
drush_set_config_options
drush_set_config_special_contexts There are certain options such as 'site-aliases' and 'command-specific' that must be merged together if defined in multiple drush configuration files. If we did not do this merge, then the last configuration file that defined any…
drush_set_context Set a specific context.
drush_set_default A small helper function to set the value in the default context
drush_set_option Set an option in one of the option contexts.
drush_sitealias_get_option Get an option out of the specified alias. If it has not been set in the alias, then get it via drush_get_option.
drush_unset_option Remove a setting from a specific context.
_drush_config_file Return a list of possible drushrc file locations.
_drush_get_option Helper function to recurse through possible option names

File

includes/context.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * The Drush context API implementation.
  5. *
  6. * This API acts as a storage mechanism for all options, arguments and
  7. * configuration settings that are loaded into drush.
  8. *
  9. * This API also acts as an IPC mechanism between the different drush commands,
  10. * and provides protection from accidentally overriding settings that are
  11. * needed by other parts of the system.
  12. *
  13. * It also avoids the necessity to pass references through the command chain
  14. * and allows the scripts to keep track of whether any settings have changed
  15. * since the previous execution.
  16. *
  17. * This API defines several contexts that are used by default.
  18. *
  19. * Argument contexts :
  20. * These contexts are used by Drush to store information on the command.
  21. * They have their own access functions in the forms of
  22. * drush_set_arguments(), drush_get_arguments(), drush_set_command(),
  23. * drush_get_command().
  24. *
  25. * command : The drush command being executed.
  26. * arguments : Any additional arguments that were specified.
  27. *
  28. * Setting contexts :
  29. * These contexts store options that have been passed to the drush.php
  30. * script, either through the use of any of the config files, directly from
  31. * the command line through --option='value' or through a JSON encoded string
  32. * passed through the STDIN pipe.
  33. *
  34. * These contexts are accessible through the drush_get_option() and
  35. * drush_set_option() functions. See drush_context_names() for a description
  36. * of all of the contexts.
  37. *
  38. * Drush commands may also choose to save settings for a specific context to
  39. * the matching configuration file through the drush_save_config() function.
  40. */
  41. use Drush\Log\LogLevel;
  42. /**
  43. * Return a list of the valid drush context names.
  44. *
  45. * These context names are carefully ordered from
  46. * highest to lowest priority.
  47. *
  48. * These contexts are evaluated in a certain order, and the highest priority value
  49. * is returned by default from drush_get_option. This allows scripts to check whether
  50. * an option was different before the current execution.
  51. *
  52. * Specified by the script itself :
  53. * process : Generated in the current process.
  54. * cli : Passed as --option=value to the command line.
  55. * stdin : Passed as a JSON encoded string through stdin.
  56. * specific : Defined in a command-specific option record, and
  57. * set in the command context whenever that command is used.
  58. * alias : Defined in an alias record, and set in the
  59. * alias context whenever that alias is used.
  60. *
  61. * Specified by config files :
  62. * custom : Loaded from the config file specified by --config or -c
  63. * site : Loaded from the drushrc.php file in the Drupal site directory.
  64. * drupal : Loaded from the drushrc.php file in the Drupal root directory.
  65. * user : Loaded from the drushrc.php file in the user's home directory.
  66. * home.drush Loaded from the drushrc.php file in the $HOME/.drush directory.
  67. * system : Loaded from the drushrc.php file in the system's $PREFIX/etc/drush directory.
  68. * drush : Loaded from the drushrc.php file in the same directory as drush.php.
  69. *
  70. * Specified by the script, but has the lowest priority :
  71. * default : The script might provide some sensible defaults during init.
  72. */
  73. function drush_context_names() {
  74. static $contexts = array(
  75. 'process', 'cli', 'stdin', 'specific', 'alias',
  76. 'custom', 'site', 'drupal', 'user', 'home.drush', 'system',
  77. 'drush', 'default');
  78. return $contexts;
  79. }
  80. /**
  81. * Return a list of possible drushrc file locations.
  82. *
  83. * @context
  84. * A valid drush context from drush_context_names().
  85. * @prefix
  86. * Optional. Specify a prefix to prepend to ".drushrc.php" when looking
  87. * for config files. Most likely used by contrib commands.
  88. * @return
  89. * An associative array containing possible config files to load
  90. * The keys are the 'context' of the files, the values are the file
  91. * system locations.
  92. */
  93. function _drush_config_file($context, $prefix = NULL, $version = '') {
  94. $configs = array();
  95. $base_name = 'drush' . $version . 'rc.php';
  96. $config_file = $prefix ? $prefix . '.' . $base_name : $base_name;
  97. // Did the user explicitly specify a config file?
  98. if ($config_list = (array)drush_get_context('DRUSH_CONFIG')) {
  99. foreach ($config_list as $config) {
  100. if (is_dir($config)) {
  101. $config = $config . '/' . $config_file;
  102. }
  103. $configs['custom'][] = $config;
  104. }
  105. }
  106. if ($drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT')) {
  107. $configs['drupal'] = array(
  108. $drupal_root . '/../drush/' . $config_file,
  109. $drupal_root . '/sites/all/drush/' . $config_file,
  110. $drupal_root . '/drush/' . $config_file,
  111. );
  112. if ($conf_path = drush_get_context('DRUSH_SELECTED_DRUPAL_SITE_CONF_PATH', 'sites/default')) {
  113. $site_path = $drupal_root . '/' . $conf_path;
  114. $configs['site'] = $site_path . "/" . $config_file;
  115. }
  116. }
  117. // in the user home directory
  118. $server_home = drush_server_home();
  119. if (isset($server_home)) {
  120. $configs['user'] = $server_home . '/.' . $config_file;
  121. }
  122. // in $HOME/.drush directory
  123. $per_user_config_dir = drush_get_context('DRUSH_PER_USER_CONFIGURATION');
  124. if (!empty($per_user_config_dir)) {
  125. $configs['home.drush'] = $per_user_config_dir . '/' . $config_file;
  126. }
  127. // In the system wide configuration folder.
  128. $configs['system'] = drush_get_context('DRUSH_SITE_WIDE_CONFIGURATION') . '/' . $config_file;
  129. // in the drush installation folder
  130. $configs['drush'] = dirname(__FILE__) . '/../' . $config_file;
  131. return empty($configs[$context]) ? '' : $configs[$context];
  132. }
  133. /**
  134. * Load drushrc files (if available) from several possible locations.
  135. */
  136. function drush_load_config($context) {
  137. drush_load_config_file($context, _drush_config_file($context));
  138. drush_load_config_file($context, _drush_config_file($context, '', DRUSH_MAJOR_VERSION));
  139. }
  140. function drush_load_config_file($context, $config_list) {
  141. foreach ((array)$config_list as $config) {
  142. if (file_exists($config)) {
  143. $options = $aliases = $command_specific = $override = array();
  144. drush_log(dt('Loading drushrc "!config" into "!context" scope.', array('!config' => realpath($config), '!context' => $context)), LogLevel::BOOTSTRAP);
  145. $ret = @include_once($config);
  146. if ($ret === FALSE) {
  147. drush_log(dt('Cannot open drushrc "!config", ignoring.', array('!config' => realpath($config))), LogLevel::WARNING);
  148. return FALSE;
  149. }
  150. if (!empty($options) || !empty($aliases) || !empty($command_specific) || !empty($override)) {
  151. $options = array_merge(drush_get_context($context), $options);
  152. $options['config-file'] = realpath($config);
  153. unset($options['site-aliases']);
  154. $options['command-specific'] = array_merge(isset($command_specific) ? $command_specific : array(), isset($options['command-specific']) ? $options['command-specific'] : array());
  155. drush_set_config_options($context, $options, $override);
  156. }
  157. }
  158. }
  159. }
  160. function drush_set_config_options($context, $options, $override = array()) {
  161. // Copy 'config-file' into 'context-path', converting to an array to hold multiple values if necessary
  162. if (isset($options['config-file'])) {
  163. if (isset($options['context-path'])) {
  164. $options['context-path'] = array_merge(array($options['config-file']), is_array($options['context-path']) ? $options['context-path'] : array($options['context-path']));
  165. }
  166. else {
  167. $options['context-path'] = $options['config-file'];
  168. }
  169. }
  170. // Take out $aliases and $command_specific options
  171. drush_set_config_special_contexts($options);
  172. drush_set_context($context, $options);
  173. }
  174. /**
  175. * For all global options with a short form, convert all options in the option
  176. * array that use the short form into the long form.
  177. */
  178. function drush_expand_short_form_options(&$options) {
  179. foreach (drush_get_global_options() as $name => $info) {
  180. if (is_array($info)) {
  181. // For any option with a short form, check to see if the short form was set in the
  182. // options. If it was, then rename it to its long form.
  183. if (array_key_exists('short-form', $info) && array_key_exists($info['short-form'], $options)) {
  184. if (!array_key_exists($name, $options) || !array_key_exists('merge-pathlist', $info)) {
  185. $options[$name] = $options[$info['short-form']];
  186. }
  187. else {
  188. $options[$name] = array_merge((array)$options[$name], (array)$options[$info['short-form']]);
  189. }
  190. unset($options[$info['short-form']]);
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * There are certain options such as 'site-aliases' and 'command-specific'
  197. * that must be merged together if defined in multiple drush configuration
  198. * files. If we did not do this merge, then the last configuration file
  199. * that defined any of these properties would overwrite all of the options
  200. * that came before in previously-loaded configuration files. We place
  201. * all of them into their own context so that this does not happen.
  202. */
  203. function drush_set_config_special_contexts(&$options) {
  204. if (isset($options) && is_array($options)) {
  205. $has_command_specific = array_key_exists('command-specific', $options);
  206. // Change the keys of the site aliases from 'alias' to '@alias'
  207. if (array_key_exists('site-aliases', $options)) {
  208. $user_aliases = $options['site-aliases'];
  209. $options['site-aliases'] = array();
  210. foreach ($user_aliases as $alias_name => $alias_value) {
  211. if (substr($alias_name,0,1) != '@') {
  212. $alias_name = "@$alias_name";
  213. }
  214. $options['site-aliases'][$alias_name] = $alias_value;
  215. }
  216. }
  217. // Expand -s into --simulate, etc.
  218. drush_expand_short_form_options($options);
  219. foreach (drush_get_global_options() as $name => $info) {
  220. if (is_array($info)) {
  221. // For any global option with the 'merge-pathlist' or 'merge-associative' flag, set its
  222. // value in the specified context. The option is 'merged' because we
  223. // load $options with the value from the context prior to including the
  224. // configuration file. If the configuration file sets $option['special'][] = 'value',
  225. // then the configuration will be merged. $option['special'] = array(...), on the
  226. // other hand, will replace rather than merge the values together.
  227. if ((array_key_exists($name, $options)) && (array_key_exists('merge', $info) || (array_key_exists('merge-pathlist', $info) || array_key_exists('merge-associative', $info)))) {
  228. $context = array_key_exists('context', $info) ? $info['context'] : $name;
  229. $cache =& drush_get_context($context);
  230. $value = $options[$name];
  231. if (!is_array($value) && array_key_exists('merge-pathlist', $info)) {
  232. $value = explode(PATH_SEPARATOR, $value);
  233. }
  234. if (array_key_exists('merge-associative', $info)) {
  235. foreach ($value as $subkey => $subvalue) {
  236. $cache[$subkey] = array_merge(isset($cache[$subkey]) ? $cache[$subkey] : array(), $subvalue);
  237. }
  238. }
  239. else {
  240. $cache = array_unique(array_merge($cache, $value));
  241. }
  242. // Once we have moved the option to its special context, we
  243. // can remove it from its option context -- unless 'propagate-cli-value'
  244. // is set, in which case we need to let it stick around in options
  245. // in case it is needed in backend invoke.
  246. if (!array_key_exists('propagate-cli-value', $info)) {
  247. unset($options[$name]);
  248. }
  249. }
  250. }
  251. }
  252. // If command-specific options were set and if we already have
  253. // a command, then apply the command-specific options immediately.
  254. if ($has_command_specific) {
  255. drush_command_default_options();
  256. }
  257. }
  258. }
  259. /**
  260. * Set a specific context.
  261. *
  262. * @param context
  263. * Any of the default defined contexts.
  264. * @param value
  265. * The value to store in the context
  266. *
  267. * @return
  268. * An associative array of the settings specified in the request context.
  269. */
  270. function drush_set_context($context, $value) {
  271. $cache =& drush_get_context($context);
  272. $cache = $value;
  273. return $value;
  274. }
  275. /**
  276. * Return a specific context, or the whole context cache
  277. *
  278. * This function provides a storage mechanism for any information
  279. * the currently running process might need to communicate.
  280. *
  281. * This avoids the use of globals, and constants.
  282. *
  283. * Functions that operate on the context cache, can retrieve a reference
  284. * to the context cache using :
  285. * $cache = &drush_get_context($context);
  286. *
  287. * This is a private function, because it is meant as an internal
  288. * generalized API for writing static cache functions, not as a general
  289. * purpose function to be used inside commands.
  290. *
  291. * Code that modifies the reference directly might have unexpected consequences,
  292. * such as modifying the arguments after they have already been parsed and dispatched
  293. * to the callbacks.
  294. *
  295. * @param context
  296. * Optional. Any of the default defined contexts.
  297. *
  298. * @return
  299. * If context is not supplied, the entire context cache will be returned.
  300. * Otherwise only the requested context will be returned.
  301. * If the context does not exist yet, it will be initialized to an empty array.
  302. */
  303. function &drush_get_context($context = NULL, $default = NULL) {
  304. static $cache = array();
  305. if (isset($context)) {
  306. if (!isset($cache[$context])) {
  307. $default = !isset($default) ? array() : $default;
  308. $cache[$context] = $default;
  309. }
  310. return $cache[$context];
  311. }
  312. return $cache;
  313. }
  314. /**
  315. * Set the arguments passed to the drush.php script.
  316. *
  317. * This function will set the 'arguments' context of the current running script.
  318. *
  319. * When initially called by drush_parse_args, the entire list of arguments will
  320. * be populated. Once the command is dispatched, this will be set to only the remaining
  321. * arguments to the command (i.e. the command name is removed).
  322. *
  323. * @param arguments
  324. * Command line arguments, as an array.
  325. */
  326. function drush_set_arguments($arguments) {
  327. drush_set_context('arguments', $arguments);
  328. }
  329. /**
  330. * Gets the command line arguments passed to Drush.
  331. *
  332. * @return array
  333. * An indexed array of arguments. Until Drush has dispatched the command, the
  334. * array will include the command name as the first element. After that point
  335. * the array will not include the command name.
  336. *
  337. * @see drush_set_arguments()
  338. */
  339. function drush_get_arguments() {
  340. return drush_get_context('arguments');
  341. }
  342. /**
  343. * Set the command being executed.
  344. *
  345. * Drush_dispatch will set the correct command based on it's
  346. * matching of the script arguments retrieved from drush_get_arguments
  347. * to the implemented commands specified by drush_get_commands.
  348. *
  349. * @param
  350. * A numerically indexed array of command components.
  351. */
  352. function drush_set_command($command) {
  353. drush_set_context('command', $command);
  354. }
  355. /**
  356. * Return the command being executed.
  357. */
  358. function drush_get_command() {
  359. return drush_get_context('command');
  360. }
  361. /**
  362. * Get the value for an option.
  363. *
  364. * If the first argument is an array, then it checks whether one of the options
  365. * exists and return the value of the first one found. Useful for allowing both
  366. * -h and --host-name
  367. *
  368. * @param option
  369. * The name of the option to get
  370. * @param default
  371. * Optional. The value to return if the option has not been set
  372. * @param context
  373. * Optional. The context to check for the option. If this is set, only this context will be searched.
  374. */
  375. function drush_get_option($option, $default = NULL, $context = NULL) {
  376. $value = NULL;
  377. if ($context) {
  378. // We have a definite context to check for the presence of an option.
  379. $value = _drush_get_option($option, drush_get_context($context));
  380. }
  381. else {
  382. // We are not checking a specific context, so check them in a predefined order of precedence.
  383. $contexts = drush_context_names();
  384. foreach ($contexts as $context) {
  385. $value = _drush_get_option($option, drush_get_context($context));
  386. if ($value !== NULL) {
  387. return $value;
  388. }
  389. }
  390. }
  391. if ($value !== NULL) {
  392. return $value;
  393. }
  394. return $default;
  395. }
  396. /**
  397. * Get the value for an option and return it as a list. If the
  398. * option in question is passed on the command line, its value should
  399. * be a comma-separated list (e.g. --flag=1,2,3). If the option
  400. * was set in a drushrc.php file, then its value may be either a
  401. * comma-separated list or an array of values (e.g. $option['flag'] = array('1', '2', '3')).
  402. *
  403. * @param option
  404. * The name of the option to get
  405. * @param default
  406. * Optional. The value to return if the option has not been set
  407. * @param context
  408. * Optional. The context to check for the option. If this is set, only this context will be searched.
  409. */
  410. function drush_get_option_list($option, $default = array(), $context = NULL) {
  411. $result = drush_get_option($option, $default, $context);
  412. if (!is_array($result)) {
  413. $result = array_map('trim', array_filter(explode(',', $result)));
  414. }
  415. return $result;
  416. }
  417. /**
  418. * Get the value for an option, but first checks the provided option overrides.
  419. *
  420. * The feature of drush_get_option that allows a list of option names
  421. * to be passed in an array is NOT supported.
  422. *
  423. * @param option_overrides
  424. * An array to check for values before calling drush_get_option.
  425. * @param option
  426. * The name of the option to get.
  427. * @param default
  428. * Optional. The value to return if the option has not been set.
  429. * @param context
  430. * Optional. The context to check for the option. If this is set, only this context will be searched.
  431. *
  432. */
  433. function drush_get_option_override($option_overrides, $option, $default = NULL, $context = NULL) {
  434. return drush_sitealias_get_option($option_overrides, $option, $default, '', $context);
  435. }
  436. /**
  437. * Get an option out of the specified alias. If it has not been
  438. * set in the alias, then get it via drush_get_option.
  439. *
  440. * @param site_alias_record
  441. * An array of options for an alias record.
  442. * @param option
  443. * The name of the option to get.
  444. * @param default
  445. * Optional. The value to return if the option does not exist in the site record and has not been set in a context.
  446. * @param context
  447. * Optional. The context to check for the option. If this is set, only this context will be searched.
  448. */
  449. function drush_sitealias_get_option($site_alias_record, $option, $default = NULL, $prefix = '', $context = NULL) {
  450. if (is_array($site_alias_record) && array_key_exists($option, $site_alias_record)) {
  451. return $site_alias_record[$option];
  452. }
  453. else {
  454. return drush_get_option($prefix . $option, $default, $context);
  455. }
  456. }
  457. /**
  458. * Get all of the values for an option in every context.
  459. *
  460. * @param option
  461. * The name of the option to get
  462. * @return
  463. * An array whose key is the context name and value is
  464. * the specific value for the option in that context.
  465. */
  466. function drush_get_context_options($option, $flatten = FALSE) {
  467. $result = array();
  468. $contexts = drush_context_names();
  469. foreach ($contexts as $context) {
  470. $value = _drush_get_option($option, drush_get_context($context));
  471. if ($value !== NULL) {
  472. if ($flatten && is_array($value)) {
  473. $result = array_merge($value, $result);
  474. }
  475. else {
  476. $result[$context] = $value;
  477. }
  478. }
  479. }
  480. return $result;
  481. }
  482. /**
  483. * Retrieves a collapsed list of all options.
  484. */
  485. function drush_get_merged_options() {
  486. $contexts = drush_context_names();
  487. $cache = drush_get_context();
  488. $result = array();
  489. foreach (array_reverse($contexts) as $context) {
  490. if (array_key_exists($context, $cache)) {
  491. $result = array_merge($result, $cache[$context]);
  492. }
  493. }
  494. return $result;
  495. }
  496. /**
  497. * Retrieves a collapsed list of all options
  498. * with a specified prefix.
  499. */
  500. function drush_get_merged_prefixed_options($prefix) {
  501. $merged_options = drush_get_merged_options();
  502. $result = array();
  503. foreach ($merged_options as $key => $value) {
  504. if ($prefix == substr($key, 0, strlen($prefix))) {
  505. $result[substr($key, strlen($prefix))] = $value;
  506. }
  507. }
  508. return $result;
  509. }
  510. /**
  511. * Helper function to recurse through possible option names
  512. */
  513. function _drush_get_option($option, $context) {
  514. if (is_array($option)) {
  515. foreach ($option as $current) {
  516. $current_value = _drush_get_option($current, $context);
  517. if (isset($current_value)) {
  518. return $current_value;
  519. }
  520. }
  521. }
  522. elseif (array_key_exists('no-' . $option, $context)) {
  523. return FALSE;
  524. }
  525. elseif (array_key_exists($option, $context)) {
  526. return $context[$option];
  527. }
  528. return NULL;
  529. }
  530. /**
  531. * Set an option in one of the option contexts.
  532. *
  533. * @param option
  534. * The option to set.
  535. * @param value
  536. * The value to set it to.
  537. * @param context
  538. * Optional. Which context to set it in.
  539. * @return
  540. * The value parameter. This allows for neater code such as
  541. * $myvalue = drush_set_option('http_host', $_SERVER['HTTP_HOST']);
  542. * Without having to constantly type out the value parameter.
  543. */
  544. function drush_set_option($option, $value, $context = 'process') {
  545. $cache =& drush_get_context($context);
  546. $cache[$option] = $value;
  547. return $value;
  548. }
  549. /**
  550. * A small helper function to set the value in the default context
  551. */
  552. function drush_set_default($option, $value) {
  553. return drush_set_option($option, $value, 'default');
  554. }
  555. /**
  556. * Remove a setting from a specific context.
  557. *
  558. * @param
  559. * Option to be unset
  560. * @param
  561. * Context in which to unset the value in.
  562. */
  563. function drush_unset_option($option, $context = NULL) {
  564. if ($context != NULL) {
  565. $cache =& drush_get_context($context);
  566. if (array_key_exists($option, $cache)) {
  567. unset($cache[$option]);
  568. }
  569. }
  570. else {
  571. $contexts = drush_context_names();
  572. foreach ($contexts as $context) {
  573. drush_unset_option($option, $context);
  574. }
  575. }
  576. }
  577. /**
  578. * Save the settings in a specific context to the applicable configuration file
  579. * This is useful is you want certain settings to be available automatically the next time a command is executed.
  580. *
  581. * @param $context
  582. * The context to save
  583. */
  584. function drush_save_config($context) {
  585. $filename = _drush_config_file($context);
  586. if (is_array($filename)) {
  587. $filename = $filename[0];
  588. }
  589. if ($filename) {
  590. $cache = drush_get_context($context);
  591. $fp = fopen($filename, "w+");
  592. if (!$fp) {
  593. return drush_set_error('DRUSH_PERM_ERROR', dt('Drushrc (!filename) could not be written', array('!filename' => $filename)));
  594. }
  595. else {
  596. fwrite($fp, "<?php\n");
  597. foreach ($cache as $key => $value) {
  598. $line = "\n\$options['$key'] = ". var_export($value, TRUE) .';';
  599. fwrite($fp, $line);
  600. }
  601. fwrite($fp, "\n");
  602. fclose($fp);
  603. drush_log(dt('Drushrc file (!filename) was written successfully', array('!filename' => $filename)));
  604. return TRUE;
  605. }
  606. }
  607. return FALSE;
  608. }