merging latest master
[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 /**
33 * @var LockManagerGroup
34 */
35 protected static $instance = null;
36
37 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
38 protected $managers = array();
39
40 protected function __construct() {}
41
42 /**
43 * @return LockManagerGroup
44 */
45 public static function singleton() {
46 if ( self::$instance == null ) {
47 self::$instance = new self();
48 self::$instance->initFromGlobals();
49 }
50 return self::$instance;
51 }
52
53 /**
54 * Destroy the singleton instance, so that a new one will be created next
55 * time singleton() is called.
56 */
57 public static function destroySingleton() {
58 self::$instance = null;
59 }
60
61 /**
62 * Register lock managers from the global variables
63 *
64 * @return void
65 */
66 protected function initFromGlobals() {
67 global $wgLockManagers;
68
69 $this->register( $wgLockManagers );
70 }
71
72 /**
73 * Register an array of file lock manager configurations
74 *
75 * @param $configs Array
76 * @return void
77 * @throws MWException
78 */
79 protected function register( array $configs ) {
80 foreach ( $configs as $config ) {
81 if ( !isset( $config['name'] ) ) {
82 throw new MWException( "Cannot register a lock manager with no name." );
83 }
84 $name = $config['name'];
85 if ( !isset( $config['class'] ) ) {
86 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
87 }
88 $class = $config['class'];
89 unset( $config['class'] ); // lock manager won't need this
90 $this->managers[$name] = array(
91 'class' => $class,
92 'config' => $config,
93 'instance' => null
94 );
95 }
96 }
97
98 /**
99 * Get the lock manager object with a given name
100 *
101 * @param $name string
102 * @return LockManager
103 * @throws MWException
104 */
105 public function get( $name ) {
106 if ( !isset( $this->managers[$name] ) ) {
107 throw new MWException( "No lock manager defined with the name `$name`." );
108 }
109 // Lazy-load the actual lock manager instance
110 if ( !isset( $this->managers[$name]['instance'] ) ) {
111 $class = $this->managers[$name]['class'];
112 $config = $this->managers[$name]['config'];
113 $this->managers[$name]['instance'] = new $class( $config );
114 }
115 return $this->managers[$name]['instance'];
116 }
117
118 /**
119 * Get the default lock manager configured for the site.
120 * Returns NullLockManager if no lock manager could be found.
121 *
122 * @return LockManager
123 */
124 public function getDefault() {
125 return isset( $this->managers['default'] )
126 ? $this->get( 'default' )
127 : new NullLockManager( array() );
128 }
129
130 /**
131 * Get the default lock manager configured for the site
132 * or at least some other effective configured lock manager.
133 * Throws an exception if no lock manager could be found.
134 *
135 * @return LockManager
136 * @throws MWException
137 */
138 public function getAny() {
139 return isset( $this->managers['default'] )
140 ? $this->get( 'default' )
141 : $this->get( 'fsLockManager' );
142 }
143 }