3 * log4php is a PHP port of the log4j java logging package.
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>
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>
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>
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27 require_once(LOG4PHP_DIR . '/or/LoggerDefaultRenderer.php');
28 require_once(LOG4PHP_DIR . '/or/LoggerObjectRenderer.php');
29 require_once(LOG4PHP_DIR . '/LoggerLog.php');
32 * Map class objects to an {@link LoggerObjectRenderer}.
34 * @author VxR <vxr@vxr.it>
35 * @version $Revision: 2 $
40 class LoggerRendererMap {
48 * @var LoggerDefaultRenderer
55 function LoggerRendererMap()
58 $this->defaultRenderer = new LoggerDefaultRenderer();
62 * Add a renderer to a hierarchy passed as parameter.
63 * Note that hierarchy must implement getRendererMap() and setRenderer() methods.
65 * @param LoggerHierarchy &$repository a logger repository.
66 * @param string &$renderedClassName
67 * @param string &$renderingClassName
70 function addRenderer(&$repository, $renderedClassName, $renderingClassName)
72 LoggerLog::debug("LoggerRendererMap::addRenderer() Rendering class: [{$renderingClassName}], Rendered class: [{$renderedClassName}].");
73 $renderer = LoggerObjectRenderer::factory($renderingClassName);
74 if($renderer == null) {
75 LoggerLog::warn("LoggerRendererMap::addRenderer() Could not instantiate renderer [{$renderingClassName}].");
78 $repository->setRenderer($renderedClassName, $renderer);
84 * Find the appropriate renderer for the class type of the
85 * <var>o</var> parameter.
87 * This is accomplished by calling the {@link getByObject()}
88 * method if <var>o</var> is object or using {@link LoggerDefaultRenderer}.
89 * Once a renderer is found, it is applied on the object <var>o</var> and
90 * the result is returned as a string.
95 function findAndRender($o)
101 $renderer = $this->getByObject($o);
102 if ($renderer !== null) {
103 return $renderer->doRender($o);
108 $renderer = $this->defaultRenderer;
109 return $renderer->doRender($o);
115 * Syntactic sugar method that calls {@link PHP_MANUAL#get_class} with the
116 * class of the object parameter.
121 function &getByObject($o)
123 return ($o == null) ? null : $this->getByClassName(get_class($o));
128 * Search the parents of <var>clazz</var> for a renderer.
130 * The renderer closest in the hierarchy will be returned. If no
131 * renderers could be found, then the default renderer is returned.
133 * @param string $class
134 * @return LoggerObjectRenderer
136 function &getByClassName($class)
139 for($c = strtolower($class); !empty($c); $c = get_parent_class($c)) {
140 if (isset($this->map[$c])) {
141 return $this->map[$c];
144 return $this->defaultRenderer;
148 * @return LoggerDefaultRenderer
150 function &getDefaultRenderer()
152 return $this->defaultRenderer;
158 $this->map = array();
162 * Register a {@link LoggerObjectRenderer} for <var>clazz</var>.
163 * @param string $class
164 * @param LoggerObjectRenderer $or
166 function put($class, $or)
168 $this->map[strtolower($class)] = $or;
172 * @param string $class
175 function rendererExists($class)
177 $class = basename($class);
178 if (!class_exists($class)) {
179 @include_once(LOG4PHP_DIR ."/or/{$class}.php");
181 return class_exists($class);