From fc2e81f57417302fcb5e653b90ee7d213af397ec Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sun, 3 Jan 2010 22:30:09 -0500 Subject: [PATCH] Use a Logger instance for communicating what happens during migrations, and pass the Logger from Migrator instances to Migration_Base instances. --- framework/Db/lib/Horde/Db/Migration/Base.php | 38 ++++++++++++++---------- framework/Db/lib/Horde/Db/Migration/Migrator.php | 5 +++- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/framework/Db/lib/Horde/Db/Migration/Base.php b/framework/Db/lib/Horde/Db/Migration/Base.php index dfc4cc574..05aa57c46 100644 --- a/framework/Db/lib/Horde/Db/Migration/Base.php +++ b/framework/Db/lib/Horde/Db/Migration/Base.php @@ -24,18 +24,18 @@ 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"); } - } diff --git a/framework/Db/lib/Horde/Db/Migration/Migrator.php b/framework/Db/lib/Horde/Db/Migration/Migrator.php index 8443959c4..52bf0aa9d 100644 --- a/framework/Db/lib/Horde/Db/Migration/Migrator.php +++ b/framework/Db/lib/Horde/Db/Migration/Migrator.php @@ -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; } /** -- 2.11.0