]> git.andy128k.dev Git - ipf.git/commitdiff
db console
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 20 Jun 2013 22:10:53 +0000 (01:10 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 20 Jun 2013 22:10:53 +0000 (01:10 +0300)
ipf.php
ipf/cli.php
ipf/command/db.php [new file with mode: 0644]
ipf/orm/manager.php
ipf/shell.php [new file with mode: 0644]

diff --git a/ipf.php b/ipf.php
index 828770956bdb85f2e1f770960399ec02efceae3f..3281b9f5ed9f9d63d6e4a42394b457f53cd9979c 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -11,7 +11,7 @@ final class IPF
 {
     private static $settings = array();
 
-    private static function applySettings(&$settings)
+    private static function applySettings($settings)
     {
         foreach($settings as $key=>$val)
             IPF::$settings[strtolower($key)] = $val;
index 4fa232559f5d58ed13b4ffd65a9496bd1ff02746..37c5f1acbfa5b127bd9e3ef254a5d6757f69f82a 100644 (file)
@@ -10,6 +10,7 @@ class IPF_Cli
             new IPF_Command_BuildModels,
             new IPF_Command_BuildContribModels,
             new IPF_Command_Sql,
+            new IPF_Command_DB,
             new IPF_Command_SyncDB,
             new IPF_Command_Fixtures,
             new IPF_Command_CreateSuperUser,
diff --git a/ipf/command/db.php b/ipf/command/db.php
new file mode 100644 (file)
index 0000000..15d9aa2
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+class IPF_Command_DB
+{
+    public $command = 'db';
+    public $description = 'Database console';
+
+    public function run($args=null)
+    {
+        $db = IPF_ORM_Manager::getInstance()->connectionParameters(IPF::get('database', IPF::get('dsn')));
+
+        if ($db['scheme'] === 'mysql') {
+            IPF_Shell::call('mysql',
+                '-h'.$db['host'],
+                '-u'.$db['username'],
+                '-p'.$db['password'],
+                $db['database']);
+        } else {
+            print 'Do not know how to connect to "'.$db['scheme'].'" database.';
+        }
+    }
+}
+
index f852ac50d38dda445e66ac81720fee27f7e22dad..f28ad393f68bd27f855b9eca53ab19435d0081a0 100644 (file)
@@ -64,26 +64,8 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato
             if (!($adapter instanceof PDO))
                 throw new IPF_ORM_Exception("First argument should be an instance of PDO");
             $driverName = $adapter->getAttribute(PDO::ATTR_DRIVER_NAME);
-        } else if (is_array($adapter)) {
-            if (!count($adapter))
-                throw new IPF_ORM_Exception('Empty data source name given.');
-
-            if (array_key_exists('database', $adapter)) {
-                $adapter['dsn'] = $this->makeDsnForPDO($adapter['driver'], $adapter['host'], @$adapter['port'], $adapter['database']);
-            } else {
-                $adapter = array(
-                    'dsn'       => urldecode($adapter[0]),
-                    'username'  => (isset($adapter[1])) ? urldecode($adapter[1]) : null,
-                    'password'  => (isset($adapter[2])) ? urldecode($adapter[2]) : null,
-                );
-            }
-
-            $e = explode(':', $adapter['dsn']);
-            $driverName = $e[0] !== 'uri' ? $e[0] : 'odbc';
         } else {
-            $adapter = $this->parseDsn($adapter);
-            $adapter['username'] = $adapter['user'];
-            $adapter['password'] = $adapter['pass'];
+            $adapter = $this->connectionParameters($adapter);
             $driverName = $adapter['scheme'];
         }
 
@@ -129,6 +111,31 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato
         return $this->_connections[$name];
     }
     
+    public function connectionParameters($adapter)
+    {
+        if (is_array($adapter)) {
+            if (!count($adapter))
+                throw new IPF_ORM_Exception('Empty data source name given.');
+
+            if (array_key_exists('database', $adapter)) {
+                $adapter['dsn'] = $this->makeDsnForPDO($adapter['driver'], $adapter['host'], @$adapter['port'], $adapter['database']);
+                $adapter['scheme'] = $adapter['driver'];
+                return $adapter;
+            } else {
+                $dsn = urldecode($adapter[0]);
+                $result = $this->parseDsn($dsn);
+                $result['username'] = (isset($adapter[1])) ? urldecode($adapter[1]) : null;
+                $result['password'] = (isset($adapter[2])) ? urldecode($adapter[2]) : null;
+                return $result;
+            }
+        } else {
+            $result = $this->parseDsn($adapter);
+            $result['username'] = $result['user'];
+            $result['password'] = $result['pass'];
+            return $result;
+        }
+    }
+
     public function parsePdoDsn($dsn)
     {
         $parts = array();
diff --git a/ipf/shell.php b/ipf/shell.php
new file mode 100644 (file)
index 0000000..f29db27
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+class IPF_Shell
+{
+    public static function call()
+    {
+        return self::callv(func_get_args());
+    }
+
+    public static function callv($command)
+    {
+        $str = '';
+        foreach ($command as $part) {
+            $str .= escapeshellarg($part) . ' ';
+        }
+        $descriptorspec = array(
+            0 => STDIN,
+            1 => STDOUT,
+            2 => STDERR
+        );
+        $process = proc_open($str, $descriptorspec, $pipes);
+        proc_close($process);
+    }
+}
+