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

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

#149 ロガークラス作成

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