/rope/log/Log.php
PHP | 93 lines | 40 code | 11 blank | 42 comment | 4 complexity | bf4d30673eff388b09f647e26782ee56 MD5 | raw file
- <?php
- namespace Rope;
- /**
- *
- *
- * @version 1.0
- * @since 1.0
- * @author Robin Drost
- */
- class Log implements LogInterface
- {
- /**
- * The path to the log file.
- * @var string
- */
- protected $log;
- /**
- * Implements the constructor that will set the given log path.
- *
- * @param string $log The path to the log file.
- */
- public function __construct($log)
- {
- $this->setPath($log);
- }
- /**
- * Write a single message into the given log file.
- *
- * @param string $message The message to write.
- * @param string $seperator A valid seperator
- */
- public function write($message, $seperator = "\n")
- {
- file_put_contents($log, $message . $seperator, FILE_APPEND);
- }
- /**
- * Write an array of messages into the log file.
- *
- * @param array $messages An array of messages to write to the logs.
- * @param string $data The default string data that is logged.
- * @param string $seperator The line ending seperator.
- */
- public function writeMultiple(array $messages, &$data = "", $seperator = "\n")
- {
- foreach ($messages as $message) {
- if (is_array($message)) {
- $this->writeMultiple($message, $data);
- } else {
- $data = $data . $message . $seperator;
- }
- }
- file_put_contents($this->log, $data, FILE_APPEND|FILE_LOCKEX);
- }
- /**
- * Read the log file into an array and return it.
- * @return array
- */
- public function read()
- {
- return file(file_get_contents($this->log));
- }
- /**
- * Clear a log file to an empty string.
- */
- public function clear()
- {
- file_put_contents($this->log, "");
- }
- /**
- * Check if the given log is writeable.
- *
- * @param string $log
- * @return boolean
- */
- protected function setPath($log)
- {
- if (is_file($log) && ! is_writable($log)) {
- throw new InvalidArgumentException("The file {$log} is not writeable.");
- }
- $this->log = $log;
- }
- }