Project

General

Profile

1
<?php
2
/**
3
 * Licensed to the Apache Software Foundation (ASF) under one or more
4
 * contributor license agreements. See the NOTICE file distributed with
5
 * this work for additional information regarding copyright ownership.
6
 * The ASF licenses this file to You under the Apache License, Version 2.0
7
 * (the "License"); you may not use this file except in compliance with
8
 * the License. You may obtain a copy of the License at
9
 *
10
 *	   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 * 
18
 * @package log4php
19
 */
20
 
21
/**
22
 * Provides methods for reflective use on php objects
23
 * @package log4php
24
 */
25
class LoggerReflectionUtils {
26
		/** the target object */
27
	private $obj;
28
	
29
	/**
30
	 * Create a new LoggerReflectionUtils for the specified Object. 
31
	 * This is done in prepartion for invoking {@link setProperty()} 
32
	 * one or more times.
33
	 * @param object &$obj the object for which to set properties
34
	 */
35
	public function __construct($obj) {
36
		$this->obj = $obj;
37
	}
38
	
39
	/**
40
	 * Set the properties of an object passed as a parameter in one
41
	 * go. The <code>properties</code> are parsed relative to a
42
	 * <code>prefix</code>.
43
	 *
44
	 * @param object $obj The object to configure.
45
	 * @param array $properties An array containing keys and values.
46
	 * @param string $prefix Only keys having the specified prefix will be set.
47
	 */
48
	 // TODO: check, if this is really useful
49
	public static function setPropertiesByObject($obj, $properties, $prefix) {
50
		$pSetter = new LoggerReflectionUtils($obj);
51
		return $pSetter->setProperties($properties, $prefix);
52
	}
53
	
54
	
55
	/**
56
	 * Set the properites for the object that match the
57
	 * <code>prefix</code> passed as parameter.
58
	 * 
59
	 * Example:
60
	 * 
61
	 * $arr['xxxname'] = 'Joe';
62
 	 * $arr['xxxmale'] = true;
63
	 * and prefix xxx causes setName and setMale.	
64
	 *
65
	 * @param array $properties An array containing keys and values.
66
	 * @param string $prefix Only keys having the specified prefix will be set.
67
	 */
68
	 // TODO: check, if this is really useful
69
	public function setProperties($properties, $prefix) {
70
		$len = strlen($prefix);
71
		reset($properties);
72
		while(list($key,) = each($properties)) {
73
			if(strpos($key, $prefix) === 0) {
74
				if(strpos($key, '.', ($len + 1)) > 0) {
75
					continue;
76
				}
77
				$value = LoggerOptionConverter::findAndSubst($key, $properties);
78
				$key = substr($key, $len);
79
				if($key == 'layout' and ($this->obj instanceof LoggerAppender)) {
80
					continue;
81
				}
82
				$this->setProperty($key, $value);
83
			}
84
		}
85
		$this->activate();
86
	}
87
	
88
	/**
89
	 * Set a property on this PropertySetter's Object. If successful, this
90
	 * method will invoke a setter method on the underlying Object. The
91
	 * setter is the one for the specified property name and the value is
92
	 * determined partly from the setter argument type and partly from the
93
	 * value specified in the call to this method.
94
	 *
95
	 * <p>If the setter expects a String no conversion is necessary.
96
	 * If it expects an int, then an attempt is made to convert 'value'
97
	 * to an int using new Integer(value). If the setter expects a boolean,
98
	 * the conversion is by new Boolean(value).
99
	 *
100
	 * @param string $name	name of the property
101
	 * @param string $value	String value of the property
102
	 */
103
	public function setProperty($name, $value) {
104
		if($value === null) {
105
			return;
106
		}
107
		
108
		$method = "set" . ucfirst($name);
109
		
110
		if(!method_exists($this->obj, $method)) {
111
			throw new Exception("Error setting log4php property $name to $value: no method $method in class ".get_class($this->obj)."!");
112
		} else {
113
			return call_user_func(array($this->obj, $method), $value);
114
		} 
115
	}
116
	
117
	public function activate() {
118
		if(method_exists($this->obj, 'activateoptions')) {
119
			return call_user_func(array($this->obj, 'activateoptions'));
120
		} 
121
	}
122
	
123
	/**
124
	 * Creates an instances from the given class name.
125
	 *
126
	 * @param string $classname
127
	 * @return an object from the class with the given classname
128
	 */
129
	public static function createObject($class) {
130
		if(!empty($class)) {
131
			return new $class();
132
		}
133
		return null;
134
	}
135
	
136
	/**
137
	 * @param object $object
138
	 * @param string $name
139
	 * @param mixed $value
140
	 */
141
	public static function setter($object, $name, $value) {
142
		if (empty($name)) {
143
			return false;
144
		}
145
		$methodName = 'set'.ucfirst($name);
146
		if (method_exists($object, $methodName)) {
147
			return call_user_func(array($object, $methodName), $value);
148
		} else {
149
			return false;
150
		}
151
	}
152
	
153
}
(16-16/19)