|
2 | 2 | <?php |
3 | 3 | // Usage: In another bash script, do this: |
4 | 4 | // eval $( bin/db-credentials.sh ) |
5 | | -// This will populate bash variables for DB_HOST, DB_NAME, DB_USER and DB_PASS. |
6 | | -// See db-login.sh for a usage pattern that preserves special char escaping for DB_PASS. |
| 5 | +// |
| 6 | +// This will populate bash variables for DB_HOST, DB_DATABASE, DB_LOGIN |
| 7 | +// and DB_PASSWORD. See `db-login` for a usage pattern that preserves |
| 8 | +// special char escaping for DB_PASS. |
| 9 | +// |
| 10 | +// Attempts to obtain the current `::$default` database connection |
| 11 | +// settings from the Cake app's Config/database.php file. There are 3 |
| 12 | +// cases to handle: |
| 13 | +// |
| 14 | +// 1. database.php is just a loader for `Configure::read('Database')` |
| 15 | +// and the ConfigRead plugin is installed. We can obtain the values |
| 16 | +// from a call to `bin/cake config_read.config_read -b Database.default`. |
| 17 | +// 2. database.php is just a loader, but ConfigRead is not available. We |
| 18 | +// have to "fake" the `Configure` and `Cache` classes to be able to |
| 19 | +// include `Config/core.php` directly. |
| 20 | +// 3. database.php uses the "stock" configuration, so we can include |
| 21 | +// `Config/database.php` directly. |
| 22 | +// |
7 | 23 | // Ref: http://stackoverflow.com/questions/16320858 |
8 | 24 |
|
9 | | -$dir = dirname(dirname(__FILE__)); |
10 | | -$dbConfigFile = $dir . '/Config/database.php'; |
11 | | -include $dbConfigFile; |
12 | | -$db = new DATABASE_CONFIG(); |
13 | 25 |
|
14 | | -echo <<<EOD |
| 26 | +// Try using ConfigReadShell first, if it's available. |
| 27 | +try { |
| 28 | + $db = new DbConfig(); |
| 29 | + printDbConfig($db); |
| 30 | + exit(0); |
| 31 | +} catch (Exception $e) {} |
| 32 | + |
| 33 | +// Fallback (standard) approach. |
| 34 | +$dbConfigFile = getcwd() . '/Config/database.php'; |
| 35 | +if ((include $dbConfigFile)) { |
| 36 | + $db = new DATABASE_CONFIG(); |
| 37 | + printDbConfig($db); |
| 38 | +} |
| 39 | + |
| 40 | +// Outright failure case. |
| 41 | +else { |
| 42 | + exit(2); |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +//---------- |
| 47 | +function printDbConfig($db) { |
| 48 | + echo <<<EOD |
15 | 49 | DB_HOST="{$db->default['host']}" |
16 | | -DB_NAME="{$db->default['database']}" |
17 | | -DB_USER="{$db->default['login']}" |
18 | | -DB_PASS="{$db->default['password']}" |
| 50 | +DB_DATABASE="{$db->default['database']}" |
| 51 | +DB_LOGIN="{$db->default['login']}" |
| 52 | +DB_PASSWORD="{$db->default['password']}" |
19 | 53 | EOD; |
| 54 | +} |
| 55 | + |
| 56 | +//---------- |
| 57 | +class DbConfig { |
| 58 | + public $default = array(); |
| 59 | + public function __construct() { |
| 60 | + $dir = getcwd(); |
| 61 | + $cmd = escapeshellcmd("{$dir}/bin/cake config_read.config_read -b Database.default"); |
| 62 | + $response = array(); |
| 63 | + $code = 0; |
| 64 | + exec($cmd, $response, $code); |
| 65 | + |
| 66 | + if (empty($response) || $code > 0) { |
| 67 | + throw new RuntimeException('Unable to fetch Database config from ConfigReadShell.'); |
| 68 | + } |
| 69 | + |
| 70 | + foreach ($response as $line) { |
| 71 | + if (!substr_count($line, '=')) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + list($key, $val) = explode('=', $line, 2); |
| 75 | + $key = strtolower(str_replace('DATABASE_DEFAULT_', '', $key)); |
| 76 | + $val = substr($val, 1, -1); |
| 77 | + $this->default[$key] = $val; |
| 78 | + } |
| 79 | + |
| 80 | + if (empty($this->default)) { |
| 81 | + throw new RuntimeException('No Database config found in Configure.'); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +//---------- |
| 87 | +class Configure { |
| 88 | + public static $storage = array(); |
| 89 | + public static function write($key, $value) { |
| 90 | + $sub = array(); |
| 91 | + $ptr = &$sub; |
| 92 | + foreach (explode('.', $key) as $part) { |
| 93 | + $ptr[$part] = array(); |
| 94 | + $ptr = &$ptr[$part]; |
| 95 | + } |
| 96 | + $ptr = $value; |
| 97 | + self::$storage = self::merge(self::$storage, $sub); |
| 98 | + } |
| 99 | + public static function read($key) { |
| 100 | + $sub = self::$storage; |
| 101 | + if (substr_count($key, '.')) { |
| 102 | + foreach (explode('.', $key) as $part) { |
| 103 | + $sub = $sub[$part]; |
| 104 | + } |
| 105 | + } |
| 106 | + return $sub; |
| 107 | + } |
| 108 | + public static function load($file) { |
| 109 | + if (!(include "Config/{$file}.php")) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + self::$storage = self::merge(self::$storage, $config); |
| 113 | + } |
| 114 | + //Ref: http://api.cakephp.org/2.5/source-class-Hash.html#661-700 |
| 115 | + public static function merge(array $data, $merge) { |
| 116 | + $args = array_slice(func_get_args(), 1); |
| 117 | + $return = $data; |
| 118 | + |
| 119 | + foreach ($args as &$curArg) { |
| 120 | + $stack[] = array((array)$curArg, &$return); |
| 121 | + } |
| 122 | + unset($curArg); |
| 123 | + |
| 124 | + while (!empty($stack)) { |
| 125 | + foreach ($stack as $curKey => &$curMerge) { |
| 126 | + foreach ($curMerge[0] as $key => &$val) { |
| 127 | + if (!empty($curMerge[1][$key]) && (array)$curMerge[1][$key] === $curMerge[1][$key] && (array)$val === $val) { |
| 128 | + $stack[] = array(&$val, &$curMerge[1][$key]); |
| 129 | + } elseif ((int)$key === $key && isset($curMerge[1][$key])) { |
| 130 | + $curMerge[1][] = $val; |
| 131 | + } else { |
| 132 | + $curMerge[1][$key] = $val; |
| 133 | + } |
| 134 | + } |
| 135 | + unset($stack[$curKey]); |
| 136 | + } |
| 137 | + unset($curMerge); |
| 138 | + } |
| 139 | + return $return; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +//---------- |
| 144 | +class Cache { |
| 145 | + public static function config($key, $value) { |
| 146 | + // Completely ignore this. |
| 147 | + } |
| 148 | +} |
0 commit comments