scratch – Blame information for rev 115
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Monolog package. |
||
5 | * |
||
6 | * (c) Jordi Boggiano <j.boggiano@seld.be> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Monolog; |
||
13 | |||
14 | use InvalidArgumentException; |
||
15 | |||
16 | /** |
||
17 | * Monolog log registry |
||
18 | * |
||
19 | * Allows to get `Logger` instances in the global scope |
||
20 | * via static method calls on this class. |
||
21 | * |
||
22 | * <code> |
||
23 | * $application = new Monolog\Logger('application'); |
||
24 | * $api = new Monolog\Logger('api'); |
||
25 | * |
||
26 | * Monolog\Registry::addLogger($application); |
||
27 | * Monolog\Registry::addLogger($api); |
||
28 | * |
||
29 | * function testLogger() |
||
30 | * { |
||
31 | * Monolog\Registry::api()->addError('Sent to $api Logger instance'); |
||
32 | * Monolog\Registry::application()->addError('Sent to $application Logger instance'); |
||
33 | * } |
||
34 | * </code> |
||
35 | * |
||
36 | * @author Tomas Tatarko <tomas@tatarko.sk> |
||
37 | */ |
||
38 | class Registry |
||
39 | { |
||
40 | /** |
||
41 | * List of all loggers in the registry (by named indexes) |
||
42 | * |
||
43 | * @var Logger[] |
||
44 | */ |
||
45 | private static $loggers = array(); |
||
46 | |||
47 | /** |
||
48 | * Adds new logging channel to the registry |
||
49 | * |
||
50 | * @param Logger $logger Instance of the logging channel |
||
51 | * @param string|null $name Name of the logging channel ($logger->getName() by default) |
||
52 | * @param bool $overwrite Overwrite instance in the registry if the given name already exists? |
||
53 | * @throws \InvalidArgumentException If $overwrite set to false and named Logger instance already exists |
||
54 | */ |
||
55 | public static function addLogger(Logger $logger, $name = null, $overwrite = false) |
||
56 | { |
||
57 | $name = $name ?: $logger->getName(); |
||
58 | |||
59 | if (isset(self::$loggers[$name]) && !$overwrite) { |
||
60 | throw new InvalidArgumentException('Logger with the given name already exists'); |
||
61 | } |
||
62 | |||
63 | self::$loggers[$name] = $logger; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Checks if such logging channel exists by name or instance |
||
68 | * |
||
69 | * @param string|Logger $logger Name or logger instance |
||
70 | */ |
||
71 | public static function hasLogger($logger) |
||
72 | { |
||
73 | if ($logger instanceof Logger) { |
||
74 | $index = array_search($logger, self::$loggers, true); |
||
75 | |||
76 | return false !== $index; |
||
77 | } else { |
||
78 | return isset(self::$loggers[$logger]); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Removes instance from registry by name or instance |
||
84 | * |
||
85 | * @param string|Logger $logger Name or logger instance |
||
86 | */ |
||
87 | public static function removeLogger($logger) |
||
88 | { |
||
89 | if ($logger instanceof Logger) { |
||
90 | if (false !== ($idx = array_search($logger, self::$loggers, true))) { |
||
91 | unset(self::$loggers[$idx]); |
||
92 | } |
||
93 | } else { |
||
94 | unset(self::$loggers[$logger]); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Clears the registry |
||
100 | */ |
||
101 | public static function clear() |
||
102 | { |
||
103 | self::$loggers = array(); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Gets Logger instance from the registry |
||
108 | * |
||
109 | * @param string $name Name of the requested Logger instance |
||
110 | * @throws \InvalidArgumentException If named Logger instance is not in the registry |
||
111 | * @return Logger Requested instance of Logger |
||
112 | */ |
||
113 | public static function getInstance($name) |
||
114 | { |
||
115 | if (!isset(self::$loggers[$name])) { |
||
116 | throw new InvalidArgumentException(sprintf('Requested "%s" logger instance is not in the registry', $name)); |
||
117 | } |
||
118 | |||
119 | return self::$loggers[$name]; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Gets Logger instance from the registry via static method call |
||
124 | * |
||
125 | * @param string $name Name of the requested Logger instance |
||
126 | * @param array $arguments Arguments passed to static method call |
||
127 | * @throws \InvalidArgumentException If named Logger instance is not in the registry |
||
128 | * @return Logger Requested instance of Logger |
||
129 | */ |
||
130 | public static function __callStatic($name, $arguments) |
||
131 | { |
||
132 | return self::getInstance($name); |
||
133 | } |
||
134 | } |