Skip to content

Commit bbf4bd0

Browse files
committed
Merge pull request #33 from loadsys/b/misc-fixes
B/misc fixes
2 parents ca230f4 + f467d4f commit bbf4bd0

4 files changed

Lines changed: 183 additions & 28 deletions

File tree

db-backup

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ ${script}
1212
default database into a ZIP file located in a backups/ folder
1313
created in the project root (if necessary). Outputs the ZIP
1414
file's size as a crude verification step. Should be run from
15-
the project root folder.
15+
the project root folder. Will exit with an error code of 1 if
16+
database credentials could not be obtained from the Cake app
17+
via the `db-credentials` script.
1618
1719
Usage:
1820
bin/${script}
@@ -30,15 +32,12 @@ if (isset($argv[1]) && $argv[1] == '-h') {
3032
// Set up variables.
3133
$dir = getcwd();
3234
$backupDir = $dir . '/backups';
33-
$configDir = $dir . '/Config';
34-
$dbConfigFile = $configDir . '/database.php';
3535
$date = date('Ymd-His');
36-
if (!is_readable($dbConfigFile)) {
37-
echo "!! Failed to read database config file: '{$dbConfigFile}'" . PHP_EOL;
36+
try {
37+
$db = new DbConfig();
38+
} catch (Exception $e) {
3839
exit(1);
3940
}
40-
include $dbConfigFile;
41-
$db = new DATABASE_CONFIG();
4241

4342
// Create an appropriate password clause for the command line.
4443
if (empty($db->default['password'])) {
@@ -70,4 +69,34 @@ function human_filesize($bytes, $decimals = 2) {
7069
$sz = 'BKMGTP';
7170
$factor = floor((strlen(intval($bytes)) - 1) / 3);
7271
return (sprintf("%.{$decimals}f", intval($bytes) / pow(1024, $factor)) + 0) . @$sz[$factor];
73-
}
72+
}
73+
74+
//----------
75+
class DbConfig {
76+
public $default = array();
77+
public function __construct() {
78+
$dir = getcwd();
79+
$cmd = escapeshellcmd("{$dir}/bin/db-credentials");
80+
$response = array();
81+
$code = 0;
82+
exec($cmd, $response, $code);
83+
84+
if (empty($response) || $code > 0) {
85+
throw new RuntimeException('Unable to fetch Database config from db-credentials.');
86+
}
87+
88+
foreach ($response as $line) {
89+
if (!substr_count($line, '=')) {
90+
continue;
91+
}
92+
list($key, $val) = explode('=', $line, 2);
93+
$key = strtolower(str_replace('DB_', '', $key));
94+
$val = substr($val, 1, -1);
95+
$this->default[$key] = $val;
96+
}
97+
98+
if (empty($this->default)) {
99+
throw new RuntimeException('No Database config found.');
100+
}
101+
}
102+
}

db-credentials

Lines changed: 139 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,147 @@
22
<?php
33
// Usage: In another bash script, do this:
44
// 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+
//
723
// Ref: http://stackoverflow.com/questions/16320858
824

9-
$dir = dirname(dirname(__FILE__));
10-
$dbConfigFile = $dir . '/Config/database.php';
11-
include $dbConfigFile;
12-
$db = new DATABASE_CONFIG();
1325

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
1549
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']}"
1953
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+
}

db-login

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ BIN_DIR="$DIR/bin"
2929
# Holy yuck, but nothing else works!
3030
eval $( ${BIN_DIR}/db-credentials )
3131

32-
CMD="mysql --host=${DB_HOST} --database=${DB_NAME} --user=${DB_USER}"
32+
CMD="mysql --host=${DB_HOST} --database=${DB_DATABASE} --user=${DB_LOGIN}"
3333
PATTERN=" |'"
3434

35-
if [[ ${DB_PASS} =~ ${PATTERN} ]]; then
36-
${CMD} -p"${DB_PASS}"
37-
elif [ -n "${DB_PASS}" ]; then
38-
${CMD} -p${DB_PASS}
35+
if [[ ${DB_PASSWORD} =~ ${PATTERN} ]]; then
36+
${CMD} -p"${DB_PASSWORD}"
37+
elif [ -n "${DB_PASSWORD}" ]; then
38+
${CMD} -p${DB_PASSWORD}
3939
else
4040
${CMD}
4141
fi

docs-generate

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ BIN_DIR="${DIR}/bin"
2828
CFG_FILE="$DIR/Config/phpdoc.xml"
2929

3030
# Bail out if phpcs isn't available to us
31-
PHPDOC="$( which phpdoc )"
32-
if [ $? -gt 0 ]; then
33-
PHPDOC="$BIN_DIR/phpdoc"
34-
test -x $PHPDOC
35-
fi
31+
PHPDOC="./bin/phpdoc"
32+
test -x $PHPDOC
3633
if [ $? -gt 0 ]; then
3734
echo "!! The 'phpdoc' command was not found on this system."
3835
echo ""

0 commit comments

Comments
 (0)