Use a Logger instance for communicating what happens during migrations, and pass
authorChuck Hagenbuch <chuck@horde.org>
Mon, 4 Jan 2010 03:30:09 +0000 (22:30 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Thu, 7 Jan 2010 03:19:54 +0000 (22:19 -0500)
the Logger from Migrator instances to Migration_Base instances.

framework/Db/lib/Horde/Db/Migration/Base.php
framework/Db/lib/Horde/Db/Migration/Migrator.php

index dfc4cc5..05aa57c 100644 (file)
 class Horde_Db_Migration_Base
 {
     /**
-     * Print messages as migrations happen
-     * @var boolean
-     */
-    public $verbose = false;
-
-    /**
      * The migration version
      * @var integer
      */
     public $version = null;
 
     /**
+     * The logger
+     * @var Horde_Log_Logger
+     */
+    protected $_logger;
+
+    /**
      * Database connection adapter
      * @var Horde_Db_Adapter_Base
      */
@@ -48,11 +48,10 @@ class Horde_Db_Migration_Base
 
     /**
      */
-    public function __construct(Horde_Db_Adapter_Base $connection, $version = null, $verbose = false)
+    public function __construct(Horde_Db_Adapter_Base $connection, $version = null)
     {
         $this->_connection = $connection;
         $this->version = $version;
-        $this->verbose = $verbose;
     }
 
 
@@ -123,11 +122,11 @@ class Horde_Db_Migration_Base
 
         if ($direction == 'up') {
             $this->announce("migrated (" . sprintf("%.4fs", $time) . ")");
-            $this->write();
+            $this->log();
         }
         if ($direction == 'down') {
             $this->announce("reverted (" . sprintf("%.4fs", $time) . ")");
-            $this->write();
+            $this->log();
         }
         return $result;
     }
@@ -135,14 +134,22 @@ class Horde_Db_Migration_Base
     /**
      * @param   string  $text
      */
-    public function write($text = '')
+    public function log($text = '')
     {
-        if ($this->verbose) {
-            echo "$text\n";
+        if ($this->_logger) {
+            $this->_logger->info($text);
         }
     }
 
     /**
+     * @param Horde_Log_Logger $logger
+     */
+    public function setLogger($logger)
+    {
+        $this->_logger = $logger;
+    }
+
+    /**
      * Announce migration
      * @param   string  $message
      */
@@ -151,7 +158,7 @@ class Horde_Db_Migration_Base
         $text = "$this->version " . get_class($this) . ": $message";
         $length = 75 - strlen($text) > 0 ? 75 - strlen($text) : 0;
 
-        $this->write(sprintf("== %s %s", $text, str_repeat('=', $length)));
+        $this->log(sprintf("== %s %s", $text, str_repeat('=', $length)));
     }
 
     /**
@@ -160,7 +167,6 @@ class Horde_Db_Migration_Base
      */
     public function say($message, $subitem = false)
     {
-        $this->write(($subitem ? "   ->" : "--") . " $message");
+        $this->log(($subitem ? "   ->" : "--") . " $message");
     }
-
 }
index 8443959..52bf0aa 100644 (file)
@@ -199,7 +199,10 @@ class Horde_Db_Migration_Migrator
     protected function _getMigrationClass($migrationName, $version)
     {
         $className = $this->_inflector->camelize($migrationName);
-        return new $className($this->_connection, $version);
+        $class = new $className($this->_connection, $version);
+        $class->setLogger($this->_logger);
+
+        return $class;
     }
 
     /**