LockManagerGroupFactory to replace singletons
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / LockManagerGroup.php
1 <?php
2 /**
3 * Lock manager registration handling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup LockManager
22 */
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Logger\LoggerFactory;
25 use Wikimedia\Rdbms\LBFactory;
26
27 /**
28 * Class to handle file lock manager registration
29 *
30 * @ingroup LockManager
31 * @since 1.19
32 */
33 class LockManagerGroup {
34 /** @var string domain (usually wiki ID) */
35 protected $domain;
36
37 /** @var LBFactory */
38 protected $lbFactory;
39
40 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
41 protected $managers = [];
42
43 /**
44 * Do not call this directly. Use LockManagerGroupFactory.
45 *
46 * @param string $domain Domain (usually wiki ID)
47 * @param array[] $lockManagerConfigs In format of $wgLockManagers
48 * @param LBFactory $lbFactory
49 */
50 public function __construct( $domain, array $lockManagerConfigs, LBFactory $lbFactory ) {
51 $this->domain = $domain;
52 $this->lbFactory = $lbFactory;
53
54 foreach ( $lockManagerConfigs as $config ) {
55 $config['domain'] = $this->domain;
56 if ( !isset( $config['name'] ) ) {
57 throw new Exception( "Cannot register a lock manager with no name." );
58 }
59 $name = $config['name'];
60 if ( !isset( $config['class'] ) ) {
61 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
62 }
63 $class = $config['class'];
64 unset( $config['class'] ); // lock manager won't need this
65 $this->managers[$name] = [
66 'class' => $class,
67 'config' => $config,
68 'instance' => null
69 ];
70 }
71 }
72
73 /**
74 * @deprecated since 1.34, use LockManagerGroupFactory
75 *
76 * @param bool|string $domain Domain (usually wiki ID). Default: false.
77 * @return LockManagerGroup
78 */
79 public static function singleton( $domain = false ) {
80 return MediaWikiServices::getInstance()->getLockManagerGroupFactory()
81 ->getLockManagerGroup( $domain );
82 }
83
84 /**
85 * Destroy the singleton instances
86 *
87 * @deprecated since 1.34, use resetServiceForTesting() on LockManagerGroupFactory
88 */
89 public static function destroySingletons() {
90 MediaWikiServices::getInstance()->resetServiceForTesting( 'LockManagerGroupFactory' );
91 }
92
93 /**
94 * Get the lock manager object with a given name
95 *
96 * @param string $name
97 * @return LockManager
98 * @throws Exception
99 */
100 public function get( $name ) {
101 if ( !isset( $this->managers[$name] ) ) {
102 throw new Exception( "No lock manager defined with the name `$name`." );
103 }
104 // Lazy-load the actual lock manager instance
105 if ( !isset( $this->managers[$name]['instance'] ) ) {
106 $class = $this->managers[$name]['class'];
107 $config = $this->managers[$name]['config'];
108 if ( $class === DBLockManager::class ) {
109 $lb = $this->lbFactory->getMainLB( $config['domain'] );
110 $config['dbServers']['localDBMaster'] = $lb->getLazyConnectionRef(
111 DB_MASTER,
112 [],
113 $config['domain'],
114 $lb::CONN_TRX_AUTOCOMMIT
115 );
116 $config['srvCache'] = ObjectCache::getLocalServerInstance( 'hash' );
117 }
118 $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
119
120 // XXX Looks like phan is right, we are trying to instantiate an abstract class and it
121 // throws. Did this ever work? Presumably we need to detect the right subclass? Or
122 // should we just get rid of this? It looks like it never worked since it was first
123 // introduced by 0cf832a3394 in 2016, so if no one's complained until now, clearly it
124 // can't be very useful?
125 // @phan-suppress-next-line PhanTypeInstantiateAbstract
126 $this->managers[$name]['instance'] = new $class( $config );
127 }
128
129 return $this->managers[$name]['instance'];
130 }
131
132 /**
133 * Get the config array for a lock manager object with a given name
134 *
135 * @param string $name
136 * @return array
137 * @throws Exception
138 */
139 public function config( $name ) {
140 if ( !isset( $this->managers[$name] ) ) {
141 throw new Exception( "No lock manager defined with the name `$name`." );
142 }
143 $class = $this->managers[$name]['class'];
144
145 return [ 'class' => $class ] + $this->managers[$name]['config'];
146 }
147
148 /**
149 * Get the default lock manager configured for the site.
150 * Returns NullLockManager if no lock manager could be found.
151 *
152 * XXX This looks unused, should we just get rid of it?
153 *
154 * @return LockManager
155 */
156 public function getDefault() {
157 return isset( $this->managers['default'] )
158 ? $this->get( 'default' )
159 : new NullLockManager( [] );
160 }
161
162 /**
163 * Get the default lock manager configured for the site
164 * or at least some other effective configured lock manager.
165 * Throws an exception if no lock manager could be found.
166 *
167 * XXX This looks unused, should we just get rid of it?
168 *
169 * @return LockManager
170 * @throws Exception
171 */
172 public function getAny() {
173 return isset( $this->managers['default'] )
174 ? $this->get( 'default' )
175 : $this->get( 'fsLockManager' );
176 }
177 }