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 : * Pool implmentation for LoggerAppender instances.
23 : *
24 : * The pool is used when configuring log4php. First all appender instances
25 : * are created in the pool. Afterward, they are linked to loggers, each
26 : * appender can be linked to multiple loggers. This makes sure duplicate
27 : * appenders are not created.
28 : *
29 : * @version $Revision: 795727 $
30 : * @package log4php
31 : */
32 : class LoggerAppenderPool {
33 :
34 : /** Holds appenders indexed by their name */
35 : public static $appenders = array();
36 :
37 : /**
38 : * Adds an appender to the pool.
39 : * The appender must be named for this operation.
40 : * @param LoggerAppender $appender
41 : */
42 : public static function add(LoggerAppender $appender)
43 : {
44 2 : $name = $appender->getName();
45 :
46 2 : if(empty($name)) {
47 1 : trigger_error('log4php: Cannot add unnamed appender to pool.', E_USER_WARNING);
48 0 : return;
49 : }
50 :
51 1 : if (isset(self::$appenders[$name])) {
52 1 : trigger_error("log4php: Appender [$name] already exists in pool. Overwriting existing appender.", E_USER_WARNING);
53 0 : }
54 :
55 1 : self::$appenders[$name] = $appender;
56 1 : }
57 :
58 : /**
59 : * Retrieves an appender from the pool by name.
60 : * @param string $name Name of the appender to retrieve.
61 : * @return LoggerAppender The named appender or NULL if no such appender
62 : * exists in the pool.
63 : */
64 : public static function get($name) {
65 0 : return isset(self::$appenders[$name]) ? self::$appenders[$name] : null;
66 : }
67 :
68 : /**
69 : * Removes an appender from the pool by name.
70 : * @param string $name Name of the appender to remove.
71 : */
72 : public static function delete($name) {
73 0 : unset(self::$appenders[$name]);
74 0 : }
75 :
76 : /**
77 : * Returns all appenders from the pool.
78 : * @return array Array of LoggerAppender objects.
79 : */
80 : public static function getAppenders() {
81 0 : return self::$appenders;
82 : }
83 :
84 : /**
85 : * Checks whether an appender exists in the pool.
86 : * @param string $name Name of the appender to look for.
87 : * @return boolean TRUE if the appender with the given name exists.
88 : */
89 : public static function exists($name) {
90 0 : return isset(self::$appenders[$name]);
91 : }
92 :
93 : /**
94 : * Clears all appenders from the pool.
95 : */
96 : public static function clear() {
97 63 : self::$appenders = array();
98 63 : }
|