Merge "(bug 36761) "Mark pages as visited" should submit previously established filte...
[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 /**
44 * @return LockManagerGroup
45 */
46 public static function singleton() {
47 if ( self::$instance == null ) {
48 self::$instance = new self();
49 self::$instance->initFromGlobals();
50 }
51 return self::$instance;
52 }
53
54 /**
55 * Destroy the singleton instance, so that a new one will be created next
56 * time singleton() is called.
57 */
58 public static function destroySingleton() {
59 self::$instance = null;
60 }
61
62 /**
63 * Register lock managers from the global variables
64 *
65 * @return void
66 */
67 protected function initFromGlobals() {
68 global $wgLockManagers;
69
70 $this->register( $wgLockManagers );
71 }
72
73 /**
74 * Register an array of file lock manager configurations
75 *
76 * @param $configs Array
77 * @return void
78 * @throws MWException
79 */
80 protected function register( array $configs ) {
81 foreach ( $configs as $config ) {
82 if ( !isset( $config['name'] ) ) {
83 throw new MWException( "Cannot register a lock manager with no name." );
84 }
85 $name = $config['name'];
86 if ( !isset( $config['class'] ) ) {
87 throw new MWException( "Cannot register lock manager `{$name}` with no class." );
88 }
89 $class = $config['class'];
90 unset( $config['class'] ); // lock manager won't need this
91 $this->managers[$name] = array(
92 'class' => $class,
93 'config' => $config,
94 'instance' => null
95 );
96 }
97 }
98
99 /**
100 * Get the lock manager object with a given name
101 *
102 * @param $name string
103 * @return LockManager
104 * @throws MWException
105 */
106 public function get( $name ) {
107 if ( !isset( $this->managers[$name] ) ) {
108 throw new MWException( "No lock manager defined with the name `$name`." );
109 }
110 // Lazy-load the actual lock manager instance
111 if ( !isset( $this->managers[$name]['instance'] ) ) {
112 $class = $this->managers[$name]['class'];
113 $config = $this->managers[$name]['config'];
114 $this->managers[$name]['instance'] = new $class( $config );
115 }
116 return $this->managers[$name]['instance'];
117 }
118 }