source: branches/comu-ver2/data/module/log4php/php5/log4php/config/LoggerPropertySetter.php @ 18220

Revision 18220, 5.3 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
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 *
19 * @package log4php
20 * @subpackage config
21 */
22
23/**
24 * @ignore
25 */
26if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27
28require_once(LOG4PHP_DIR . '/LoggerLog.php');
29require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
30
31/**
32 * General purpose Object property setter. Clients repeatedly invokes
33 * {@link setProperty()} in order to invoke setters
34 * on the Object specified in the constructor.
35 * 
36 * Usage:
37 * <code>
38 * $ps = new LoggerPropertySetter($anObject);
39 * $ps->set("name", "Joe");
40 * $ps->set("age", 32);
41 * $ps->set("isMale", true);
42 * </code>
43 * will cause the invocations
44 * <code>
45 * $anObject->setName("Joe");
46 * $anObject->setAge(32);
47 * $anObject->setMale(true)
48 * </code>
49 * if such methods exist.
50 * 
51 * @author  Marco Vassura
52 * @version $Revision: 663201 $
53 * @package log4php
54 * @subpackage config
55 * @since 0.5
56 */
57class LoggerPropertySetter {
58
59    /**
60     * @var object the target object
61     * @access private
62     */
63    var $obj;
64 
65    /**
66     * Create a new LoggerPropertySetter for the specified Object.
67     * This is done in prepartion for invoking {@link setProperty()}
68     * one or more times.
69     * @param object &$obj the object for which to set properties
70     */
71    function LoggerPropertySetter(&$obj)
72    {
73        $this->obj =& $obj;
74    }
75 
76    /**
77     * Set the properties of an object passed as a parameter in one
78     * go. The <code>properties</code> are parsed relative to a
79     * <code>prefix</code>.
80     *
81     * @param object &$obj The object to configure.
82     * @param array $properties An array containing keys and values.
83     * @param string $prefix Only keys having the specified prefix will be set.
84     * @static
85     */
86    public static function setPropertiesByObject(&$obj, $properties, $prefix)
87    {
88        $pSetter = new LoggerPropertySetter($obj);
89        return $pSetter->setProperties($properties, $prefix);
90    }
91 
92
93    /**
94     * Set the properites for the object that match the
95     * <code>prefix</code> passed as parameter.
96     *
97     * @param array $properties An array containing keys and values.
98     * @param string $prefix Only keys having the specified prefix will be set.
99     */
100    function setProperties($properties, $prefix)
101    {
102        LoggerLog::debug("LoggerOptionConverter::setProperties():prefix=[{$prefix}]");
103
104        $len = strlen($prefix);
105        while (list($key,) = each($properties)) {
106            if (strpos($key, $prefix) === 0) {
107                if (strpos($key, '.', ($len + 1)) > 0)
108                    continue;
109                $value = LoggerOptionConverter::findAndSubst($key, $properties);
110                $key = substr($key, $len);
111                if ($key == 'layout' and ($this->obj instanceof LoggerAppender)) {
112                    continue;
113                }
114                $this->setProperty($key, $value);
115            }
116        }
117        $this->activate();
118    }
119   
120    /**
121     * Set a property on this PropertySetter's Object. If successful, this
122     * method will invoke a setter method on the underlying Object. The
123     * setter is the one for the specified property name and the value is
124     * determined partly from the setter argument type and partly from the
125     * value specified in the call to this method.
126     *
127     * <p>If the setter expects a String no conversion is necessary.
128     * If it expects an int, then an attempt is made to convert 'value'
129     * to an int using new Integer(value). If the setter expects a boolean,
130     * the conversion is by new Boolean(value).
131     *
132     * @param string $name    name of the property
133     * @param string $value   String value of the property
134     */
135    function setProperty($name, $value)
136    {
137        LoggerLog::debug("LoggerOptionConverter::setProperty():name=[{$name}]:value=[{$value}]");
138
139        if ($value === null) return;
140       
141        $method = "set" . ucfirst($name);
142       
143        if (!method_exists($this->obj, $method)) {
144            LoggerLog::warn(
145                "LoggerOptionConverter::setProperty() No such setter method for [{$name}] property in " .
146                        get_class($this->obj) . "."
147            );
148        } else {
149            return call_user_func(array(&$this->obj, $method), $value);
150        }
151    }
152 
153    function activate()
154    {
155        LoggerLog::debug("LoggerOptionConverter::activate()");
156   
157        if (method_exists($this->obj, 'activateoptions')) {
158            return call_user_func(array(&$this->obj, 'activateoptions'));
159        } else {
160            LoggerLog::debug("LoggerOptionConverter::activate() Nothing to activate.");
161        }
162    }
163}
Note: See TracBrowser for help on using the repository browser.