From 33ca623b1c5b2cdee888aff64e6a0bae86341834 Mon Sep 17 00:00:00 2001 From: seyfer Date: Mon, 29 Dec 2014 16:28:27 +0700 Subject: [PATCH 1/3] Fix naming --- classes/{kohana/minion/log.php => Kohana/Minion/Log.php} | 0 classes/{minion/log.php => Minion/Log.php} | 0 tests/{minion/log.php => Minion/Log.php} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename classes/{kohana/minion/log.php => Kohana/Minion/Log.php} (100%) rename classes/{minion/log.php => Minion/Log.php} (100%) rename tests/{minion/log.php => Minion/Log.php} (100%) diff --git a/classes/kohana/minion/log.php b/classes/Kohana/Minion/Log.php similarity index 100% rename from classes/kohana/minion/log.php rename to classes/Kohana/Minion/Log.php diff --git a/classes/minion/log.php b/classes/Minion/Log.php similarity index 100% rename from classes/minion/log.php rename to classes/Minion/Log.php diff --git a/tests/minion/log.php b/tests/Minion/Log.php similarity index 100% rename from tests/minion/log.php rename to tests/Minion/Log.php From 8d2003de5f80f83fba17e651f5e2b1fdb378438a Mon Sep 17 00:00:00 2001 From: seyfer Date: Mon, 29 Dec 2014 16:29:22 +0700 Subject: [PATCH 2/3] fix readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f6ae9f1..d0b7657 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ * [kohana-minion](https://github.com/kohana-minion/core) is not strictly required, but this was written with minion/command line interfaces in mind. ## Compatibility -* Written for Kohana 3.2. +* Written for Kohana 3.3 and 3.2. ## Usage @@ -56,4 +56,4 @@ That's why this is hosted on github :). Feel free to fork it, submit issues, or ## License -This is licensed under the [same license as Kohana](http://kohanaframework.org/license). \ No newline at end of file +This is licensed under the [same license as Kohana](http://kohanaframework.org/license). From e2319e4183107bcb3778158d1393ec3289c12c65 Mon Sep 17 00:00:00 2001 From: seyfer Date: Fri, 9 Jan 2015 12:24:18 +0700 Subject: [PATCH 3/3] Fix Log::add, fix code style, add composer.json --- classes/Kohana/Minion/Log.php | 223 +++++++++++++------------- composer.json | 30 ++++ tests/Minion/Log.php | 285 +++++++++++++++++----------------- 3 files changed, 284 insertions(+), 254 deletions(-) create mode 100644 composer.json diff --git a/classes/Kohana/Minion/Log.php b/classes/Kohana/Minion/Log.php index 578a38d..9fe770e 100644 --- a/classes/Kohana/Minion/Log.php +++ b/classes/Kohana/Minion/Log.php @@ -1,4 +1,6 @@ -attach($writer); - * - * @param Log_Writer $writer instance - * @param mixed $levels array of messages levels to write OR max level to write - * @param integer $min_level min level to write IF $levels is not an array - * @param boolean $write_on_add Should we write to this log immediately? - * @return Minion_Log - */ - public function attach(Log_Writer $writer, $levels = array(), $min_level = 0, $write_on_add = FALSE) - { - parent::attach($writer, $levels, $min_level); - - if ($write_on_add) - { - $this->_on_add_writers["{$writer}"] = $this->_writers["{$writer}"]; - unset($this->_writers["{$writer}"]); - } - - return $this; - } - - /** - * Detaches a log writer. The same writer object must be used. - * - * $log->detach($writer); - * - * @param Log_Writer $writer instance - * @return Minion_Log - */ - public function detach(Log_Writer $writer) - { - unset($this->_on_add_writers["{$writer}"]); - return parent::detach($writer); - } - - /** - * Adds a message to the log. Replacement values must be passed in to be - * replaced using [strtr](http://php.net/strtr). - * - * $log->add(Log::ERROR, 'Could not locate user: :user', array( - * ':user' => $username, - * )); - * - * @param string $level level of message - * @param string $message message body - * @param array $values values to replace in the message - * @return Minion_Log - */ - public function add($level, $message, array $values = NULL) - { - parent::add($level, $message, $values); - - if ( ! empty($this->_on_add_writers)) - { - // Get the last message - $msg = end($this->_messages); - reset($this->_messages); - - foreach ($this->_on_add_writers as $writer) - { - if (empty($writer['levels']) OR in_array($msg['level'], $writer['levels'])) - { - $writer['object']->write(array($msg)); - } - } - } - - return $this; - } - - /** - * Get the singleton instance of this class and enable writing at shutdown. - * This can be different from the default Kohana logger - * - * $log = Minion_Log::instance(); - * - * @return Minion_Log - */ - public static function instance() - { - if (Minion_Log::$_instance === NULL) - { - // Create a new instance - Minion_Log::$_instance = new Minion_Log; - - // Write the logs at shutdown - register_shutdown_function(array(Minion_Log::$_instance, 'write')); - } - - return Minion_Log::$_instance; - } -} \ No newline at end of file +abstract class Kohana_Minion_Log extends Log +{ + + /** + * @var array Log writers that should write out as soon as soon as messages are added + * @access protected + */ + protected $_on_add_writers = array(); + + /** + * @var mixed Log writers that should write out as soon as soon as messages are added + * @static override instance + * @access protected + */ + protected static $_instance = NULL; + + /** + * Attaches a log writer, and optionally limits the levels of messages that + * will be written by the writer. + * + * $log->attach($writer); + * + * @param Log_Writer $writer instance + * @param mixed $levels array of messages levels to write OR max level to write + * @param integer $min_level min level to write IF $levels is not an array + * @param boolean $write_on_add Should we write to this log immediately? + * @return Minion_Log + */ + public function attach(Log_Writer $writer, $levels = array(), $min_level = 0, $write_on_add = FALSE) + { + parent::attach($writer, $levels, $min_level); + + if ($write_on_add) { + $this->_on_add_writers["{$writer}"] = $this->_writers["{$writer}"]; + unset($this->_writers["{$writer}"]); + } + + return $this; + } + + /** + * Detaches a log writer. The same writer object must be used. + * + * $log->detach($writer); + * + * @param Log_Writer $writer instance + * @return Minion_Log + */ + public function detach(Log_Writer $writer) + { + unset($this->_on_add_writers["{$writer}"]); + return parent::detach($writer); + } + + /** + * Adds a message to the log. Replacement values must be passed in to be + * replaced using [strtr](http://php.net/strtr). + * + * $log->add(Log::ERROR, 'Could not locate user: :user', array( + * ':user' => $username, + * )); + * + * @param string $level level of message + * @param string $message message body + * @param array $values values to replace in the message + * @return Minion_Log + */ + public function add($level, $message, array $values = NULL, array $additional = NULL) + { + parent::add($level, $message, $values, $additional); + + if (!empty($this->_on_add_writers)) { + // Get the last message + $msg = end($this->_messages); + reset($this->_messages); + + foreach ($this->_on_add_writers as $writer) { + if (empty($writer['levels']) OR in_array($msg['level'], $writer['levels'])) { + $writer['object']->write(array($msg)); + } + } + } + + return $this; + } + + /** + * Get the singleton instance of this class and enable writing at shutdown. + * This can be different from the default Kohana logger + * + * $log = Minion_Log::instance(); + * + * @return Minion_Log + */ + public static function instance() + { + if (Minion_Log::$_instance === NULL) { + // Create a new instance + Minion_Log::$_instance = new Minion_Log; + + // Write the logs at shutdown + register_shutdown_function(array(Minion_Log::$_instance, 'write')); + } + + return Minion_Log::$_instance; + } + +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..93540a4 --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "seyfer/kohana-minion-log", + "version": "3.3", + "description": "Kohana module to log while in CLI environment", + "type": "kohana-module", + "authors": [ + { + "name": "seyfer", + "email": "seyferseed@mail.ru", + "homepage": "http://seyferseed.ru", + "role": "Developer" + } + ], + "require": { + "php": ">=5.4", + "composer/installers": "1.*" + }, + "autoload": { + "classmap": [ + "classes/" + ] + }, + "extra": { + "installer-paths": { + "vendor/kohana/modules/{$name}/": [ + "type:kohana-module" + ] + } + } +} diff --git a/tests/Minion/Log.php b/tests/Minion/Log.php index 5ec2e2e..ca67571 100644 --- a/tests/Minion/Log.php +++ b/tests/Minion/Log.php @@ -9,145 +9,146 @@ */ class Minion_LogTest extends Kohana_Unittest_TestCase { - /** - * Can we instantiate the log? - * - * @access public - * @return void - */ - function test_log() - { - $log = new Minion_Log; - - $this->assertInstanceOf('Kohana_Log',$log); - } - - /** - * Make sure we aren't overriding Kohana::log and that - * Make sure Minion_Log::instance() is an instance of Minion_Log (not just Log) - * - * @access public - * @return void - */ - function test_instance() - { - $log = Minion_Log::instance(); - - $this->assertInstanceOf('Minion_Log', $log); - - // assertNotInstanceOf doesn't work here!? - $this->assertFalse(is_a(Kohana::$log, 'Minion_Log')); - } - - /** - * Attach a regular writer and a write-on-add writer - * - * @access public - * @return void - */ - function test_attach_writer() - { - $log = new Minion_Log; - - $writer = $this->getMockForAbstractClass('Log_Writer'); - $writer_expects = array( - spl_object_hash($writer) => array( - 'object' => $writer, - 'levels' => array(Log::NOTICE))); - - $on_add = $this->getMockForAbstractClass('Log_Writer'); - $on_add_expects = array( - spl_object_hash($on_add) => array( - 'object' => $on_add, - 'levels' => array(Log::NOTICE))); - - $this->assertSame($log, $log->attach($writer, array(Log::NOTICE)), "Minion_Log should not change after attaching a writer"); - $this->assertAttributeSame($writer_expects, '_writers', $log, "The writer should be added to the _writers array"); - - $this->assertSame($log, $log->attach($on_add, array(Log::NOTICE), 0, TRUE), "Minion_Log should not change after attaching a write-on-add writer"); - $this->assertAttributeSame($writer_expects, '_writers', $log, "_writers should not be affected when adding a _write_on_add writer"); - $this->assertAttributeSame($on_add_expects, '_on_add_writers', $log, "_write_on_add_writers should be updated after attaching a new writer-on-add writer"); - - } - - /** - * Detach a regular writer and a write-on-add writer - * - * @access public - * @return void - */ - function test_detach_writer() - { - $log = new Minion_Log; - $writer = $this->getMockForAbstractClass('Log_Writer'); - $on_add = $this->getMockForAbstractClass('Log_Writer'); - - $log->attach($writer); - $log->attach($on_add,array(),0,TRUE); - - $this->assertAttributeNotEmpty('_writers', $log, "_writer should be attached"); - $this->assertAttributeNotEmpty('_on_add_writers', $log, "_on_add_writer should be attached"); - - $log->detach($writer); - $this->assertAttributeEmpty('_writers', $log, "_writer should not be attached"); - $this->assertAttributeNotEmpty('_on_add_writers', $log, "_on_add_writer should be attached"); - - $log->detach($on_add); - $this->assertAttributeEmpty('_writers', $log, "_writer should not be attached"); - $this->assertAttributeEmpty('_on_add_writers', $log, "_on_add_writers should not be attached"); - } - - /** - * Test $log->add() - * - * @access public - * @return void - */ - function test_add() - { - $log = new Minion_Log; - $writer = $this->getMockForAbstractClass('Log_Writer'); - $on_add = $this->getMockForAbstractClass('Log_Writer'); - - $log->attach($writer); - $log->attach($on_add,array(),0,TRUE); - - // Should only be called on $log->write() - $writer->expects($this->never()) - ->method('write'); - - // Should only be called on $log->add() - $on_add->expects($this->once()) - ->method('write'); - - $log->add(Log::INFO, "foobar"); - } - - /** - * Test $log->write() - * - * @access public - * @return void - */ - function test_write() - { - $log = new Minion_Log; - $writer = $this->getMockForAbstractClass('Log_Writer'); - $on_add = $this->getMockForAbstractClass('Log_Writer'); - - $log->attach($writer); - $log->attach($on_add,array(),0,TRUE); - - // Should be called on $log->write() - $writer->expects($this->once()) - ->method('write'); - - // Should be called on $log->add() - $on_add->expects($this->once()) - ->method('write'); - - $log->add(Log::INFO, "foobar"); - - $log->write(); - } -} \ No newline at end of file + + /** + * Can we instantiate the log? + * + * @access public + * @return void + */ + function test_log() + { + $log = new Minion_Log; + + $this->assertInstanceOf('Kohana_Log', $log); + } + + /** + * Make sure we aren't overriding Kohana::log and that + * Make sure Minion_Log::instance() is an instance of Minion_Log (not just Log) + * + * @access public + * @return void + */ + function test_instance() + { + $log = Minion_Log::instance(); + + $this->assertInstanceOf('Minion_Log', $log); + + // assertNotInstanceOf doesn't work here!? + $this->assertFalse(is_a(Kohana::$log, 'Minion_Log')); + } + + /** + * Attach a regular writer and a write-on-add writer + * + * @access public + * @return void + */ + function test_attach_writer() + { + $log = new Minion_Log; + + $writer = $this->getMockForAbstractClass('Log_Writer'); + $writer_expects = array( + spl_object_hash($writer) => array( + 'object' => $writer, + 'levels' => array(Log::NOTICE))); + + $on_add = $this->getMockForAbstractClass('Log_Writer'); + $on_add_expects = array( + spl_object_hash($on_add) => array( + 'object' => $on_add, + 'levels' => array(Log::NOTICE))); + + $this->assertSame($log, $log->attach($writer, array(Log::NOTICE)), "Minion_Log should not change after attaching a writer"); + $this->assertAttributeSame($writer_expects, '_writers', $log, "The writer should be added to the _writers array"); + + $this->assertSame($log, $log->attach($on_add, array(Log::NOTICE), 0, TRUE), "Minion_Log should not change after attaching a write-on-add writer"); + $this->assertAttributeSame($writer_expects, '_writers', $log, "_writers should not be affected when adding a _write_on_add writer"); + $this->assertAttributeSame($on_add_expects, '_on_add_writers', $log, "_write_on_add_writers should be updated after attaching a new writer-on-add writer"); + } + + /** + * Detach a regular writer and a write-on-add writer + * + * @access public + * @return void + */ + function test_detach_writer() + { + $log = new Minion_Log; + $writer = $this->getMockForAbstractClass('Log_Writer'); + $on_add = $this->getMockForAbstractClass('Log_Writer'); + + $log->attach($writer); + $log->attach($on_add, array(), 0, TRUE); + + $this->assertAttributeNotEmpty('_writers', $log, "_writer should be attached"); + $this->assertAttributeNotEmpty('_on_add_writers', $log, "_on_add_writer should be attached"); + + $log->detach($writer); + $this->assertAttributeEmpty('_writers', $log, "_writer should not be attached"); + $this->assertAttributeNotEmpty('_on_add_writers', $log, "_on_add_writer should be attached"); + + $log->detach($on_add); + $this->assertAttributeEmpty('_writers', $log, "_writer should not be attached"); + $this->assertAttributeEmpty('_on_add_writers', $log, "_on_add_writers should not be attached"); + } + + /** + * Test $log->add() + * + * @access public + * @return void + */ + function test_add() + { + $log = new Minion_Log; + $writer = $this->getMockForAbstractClass('Log_Writer'); + $on_add = $this->getMockForAbstractClass('Log_Writer'); + + $log->attach($writer); + $log->attach($on_add, array(), 0, TRUE); + + // Should only be called on $log->write() + $writer->expects($this->never()) + ->method('write'); + + // Should only be called on $log->add() + $on_add->expects($this->once()) + ->method('write'); + + $log->add(Log::INFO, "foobar"); + } + + /** + * Test $log->write() + * + * @access public + * @return void + */ + function test_write() + { + $log = new Minion_Log; + $writer = $this->getMockForAbstractClass('Log_Writer'); + $on_add = $this->getMockForAbstractClass('Log_Writer'); + + $log->attach($writer); + $log->attach($on_add, array(), 0, TRUE); + + // Should be called on $log->write() + $writer->expects($this->once()) + ->method('write'); + + // Should be called on $log->add() + $on_add->expects($this->once()) + ->method('write'); + + $log->add(Log::INFO, "foobar"); + + $log->write(); + } + +}