source: branches/comu-ver2/data/module/log4php/php5/log4php/or/LoggerRendererMap.php @ 18220

Revision 18220, 5.1 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 or
21 */
22
23/**
24 * @ignore
25 */
26if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27 
28/**
29 */
30require_once(LOG4PHP_DIR . '/or/LoggerDefaultRenderer.php');
31require_once(LOG4PHP_DIR . '/or/LoggerObjectRenderer.php');
32require_once(LOG4PHP_DIR . '/LoggerLog.php');
33
34/**
35 * Map class objects to an {@link LoggerObjectRenderer}.
36 *
37 * @author  Marco Vassura
38 * @version $Revision: 635069 $
39 * @package log4php
40 * @subpackage or
41 * @since 0.3
42 */
43class LoggerRendererMap {
44
45    /**
46     * @var array
47     */
48    var $map;
49
50    /**
51     * @var LoggerDefaultRenderer
52     */
53    var $defaultRenderer;
54
55    /**
56     * Constructor
57     */
58    public function __construct()
59    {
60        $this->map = array();
61        $this->defaultRenderer = new LoggerDefaultRenderer();
62    }
63
64    /**
65     * Add a renderer to a hierarchy passed as parameter.
66     * Note that hierarchy must implement getRendererMap() and setRenderer() methods.
67     *
68     * @param LoggerHierarchy $repository a logger repository.
69     * @param string $renderedClassName
70     * @param string $renderingClassName
71     * @static
72     */
73    public static function addRenderer($repository, $renderedClassName, $renderingClassName)
74    {
75        LoggerLog::debug("LoggerRendererMap::addRenderer() Rendering class: [{$renderingClassName}], Rendered class: [{$renderedClassName}].");
76        $renderer = LoggerObjectRenderer::factory($renderingClassName);
77        if($renderer == null) {
78            LoggerLog::warn("LoggerRendererMap::addRenderer() Could not instantiate renderer [{$renderingClassName}].");
79            return;
80        } else {
81            $repository->setRenderer($renderedClassName, $renderer);
82        }
83    }
84
85
86    /**
87     * Find the appropriate renderer for the class type of the
88     * <var>o</var> parameter.
89     *
90     * This is accomplished by calling the {@link getByObject()}
91     * method if <var>o</var> is object or using {@link LoggerDefaultRenderer}.
92     * Once a renderer is found, it is applied on the object <var>o</var> and
93     * the result is returned as a string.
94     *
95     * @param mixed $o
96     * @return string
97     */
98    public function findAndRender($o)
99    {
100        if($o == null) {
101            return null;
102        } else {
103            if (is_object($o)) {
104                $renderer = $this->getByObject($o);
105                if ($renderer !== null) {
106                    return $renderer->doRender($o);
107                } else {
108                    return null;
109                }
110            } else {
111                $renderer = $this->defaultRenderer;
112                return $renderer->doRender($o);
113            }
114        }
115    }
116
117    /**
118     * Syntactic sugar method that calls {@link PHP_MANUAL#get_class} with the
119     * class of the object parameter.
120     *
121     * @param mixed $o
122     * @return string
123     */
124    public function getByObject($o)
125    {
126        return ($o == null) ? null : $this->getByClassName(get_class($o));
127    }
128
129
130    /**
131     * Search the parents of <var>clazz</var> for a renderer.
132     *
133     * The renderer closest in the hierarchy will be returned. If no
134     * renderers could be found, then the default renderer is returned.
135     *
136     * @param string $class
137     * @return LoggerObjectRenderer
138     */
139    public function getByClassName($class)
140    {
141        $r = null;
142        for($c = strtolower($class); !empty($c); $c = get_parent_class($c)) {
143            if (isset($this->map[$c])) {
144                return  $this->map[$c];
145            }
146        }
147        return $this->defaultRenderer;
148    }
149
150    /**
151     * @return LoggerDefaultRenderer
152     */
153    public function getDefaultRenderer()
154    {
155        return $this->defaultRenderer;
156    }
157
158
159    public function clear()
160    {
161        $this->map = array();
162    }
163
164    /**
165     * Register a {@link LoggerObjectRenderer} for <var>clazz</var>.
166     * @param string $class
167     * @param LoggerObjectRenderer $or
168     */
169    public function put($class, $or)
170    {
171        $this->map[strtolower($class)] = $or;
172    }
173   
174    /**
175     * @param string $class
176     * @return boolean
177     */
178    public function rendererExists($class)
179    {
180        $class = basename($class);
181        if (!class_exists($class)) {
182            include_once(LOG4PHP_DIR ."/or/{$class}.php");
183        }
184        return class_exists($class);
185    }
186}
Note: See TracBrowser for help on using the repository browser.