1: <?php
2:
3: /**
4: * This file contains the abstract log writer class.
5: *
6: * @package Core
7: * @subpackage Log
8: * @author Dominik Ziegler
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * This class contains the main functionalities for the logging writer
19: * in CONTENIDO.
20: *
21: * @package Core
22: * @subpackage Log
23: */
24: abstract class cLogWriter {
25:
26: /**
27: * Contains all options of the current writer instance.
28: *
29: * @var array
30: */
31: protected $_options = array();
32:
33: /**
34: * Constructor to create an instance of this class.
35: *
36: * @param array $options [optional]
37: * Array with options for the writer instance (optional)
38: */
39: public function __construct($options = array()) {
40: $this->setOptions($options);
41:
42: // Set all default options if they were not set already
43: $this->setOption('default_priority', cLog::INFO, false);
44: $this->setOption('line_ending', PHP_EOL, false);
45: }
46:
47: /**
48: * Factory method for a new writer instance.
49: *
50: * @param string $writerName
51: * Name of the writer
52: * @param array $writerOptions
53: * Options array for the writer instance
54: * @throws cInvalidArgumentException
55: * if the writer class with the given name does not exist
56: * or is not an instance of clogWriter
57: * @return cLogWriter
58: * Log writer instance
59: */
60: public static function factory($writerName, array $writerOptions) {
61: $logWriterClassName = 'cLogWriter' . ucfirst($writerName);
62: if (!class_exists($logWriterClassName)) {
63: throw new cInvalidArgumentException('Unknown writer class: ' . $writerName);
64: }
65:
66: $writer = new $logWriterClassName($writerOptions);
67: if (($writer instanceof cLogWriter) == false) {
68: throw new cInvalidArgumentException('Provided class is not an instance of cLogWriter');
69: }
70:
71: return $writer;
72: }
73:
74: /**
75: * Sets the whole options array.
76: *
77: * @param array $options
78: * Array with options
79: */
80: public function setOptions(array $options) {
81: $this->_options = $options;
82: }
83:
84: /**
85: * Returns an array with all options.
86: *
87: * @return array
88: * Array with all options
89: */
90: public function getOptions() {
91: return $this->_options;
92: }
93:
94: /**
95: * Sets an option. If option was set previously, it must be forced
96: * to overwrite the value.
97: *
98: * @param string $option
99: * Name of the option
100: * @param mixed $value
101: * Value of the option
102: * @param bool $force [optional]
103: * Flag to force setting the option value (optional, default: false)
104: */
105: public function setOption($option, $value, $force = false) {
106: if ($force == false && isset($this->_options[$option]) == true) {
107: return;
108: }
109:
110: $this->_options[$option] = $value;
111: }
112:
113: /**
114: * Returns the value of an option entry.
115: *
116: * @param string $option
117: * Name of the option
118: * @return mixed
119: * Value of the option entry
120: */
121: public function getOption($option) {
122: return $this->_options[$option];
123: }
124:
125: /**
126: * Removes an option entry.
127: *
128: * @param string $option
129: * Name of the option
130: */
131: public function removeOption($option) {
132: unset($this->_options[$option]);
133: }
134:
135: /**
136: * Abstract function for the write process.
137: * This method must be implemented in the specific writer.
138: *
139: * @param string $message
140: * Message to write
141: * @param int $priority
142: * Priority of the log entry
143: * @return bool
144: * State of the write process
145: */
146: abstract function write($message, $priority);
147: }
148: