1 | <?php
|
---|
2 | /**
|
---|
3 | * log4php is a PHP port of the log4j java logging package.
|
---|
4 | *
|
---|
5 | * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
|
---|
6 | * <p>Design, strategies and part of the methods documentation are developed by log4j team
|
---|
7 | * (Ceki Gülcü as log4j project founder and
|
---|
8 | * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
|
---|
9 | *
|
---|
10 | * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
|
---|
11 | * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
|
---|
12 | *
|
---|
13 | * <p>This software is published under the terms of the LGPL License
|
---|
14 | * a copy of which has been included with this distribution in the LICENSE file.</p>
|
---|
15 | *
|
---|
16 | * @package log4php
|
---|
17 | * @subpackage config
|
---|
18 | */
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @ignore
|
---|
22 | */
|
---|
23 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
|
---|
24 |
|
---|
25 | require_once(LOG4PHP_DIR . '/LoggerLog.php');
|
---|
26 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * General purpose Object property setter. Clients repeatedly invokes
|
---|
30 | * {@link setProperty()} in order to invoke setters
|
---|
31 | * on the Object specified in the constructor.
|
---|
32 | *
|
---|
33 | * Usage:
|
---|
34 | * <code>
|
---|
35 | * $ps = new LoggerPropertySetter($anObject);
|
---|
36 | * $ps->set("name", "Joe");
|
---|
37 | * $ps->set("age", 32);
|
---|
38 | * $ps->set("isMale", true);
|
---|
39 | * </code>
|
---|
40 | * will cause the invocations
|
---|
41 | * <code>
|
---|
42 | * $anObject->setName("Joe");
|
---|
43 | * $anObject->setAge(32);
|
---|
44 | * $anObject->setMale(true)
|
---|
45 | * </code>
|
---|
46 | * if such methods exist.
|
---|
47 | *
|
---|
48 | * @author VxR <vxr@vxr.it>
|
---|
49 | * @version $Revision: 1.3 $
|
---|
50 | * @package log4php
|
---|
51 | * @subpackage config
|
---|
52 | * @since 0.5
|
---|
53 | */
|
---|
54 | class LoggerPropertySetter {
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * @var object the target object
|
---|
58 | * @access private
|
---|
59 | */
|
---|
60 | var $obj;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Create a new LoggerPropertySetter for the specified Object.
|
---|
64 | * This is done in prepartion for invoking {@link setProperty()}
|
---|
65 | * one or more times.
|
---|
66 | * @param object &$obj the object for which to set properties
|
---|
67 | */
|
---|
68 | function LoggerPropertySetter(&$obj)
|
---|
69 | {
|
---|
70 | $this->obj =& $obj;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Set the properties of an object passed as a parameter in one
|
---|
75 | * go. The <code>properties</code> are parsed relative to a
|
---|
76 | * <code>prefix</code>.
|
---|
77 | *
|
---|
78 | * @param object &$obj The object to configure.
|
---|
79 | * @param array $properties An array containing keys and values.
|
---|
80 | * @param string $prefix Only keys having the specified prefix will be set.
|
---|
81 | * @static
|
---|
82 | */
|
---|
83 | function setPropertiesByObject(&$obj, $properties, $prefix)
|
---|
84 | {
|
---|
85 | $pSetter = new LoggerPropertySetter($obj);
|
---|
86 | return $pSetter->setProperties($properties, $prefix);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Set the properites for the object that match the
|
---|
92 | * <code>prefix</code> passed as parameter.
|
---|
93 | *
|
---|
94 | * @param array $properties An array containing keys and values.
|
---|
95 | * @param string $prefix Only keys having the specified prefix will be set.
|
---|
96 | */
|
---|
97 | function setProperties($properties, $prefix)
|
---|
98 | {
|
---|
99 | LoggerLog::debug("LoggerOptionConverter::setProperties():prefix=[{$prefix}]");
|
---|
100 |
|
---|
101 | $len = strlen($prefix);
|
---|
102 | while (list($key,) = each($properties)) {
|
---|
103 | if (strpos($key, $prefix) === 0) {
|
---|
104 | if (strpos($key, '.', ($len + 1)) > 0)
|
---|
105 | continue;
|
---|
106 | $value = LoggerOptionConverter::findAndSubst($key, $properties);
|
---|
107 | $key = substr($key, $len);
|
---|
108 | if ($key == 'layout' and is_a($this->obj, 'loggerappender')) {
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | $this->setProperty($key, $value);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | $this->activate();
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Set a property on this PropertySetter's Object. If successful, this
|
---|
119 | * method will invoke a setter method on the underlying Object. The
|
---|
120 | * setter is the one for the specified property name and the value is
|
---|
121 | * determined partly from the setter argument type and partly from the
|
---|
122 | * value specified in the call to this method.
|
---|
123 | *
|
---|
124 | * <p>If the setter expects a String no conversion is necessary.
|
---|
125 | * If it expects an int, then an attempt is made to convert 'value'
|
---|
126 | * to an int using new Integer(value). If the setter expects a boolean,
|
---|
127 | * the conversion is by new Boolean(value).
|
---|
128 | *
|
---|
129 | * @param string $name name of the property
|
---|
130 | * @param string $value String value of the property
|
---|
131 | */
|
---|
132 | function setProperty($name, $value)
|
---|
133 | {
|
---|
134 | LoggerLog::debug("LoggerOptionConverter::setProperty():name=[{$name}]:value=[{$value}]");
|
---|
135 |
|
---|
136 | if ($value === null) return;
|
---|
137 |
|
---|
138 | $method = "set" . ucfirst($name);
|
---|
139 |
|
---|
140 | if (!method_exists($this->obj, $method)) {
|
---|
141 | LoggerLog::warn(
|
---|
142 | "LoggerOptionConverter::setProperty() No such setter method for [{$name}] property in " .
|
---|
143 | get_class($this->obj) . "."
|
---|
144 | );
|
---|
145 | } else {
|
---|
146 | return call_user_func(array(&$this->obj, $method), $value);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | function activate()
|
---|
151 | {
|
---|
152 | LoggerLog::debug("LoggerOptionConverter::activate()");
|
---|
153 |
|
---|
154 | if (method_exists($this->obj, 'activateoptions')) {
|
---|
155 | return call_user_func(array(&$this->obj, 'activateoptions'));
|
---|
156 | } else {
|
---|
157 | LoggerLog::debug("LoggerOptionConverter::activate() Nothing to activate.");
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | ?> |
---|