a tiny mvc framework for php using php-activerecord
at v1 93 lines 2.0 kB view raw
1<?php 2/* 3 for the given halfmoon environment, extract its database settings and pass 4 them to the command-line utility used to administrate that database 5*/ 6 7namespace HalfMoon; 8 9class DBConsole { 10 private $args; 11 public $include_password; 12 13 public function __construct($args) { 14 if (!function_exists("pcntl_exec")) 15 die("pcntl extension not installed/loaded. exiting.\n"); 16 17 $this->args = $args; 18 $this->include_password = false; 19 20 for ($x = 1; $x < count($args); $x++) 21 switch ($args[$x]) { 22 case "-h": 23 case "--help": 24 $this->usage(); 25 break; 26 27 case "-p": 28 case "--include-password": 29 $this->include_password = true; 30 break; 31 32 default: 33 if (substr($args[$x], 0, 1) == "-") 34 $this->usage(); 35 elseif (defined("HALFMOON_ENV")) 36 $this->usage(); 37 else 38 define("HALFMOON_ENV", $args[$x]); 39 } 40 41 $this->run_db_utility(); 42 } 43 44 public function usage() { 45 die("usage: " . $this->args[0] . " [-hp] [environment]\n"); 46 } 47 48 public function run_db_utility() { 49 require_once(__DIR__ . "/../halfmoon.php"); 50 51 $db_config = Utils::A(Config::instance()->db_config, HALFMOON_ENV); 52 53 switch ($db_config["adapter"]) { 54 case "mysql": 55 if ($db_config["socket"]) 56 $bin_args = array( 57 "-S", $db_config["socket"], 58 ); 59 else 60 $bin_args = array( 61 "-h", $db_config["hostname"], 62 "-P", $db_config["port"], 63 ); 64 65 array_push($bin_args, "-D"); 66 array_push($bin_args, $db_config["database"]); 67 68 array_push($bin_args, "-u"); 69 array_push($bin_args, $db_config["username"]); 70 71 if ($this->include_password) 72 array_push($bin_args, "--password=" . $db_config["password"]); 73 74 $bin_path = null; 75 foreach (explode(":", getenv("PATH")) as $dir) 76 if (file_exists($dir . "/mysql")) { 77 $bin_path = $dir . "/mysql"; 78 break; 79 } 80 81 if (!$bin_path) 82 die("cannot find mysql in \$PATH\n"); 83 84 pcntl_exec($bin_path, $bin_args); 85 break; 86 87 default: 88 die($db_config["adapter"] . " not yet supported\n"); 89 } 90 } 91} 92 93?>