Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
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()->error('Sent to $api Logger instance');
32
 *     Monolog\Registry::application()->error('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 = [];
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
     * @return void
55
     */
56
    public static function addLogger(Logger $logger, ?string $name = null, bool $overwrite = false)
57
    {
58
        $name = $name ?: $logger->getName();
59
 
60
        if (isset(self::$loggers[$name]) && !$overwrite) {
61
            throw new InvalidArgumentException('Logger with the given name already exists');
62
        }
63
 
64
        self::$loggers[$name] = $logger;
65
    }
66
 
67
    /**
68
     * Checks if such logging channel exists by name or instance
69
     *
70
     * @param string|Logger $logger Name or logger instance
71
     */
72
    public static function hasLogger($logger): bool
73
    {
74
        if ($logger instanceof Logger) {
75
            $index = array_search($logger, self::$loggers, true);
76
 
77
            return false !== $index;
78
        }
79
 
80
        return isset(self::$loggers[$logger]);
81
    }
82
 
83
    /**
84
     * Removes instance from registry by name or instance
85
     *
86
     * @param string|Logger $logger Name or logger instance
87
     */
88
    public static function removeLogger($logger): void
89
    {
90
        if ($logger instanceof Logger) {
91
            if (false !== ($idx = array_search($logger, self::$loggers, true))) {
92
                unset(self::$loggers[$idx]);
93
            }
94
        } else {
95
            unset(self::$loggers[$logger]);
96
        }
97
    }
98
 
99
    /**
100
     * Clears the registry
101
     */
102
    public static function clear(): void
103
    {
104
        self::$loggers = [];
105
    }
106
 
107
    /**
108
     * Gets Logger instance from the registry
109
     *
110
     * @param  string                    $name Name of the requested Logger instance
111
     * @throws \InvalidArgumentException If named Logger instance is not in the registry
112
     */
113
    public static function getInstance($name): Logger
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  mixed[]                   $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
}