Merge "Http::getProxy() method to get proxy configuration"
[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
24 /**
25 * Class to handle file lock manager registration
26 *
27 * @ingroup LockManager
28 * @author Aaron Schulz
29 * @since 1.19
30 */
31 class LockManagerGroup {
32 /** @var array (domain => LockManager) */
33 protected static $instances = [];
34
35 protected $domain; // string; domain (usually wiki ID)
36
37 /** @var array Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
38 protected $managers = [];
39
40 /**
41 * @param string $domain Domain (usually wiki ID)
42 */
43 protected function __construct( $domain ) {
44 $this->domain = $domain;
45 }
46
47 /**
48 * @param bool|string $domain Domain (usually wiki ID). Default: false.
49 * @return LockManagerGroup
50 */
51 public static function singleton( $domain = false ) {
52 $domain = ( $domain === false ) ? wfWikiID() : $domain;
53 if ( !isset( self::$instances[$domain] ) ) {
54 self::$instances[$domain] = new self( $domain );
55 self::$instances[$domain]->initFromGlobals();
56 }
57
58 return self::$instances[$domain];
59 }
60
61 /**
62 * Destroy the singleton instances
63 */
64 public static function destroySingletons() {
65 self::$instances = [];
66 }
67
68 /**
69 * Register lock managers from the global variables
70 */
71 protected function initFromGlobals() {
72 global $wgLockManagers;
73
74 $this->register( $wgLockManagers );
75 }
76
77 /**
78 * Register an array of file lock manager configurations
79 *
80 * @param array $configs
81 * @throws Exception
82 */
83 protected function register( array $configs ) {
84 foreach ( $configs as $config ) {
85 $config['domain'] = $this->domain;
86 if ( !isset( $config['name'] ) ) {
87 throw new Exception( "Cannot register a lock manager with no name." );
88 }
89 $name = $config['name'];
90 if ( !isset( $config['class'] ) ) {
91 throw new Exception( "Cannot register lock manager `{$name}` with no class." );
92 }
93 $class = $config['class'];
94 unset( $config['class'] ); // lock manager won't need this
95 $this->managers[$name] = [
96 'class' => $class,
97 'config' => $config,
98 'instance' => null
99 ];
100 }
101 }
102
103 /**
104 * Get the lock manager object with a given name
105 *
106 * @param string $name
107 * @return LockManager
108 * @throws Exception
109 */
110 public function get( $name ) {
111 if ( !isset( $this->managers[$name] ) ) {
112 throw new Exception( "No lock manager defined with the name `$name`." );
113 }
114 // Lazy-load the actual lock manager instance
115 if ( !isset( $this->managers[$name]['instance'] ) ) {
116 $class = $this->managers[$name]['class'];
117 $config = $this->managers[$name]['config'];
118 $this->managers[$name]['instance'] = new $class( $config );
119 }
120
121 return $this->managers[$name]['instance'];
122 }
123
124 /**
125 * Get the config array for a lock manager object with a given name
126 *
127 * @param string $name
128 * @return array
129 * @throws Exception
130 */
131 public function config( $name ) {
132 if ( !isset( $this->managers[$name] ) ) {
133 throw new Exception( "No lock manager defined with the name `$name`." );
134 }
135 $class = $this->managers[$name]['class'];
136
137 return [ 'class' => $class ] + $this->managers[$name]['config'];
138 }
139
140 /**
141 * Get the default lock manager configured for the site.
142 * Returns NullLockManager if no lock manager could be found.
143 *
144 * @return LockManager
145 */
146 public function getDefault() {
147 return isset( $this->managers['default'] )
148 ? $this->get( 'default' )
149 : new NullLockManager( [] );
150 }
151
152 /**
153 * Get the default lock manager configured for the site
154 * or at least some other effective configured lock manager.
155 * Throws an exception if no lock manager could be found.
156 *
157 * @return LockManager
158 * @throws Exception
159 */
160 public function getAny() {
161 return isset( $this->managers['default'] )
162 ? $this->get( 'default' )
163 : $this->get( 'fsLockManager' );
164 }
165 }