]> git.andy128k.dev Git - ipf.git/commitdiff
database provider
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 23:05:35 +0000 (00:05 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 23:13:30 +0000 (00:13 +0100)
ipf/bootstrap.php
ipf/database.php
ipf/database/logger.php [new file with mode: 0644]
ipf/database/provider.php [new file with mode: 0644]

index 09ae581af3ec12c9bf08864bfc16330ef5f1b804..de5f6d0d2e629ea420c4c9a9d372dce9998b01c3 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace IPF;
 
+use IPF\Database\DatabaseProvider;
 use IPF\Error\ErrorPageProvider;
 use IPF\Template\TemplateProvider;
 use Pimple\Container;
@@ -42,12 +43,7 @@ class BootstrapProvider implements ServiceProviderInterface
         $container->register(new CoreServicesProvider());
         $container->register(new TemplateProvider());
         $container->register(new ErrorPageProvider());
-
-        $container['db'] = $container->factory(function ($c) {
-            $config = $c['settings']->get('database');
-            return \IPF_Database::connectDBAL($config);
-        });
-
+        $container->register(new DatabaseProvider());
         $container->register(new CliProvider());
 
         $container['pipeline'] = function ($c) {
index cdabcac728783852a7b1fd2aa323528e3e2db6a1..ce509320b224c553bdcdd73e8ad5dd9559683fb2 100644 (file)
@@ -1,27 +1,9 @@
 <?php
 
-use Doctrine\DBAL\Configuration;
 use Doctrine\DBAL\Connection;
-use Doctrine\DBAL\DriverManager;
 
 class IPF_Database
 {
-    public static function connectDBAL($database)
-    {
-        $config = new Configuration();
-
-        $connectionParams = array(
-            'driver' => 'pdo_' . \PFF\Arr::get($database, 'driver', 'mysql'),
-            'host' => \PFF\Arr::get($database, 'host', 'localhost'),
-            'dbname' => \PFF\Arr::get($database, 'database'),
-            'user' => \PFF\Arr::get($database, 'username'),
-            'password' => \PFF\Arr::get($database, 'password'),
-            'charset' => 'utf8',
-        );
-
-        return DriverManager::getConnection($connectionParams, $config);
-    }
-
     public static function queryOneObject(Connection $connection, $className, $sql, $params = [], $types = [])
     {
         $stmt = $connection->executeQuery($sql, $params, $types);
@@ -35,99 +17,3 @@ class IPF_Database
         return $stmt->fetchAll(\PDO::FETCH_CLASS, $className);
     }
 }
-
-
-
-class PDOProfile extends PDO
-{
-    public function __construct($dsn, $username=null, $password=null, $options=null)
-    {
-        parent::__construct($dsn, $username, $password, $options);
-        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOProfileStatement', array($this)));
-
-        $this->colors = array(
-            "\033[1;35m", // magenta
-            "\033[1;36m", // cyan
-        );
-    }
-
-    public function exec($statement)
-    {
-        $start = microtime(true);
-        $result = parent::exec($statement);
-        $exec_time = microtime(true) - $start;
-        $this->report($statement, null, $exec_time, $exec_time);
-        return $result;
-    }
-
-    public function prepare($q, $driver_options=array())
-    {
-        $stmt = parent::prepare($q, $driver_options);
-        $stmt->q = $q;
-        return $stmt;
-    }
-
-    public function query($q)
-    {
-        $stmt = parent::query($q);
-        $stmt->q = $q;
-        return $stmt;
-    }
-
-    private $colors, $color = 0;
-
-    private function getColor()
-    {
-        $color = $this->colors[$this->color];
-        $this->color = ($this->color + 1) % count($this->colors);
-        return $color;
-    }
-
-    public function report($query, $params, $total_time, $exec_time)
-    {
-        $color = $this->getColor();
-        error_log($color.$query."\033[0m");
-        if ($params)
-            error_log($color.trim(print_r($params, 1))."\033[0m");
-        error_log($color.sprintf('Query time: %0.3fms Total time: %0.3fms', $exec_time * 1000, $total_time * 1000)."\033[0m");
-    }
-}
-
-class PDOProfileStatement extends PDOStatement
-{
-    public $q = 'UNKNOWN QUERY', $params = null;
-    private $start, $execute_time;
-
-    protected function __construct($pdo)
-    {
-        $this->pdo = $pdo;
-        $this->start = microtime(true);
-    }
-
-    function bindValue($parameter, $value, $data_type = null)
-    {
-        $success = parent::bindValue($parameter, $value, $data_type);
-        if ($this->params === null) {
-            $this->params = [];
-        }
-        $this->params[$parameter] = $value;
-        return $success;
-    }
-
-    function execute($params=null)
-    {
-        if ($this->params === null) {
-            $this->params = $params;
-        }
-        $start = microtime(true);
-        $result = $params ? parent::execute($params) : parent::execute();
-        $this->execute_time = microtime(true) - $start;
-        return $result;
-    }
-
-    function __destruct()
-    {
-        $total = microtime(true) - $this->start;
-        $this->pdo->report($this->q, $this->params, $total, $this->execute_time);
-    }
-}
diff --git a/ipf/database/logger.php b/ipf/database/logger.php
new file mode 100644 (file)
index 0000000..07494d3
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace IPF\Database;
+
+use Doctrine\DBAL\Logging\SQLLogger;
+
+final class DebugSQLLogger implements SQLLogger
+{
+    private $colors, $color = 0;
+    private $start;
+
+    function __construct()
+    {
+        $this->colors = array(
+            "\033[1;35m", // magenta
+            "\033[1;36m", // cyan
+        );
+    }
+
+    private function log($message)
+    {
+        $color = $this->colors[$this->color];
+        error_log($color . $message . "\033[0m");
+    }
+
+    public function startQuery($sql, array $params = null, array $types = null)
+    {
+        $this->start = microtime(true);
+        $this->log($sql);
+        if ($params)
+            $this->log(trim(print_r($params, 1)));
+    }
+
+    public function stopQuery()
+    {
+        $total = microtime(true) - $this->start;
+        $this->log(sprintf('Query time: %0.3fms', $total * 1000));
+        $this->color = ($this->color + 1) % count($this->colors);
+    }
+}
diff --git a/ipf/database/provider.php b/ipf/database/provider.php
new file mode 100644 (file)
index 0000000..dabdf82
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace IPF\Database;
+
+use Doctrine\DBAL\Configuration;
+use Doctrine\DBAL\DriverManager;
+use Pimple\Container;
+use Pimple\ServiceProviderInterface;
+
+class DatabaseProvider implements ServiceProviderInterface
+{
+    public function register(Container $container)
+    {
+        $container['db_logger'] = function ($c) {
+            if ($c['settings']->get('debug')) {
+                return new DebugSQLLogger();
+            } else {
+                return null;
+            }
+        };
+
+        $container['db_config'] = function ($c) {
+            $database = $c['settings']->get('database');
+            return array(
+                'driver' => 'pdo_' . \PFF\Arr::get($database, 'driver', 'mysql'),
+                'host' => \PFF\Arr::get($database, 'host', 'localhost'),
+                'dbname' => \PFF\Arr::get($database, 'database'),
+                'user' => \PFF\Arr::get($database, 'username'),
+                'password' => \PFF\Arr::get($database, 'password'),
+                'charset' => 'utf8',
+            );
+        };
+
+        $container['db'] = $container->factory(function ($c) {
+            $config = new Configuration();
+            $config->setSQLLogger($c['db_logger']);
+            return DriverManager::getConnection($c['db_config'], $config);
+        });
+    }
+}