rope-components /rope/log/Log.php

Language PHP Lines 93
MD5 Hash bf4d30673eff388b09f647e26782ee56 Estimated Cost $872 (why?)
Repository https://bitbucket.org/robindrost/rope-components.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?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;
	}

}
Back to Top