ArrayLogger.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Logs to an Array backend.
  11. *
  12. * @package Swift
  13. * @subpackage Transport
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Plugins_Loggers_ArrayLogger implements Swift_Plugins_Logger
  17. {
  18. /**
  19. * The log contents.
  20. *
  21. * @var array
  22. */
  23. private $_log = array();
  24. /**
  25. * Max size of the log.
  26. *
  27. * @var int
  28. */
  29. private $_size = 0;
  30. /**
  31. * Create a new ArrayLogger with a maximum of $size entries.
  32. *
  33. * @var int $size
  34. */
  35. public function __construct($size = 50)
  36. {
  37. $this->_size = $size;
  38. }
  39. /**
  40. * Add a log entry.
  41. *
  42. * @param string $entry
  43. */
  44. public function add($entry)
  45. {
  46. $this->_log[] = $entry;
  47. while (count($this->_log) > $this->_size) {
  48. array_shift($this->_log);
  49. }
  50. }
  51. /**
  52. * Clear the log contents.
  53. */
  54. public function clear()
  55. {
  56. $this->_log = array();
  57. }
  58. /**
  59. * Get this log as a string.
  60. *
  61. * @return string
  62. */
  63. public function dump()
  64. {
  65. return implode(PHP_EOL, $this->_log);
  66. }
  67. }