|
| 1 | +<?php |
| 2 | + |
| 3 | +use EE\Model\Site; |
| 4 | +use Mustangostang\Spyc; |
| 5 | +use \Symfony\Component\Filesystem\Filesystem; |
| 6 | + |
| 7 | +/** |
| 8 | + * Manges global EE configuration. |
| 9 | + * |
| 10 | + * ## EXAMPLES |
| 11 | + * |
| 12 | + * # Save value in config |
| 13 | + * $ ee config set le-mail='abc@example.com' admin-email=abcd@example1.com |
| 14 | + * |
| 15 | + * # Get value from config |
| 16 | + * $ ee config get le-mail |
| 17 | + * |
| 18 | + * @package ee-cli |
| 19 | + */ |
| 20 | +class Config_Command extends EE_Command { |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Filesystem $fs Symfony Filesystem object. |
| 24 | + */ |
| 25 | + private $fs; |
| 26 | + |
| 27 | + public function __construct() { |
| 28 | + |
| 29 | + $this->fs = new Filesystem(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Get a config value |
| 34 | + * |
| 35 | + * ## OPTIONS |
| 36 | + * |
| 37 | + * <config-key> |
| 38 | + * : Name of config value to get |
| 39 | + */ |
| 40 | + public function get( $args, $assoc_args ) { |
| 41 | + $config_file_path = getenv( 'EE_CONFIG_PATH' ) ? getenv( 'EE_CONFIG_PATH' ) : EE_CONF_ROOT . '/config.yml'; |
| 42 | + $config = Spyc::YAMLLoad( $config_file_path ); |
| 43 | + |
| 44 | + if ( ! isset( $config[$args[0]] ) ) { |
| 45 | + EE::error("No config value with key '$args[0]' set"); |
| 46 | + } |
| 47 | + |
| 48 | + EE::log( $config[$args[0]] ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get a config value |
| 53 | + * |
| 54 | + * ## OPTIONS |
| 55 | + * |
| 56 | + * <config-key-value>... |
| 57 | + * : Key value pair of config to set |
| 58 | + */ |
| 59 | + public function set( $args, $assoc_args ) { |
| 60 | + $config_file_path = getenv( 'EE_CONFIG_PATH' ) ? getenv( 'EE_CONFIG_PATH' ) : EE_CONF_ROOT . '/config.yml'; |
| 61 | + $config = Spyc::YAMLLoad( $config_file_path ); |
| 62 | + |
| 63 | + foreach ( $args as $arg ) { |
| 64 | + $key_val = explode( '=', $arg, 2 ); |
| 65 | + |
| 66 | + if ( count( $key_val ) < 2 ) { |
| 67 | + EE::warning( "Cannot add $arg in config as it has no corrosponding value" ); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + list( $key, $val ) = $key_val; |
| 72 | + $config[$key] = $value; |
| 73 | + } |
| 74 | + |
| 75 | + $this->fs->dumpFile( $config_file_path, Spyc::YAMLDump( $config, false, false, true ) ); |
| 76 | + } |
| 77 | +} |
0 commit comments