Add a hook run after EditPage::attemptSave for WikiEditor
[lhc/web/wiklou.git] / includes / debug / logger / Factory.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21
22 /**
23 * PSR-3 logger instance factory.
24 *
25 * Creation of \Psr\Log\LoggerInterface instances is managed via the
26 * MWLoggerFactory::getInstance() static method which in turn delegates to the
27 * currently registered service provider.
28 *
29 * A service provider is any class implementing the MWLoggerSpi interface.
30 * There are two possible methods of registering a service provider. The
31 * MWLoggerFactory::registerProvider() static method can be called at any time
32 * to change the service provider. If MWLoggerFactory::getInstance() is called
33 * before any service provider has been registered, it will attempt to use the
34 * $wgMWLoggerDefaultSpi global to bootstrap MWLoggerSpi registration.
35 * $wgMWLoggerDefaultSpi is expected to be an array usable by
36 * ObjectFactory::getObjectFromSpec() to create a class.
37 *
38 * @see MWLoggerSpi
39 * @since 1.25
40 * @author Bryan Davis <bd808@wikimedia.org>
41 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
42 */
43 class MWLoggerFactory {
44
45 /**
46 * Service provider.
47 * @var MWLoggerSpi $spi
48 */
49 private static $spi;
50
51
52 /**
53 * Register a service provider to create new \Psr\Log\LoggerInterface
54 * instances.
55 *
56 * @param MWLoggerSpi $provider Provider to register
57 */
58 public static function registerProvider( MWLoggerSpi $provider ) {
59 self::$spi = $provider;
60 }
61
62
63 /**
64 * Get the registered service provider.
65 *
66 * If called before any service provider has been registered, it will
67 * attempt to use the $wgMWLoggerDefaultSpi global to bootstrap
68 * MWLoggerSpi registration. $wgMWLoggerDefaultSpi is expected to be an
69 * array usable by ObjectFactory::getObjectFromSpec() to create a class.
70 *
71 * @return MWLoggerSpi
72 * @see registerProvider()
73 * @see ObjectFactory::getObjectFromSpec()
74 */
75 public static function getProvider() {
76 if ( self::$spi === null ) {
77 global $wgMWLoggerDefaultSpi;
78 $provider = ObjectFactory::getObjectFromSpec(
79 $wgMWLoggerDefaultSpi
80 );
81 self::registerProvider( $provider );
82 }
83 return self::$spi;
84 }
85
86
87 /**
88 * Get a named logger instance from the currently configured logger factory.
89 *
90 * @param string $channel Logger channel (name)
91 * @return \Psr\Log\LoggerInterface
92 */
93 public static function getInstance( $channel ) {
94 if ( !interface_exists( '\Psr\Log\LoggerInterface' ) ) {
95 $message = <<<TXT
96 MediaWiki requires the <a href="https://github.com/php-fig/log">PSR-3 logging library</a> to be present. This library is not embedded directly in MediaWiki's git repository and must be installed separately by the end user.
97
98 Please see <a href="https://www.mediawiki.org/wiki/Download_from_Git#Fetch_external_libraries">mediawiki.org</a> for help on installing the required components.
99 TXT;
100 echo $message;
101 trigger_error( $message, E_USER_ERROR );
102 die( 1 );
103 }
104
105 return self::getProvider()->getLogger( $channel );
106 }
107
108
109 /**
110 * Construction of utility class is not allowed.
111 */
112 private function __construct() {
113 // no-op
114 }
115 }