Revert "Adding sanity check to Title::isRedirect()."
[lhc/web/wiklou.git] / includes / filerepo / backend / 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 /**
34 * @var LockManagerGroup
35 */
36 protected static $instance = null;
37
38 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
39 protected $managers = array();
40
41 protected function __construct() {}
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 }