PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/rope/log/Log.php

https://bitbucket.org/robindrost/rope-components
PHP | 93 lines | 40 code | 11 blank | 42 comment | 4 complexity | bf4d30673eff388b09f647e26782ee56 MD5 | raw file
  1. <?php
  2. namespace Rope;
  3. /**
  4. *
  5. *
  6. * @version 1.0
  7. * @since 1.0
  8. * @author Robin Drost
  9. */
  10. class Log implements LogInterface
  11. {
  12. /**
  13. * The path to the log file.
  14. * @var string
  15. */
  16. protected $log;
  17. /**
  18. * Implements the constructor that will set the given log path.
  19. *
  20. * @param string $log The path to the log file.
  21. */
  22. public function __construct($log)
  23. {
  24. $this->setPath($log);
  25. }
  26. /**
  27. * Write a single message into the given log file.
  28. *
  29. * @param string $message The message to write.
  30. * @param string $seperator A valid seperator
  31. */
  32. public function write($message, $seperator = "\n")
  33. {
  34. file_put_contents($log, $message . $seperator, FILE_APPEND);
  35. }
  36. /**
  37. * Write an array of messages into the log file.
  38. *
  39. * @param array $messages An array of messages to write to the logs.
  40. * @param string $data The default string data that is logged.
  41. * @param string $seperator The line ending seperator.
  42. */
  43. public function writeMultiple(array $messages, &$data = "", $seperator = "\n")
  44. {
  45. foreach ($messages as $message) {
  46. if (is_array($message)) {
  47. $this->writeMultiple($message, $data);
  48. } else {
  49. $data = $data . $message . $seperator;
  50. }
  51. }
  52. file_put_contents($this->log, $data, FILE_APPEND|FILE_LOCKEX);
  53. }
  54. /**
  55. * Read the log file into an array and return it.
  56. * @return array
  57. */
  58. public function read()
  59. {
  60. return file(file_get_contents($this->log));
  61. }
  62. /**
  63. * Clear a log file to an empty string.
  64. */
  65. public function clear()
  66. {
  67. file_put_contents($this->log, "");
  68. }
  69. /**
  70. * Check if the given log is writeable.
  71. *
  72. * @param string $log
  73. * @return boolean
  74. */
  75. protected function setPath($log)
  76. {
  77. if (is_file($log) && ! is_writable($log)) {
  78. throw new InvalidArgumentException("The file {$log} is not writeable.");
  79. }
  80. $this->log = $log;
  81. }
  82. }