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 : * Log objects using customized renderers that implement {@link LoggerRendererObject}.
23 : *
24 : * Example:
25 : * {@example ../../examples/php/renderer_map.php 19}<br>
26 : * {@example ../../examples/resources/renderer_map.properties 18}<br>
27 : * <pre>
28 : * DEBUG - Now comes the current MyClass object:
29 : * DEBUG - Doe, John
30 : * </pre>
31 : *
32 : * @version $Revision: 1166187 $
33 : * @package log4php
34 : * @subpackage renderers
35 : * @since 0.3
36 : */
37 : class LoggerRendererMap {
38 :
39 : /**
40 : * @var array
41 : */
42 : private $map;
43 :
44 : /**
45 : * @var LoggerDefaultRenderer
46 : */
47 : private $defaultRenderer;
48 :
49 : /**
50 : * Constructor
51 : */
52 : public function __construct() {
53 10 : $this->map = array();
54 10 : $this->defaultRenderer = new LoggerRendererDefault();
55 10 : }
56 :
57 : /**
58 : * Add a renderer to a hierarchy passed as parameter.
59 : * Note that hierarchy must implement getRendererMap() and setRenderer() methods.
60 : *
61 : * @param LoggerHierarchy $repository a logger repository.
62 : * @param string $renderedClassName
63 : * @param string $renderingClassName
64 : */
65 : public function addRenderer($renderedClassName, $renderingClassName) {
66 6 : $renderer = LoggerReflectionUtils::createObject($renderingClassName);
67 6 : if($renderer == null) {
68 0 : return;
69 : } else {
70 6 : $this->put($renderedClassName, $renderer);
71 : }
72 6 : }
73 :
74 :
75 : /**
76 : * Find the appropriate renderer for the class type of the
77 : * <var>o</var> parameter.
78 : *
79 : * This is accomplished by calling the {@link getByObject()}
80 : * method if <var>o</var> is object or using {@link LoggerRendererDefault}.
81 : * Once a renderer is found, it is applied on the object <var>o</var> and
82 : * the result is returned as a string.
83 : *
84 : * @param mixed $o
85 : * @return string
86 : */
87 : public function findAndRender($o) {
88 3 : if($o == null) {
89 0 : return null;
90 : } else {
91 3 : if(is_object($o)) {
92 3 : $renderer = $this->getByObject($o);
93 3 : if($renderer !== null) {
94 3 : return $renderer->render($o);
95 : } else {
96 0 : return null;
97 : }
98 : } else {
99 0 : $renderer = $this->defaultRenderer;
100 0 : return $renderer->render($o);
101 : }
102 : }
103 : }
104 :
105 : /**
106 : * Syntactic sugar method that calls {@link PHP_MANUAL#get_class} with the
107 : * class of the object parameter.
108 : *
109 : * @param mixed $o
110 : * @return string
111 : */
112 : public function getByObject($o) {
113 4 : return ($o == null) ? null : $this->getByClassName(get_class($o));
114 : }
115 :
116 :
117 : /**
118 : * Search the parents of <var>clazz</var> for a renderer.
119 : *
120 : * The renderer closest in the hierarchy will be returned. If no
121 : * renderers could be found, then the default renderer is returned.
122 : *
123 : * @param string $class
124 : * @return LoggerRendererObject
125 : */
126 : public function getByClassName($class) {
127 8 : $r = null;
128 8 : for($c = $class; !empty($c); $c = get_parent_class($c)) {
129 8 : $c = strtolower($c);
130 8 : if(isset($this->map[$c])) {
131 5 : return $this->map[$c];
132 : }
133 4 : }
134 3 : return $this->defaultRenderer;
135 : }
136 :
137 : public function clear() {
138 63 : $this->map = array();
139 63 : }
140 :
141 : /**
142 : * Register a {@link LoggerRendererObject} for <var>clazz</var>.
143 : * @param string $class
144 : * @param LoggerRendererObject $or
145 : */
146 : private function put($class, $or) {
147 6 : $this->map[strtolower($class)] = $or;
148 6 : }
149 : }
|