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