Kill "* @return void"
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackendGroup.php
1 <?php
2 /**
3 * @file
4 * @ingroup FileBackend
5 * @author Aaron Schulz
6 */
7
8 /**
9 * Class to handle file backend registration
10 *
11 * @ingroup FileBackend
12 */
13 class FileBackendGroup {
14 protected static $instance = null;
15
16 /** @var Array (name => ('class' => string, 'config' => array, 'instance' => object)) */
17 protected $backends = array();
18
19 protected function __construct() {}
20 protected function __clone() {}
21
22 /**
23 * @return FileBackendGroup
24 */
25 public static function singleton() {
26 if ( self::$instance == null ) {
27 self::$instance = new self();
28 self::$instance->initFromGlobals();
29 }
30 return self::$instance;
31 }
32
33 /**
34 * Destroy the singleton instance
35 */
36 public static function destroySingleton() {
37 self::$instance = null;
38 }
39
40 /**
41 * Register file backends from the global variables
42 */
43 protected function initFromGlobals() {
44 global $wgLocalFileRepo, $wgForeignFileRepos, $wgFileBackends;
45
46 // Register explicitly defined backends
47 $this->register( $wgFileBackends );
48
49 $autoBackends = array();
50 // Automatically create b/c backends for file repos...
51 $repos = array_merge( $wgForeignFileRepos, array( $wgLocalFileRepo ) );
52 foreach ( $repos as $info ) {
53 $backendName = $info['backend'];
54 if ( is_object( $backendName ) || isset( $this->backends[$backendName] ) ) {
55 continue; // already defined (or set to the object for some reason)
56 }
57 $repoName = $info['name'];
58 // Local vars that used to be FSRepo members...
59 $directory = $info['directory'];
60 $deletedDir = isset( $info['deletedDir'] )
61 ? $info['deletedDir']
62 : false; // deletion disabled
63 $thumbDir = isset( $info['thumbDir'] )
64 ? $info['thumbDir']
65 : "{$directory}/thumb";
66 $fileMode = isset( $info['fileMode'] )
67 ? $info['fileMode']
68 : 0644;
69 // Get the FS backend configuration
70 $autoBackends[] = array(
71 'name' => $backendName,
72 'class' => 'FSFileBackend',
73 'lockManager' => 'fsLockManager',
74 'containerPaths' => array(
75 "{$repoName}-public" => "{$directory}",
76 "{$repoName}-thumb" => $thumbDir,
77 "{$repoName}-deleted" => $deletedDir,
78 "{$repoName}-temp" => "{$directory}/temp"
79 ),
80 'fileMode' => $fileMode,
81 );
82 }
83
84 // Register implicitly defined backends
85 $this->register( $autoBackends );
86 }
87
88 /**
89 * Register an array of file backend configurations
90 *
91 * @param $configs Array
92 * @throws MWException
93 */
94 protected function register( array $configs ) {
95 foreach ( $configs as $config ) {
96 if ( !isset( $config['name'] ) ) {
97 throw new MWException( "Cannot register a backend with no name." );
98 }
99 $name = $config['name'];
100 if ( !isset( $config['class'] ) ) {
101 throw new MWException( "Cannot register backend `{$name}` with no class." );
102 }
103 $class = $config['class'];
104
105 unset( $config['class'] ); // backend won't need this
106 $this->backends[$name] = array(
107 'class' => $class,
108 'config' => $config,
109 'instance' => null
110 );
111 }
112 }
113
114 /**
115 * Get the backend object with a given name
116 *
117 * @param $name string
118 * @return FileBackendBase
119 * @throws MWException
120 */
121 public function get( $name ) {
122 if ( !isset( $this->backends[$name] ) ) {
123 throw new MWException( "No backend defined with the name `$name`." );
124 }
125 // Lazy-load the actual backend instance
126 if ( !isset( $this->backends[$name]['instance'] ) ) {
127 $class = $this->backends[$name]['class'];
128 $config = $this->backends[$name]['config'];
129 $this->backends[$name]['instance'] = new $class( $config );
130 }
131 return $this->backends[$name]['instance'];
132 }
133
134 /**
135 * Get an appropriate backend object from a storage path
136 *
137 * @param $storagePath string
138 * @return FileBackendBase|null Backend or null on failure
139 */
140 public function backendFromPath( $storagePath ) {
141 list( $backend, $c, $p ) = FileBackend::splitStoragePath( $storagePath );
142 if ( $backend !== null && isset( $this->backends[$backend] ) ) {
143 return $this->get( $backend );
144 }
145 return null;
146 }
147 }