8 * Class to handle file backend registration
10 * @ingroup FileBackend
12 class FileBackendGroup
{
13 protected static $instance = null;
15 /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
16 protected $backends = array();
18 protected function __construct() {}
19 protected function __clone() {}
21 public static function singleton() {
22 if ( self
::$instance == null ) {
23 self
::$instance = new self();
25 return self
::$instance;
29 * Destroy the singleton instance, so that a new one will be created next
30 * time singleton() is called.
32 public static function destroySingleton() {
33 self
::$instance = null;
37 * Register an array of file backend configurations
39 * @param $configs Array
43 public function register( array $configs ) {
44 foreach ( $configs as $config ) {
45 if ( !isset( $config['name'] ) ) {
46 throw new MWException( "Cannot register a backend with no name." );
48 $name = $config['name'];
49 if ( !isset( $config['class'] ) ) {
50 throw new MWException( "Cannot register backend `{$name}` with no class." );
52 $class = $config['class'];
54 unset( $config['class'] ); // backend won't need this
55 $this->backends
[$name] = array(
64 * Get the backend object with a given name
67 * @return FileBackendBase
70 public function get( $name ) {
71 if ( !isset( $this->backends
[$name] ) ) {
72 throw new MWException( "No backend defined with the name `$name`." );
74 // Lazy-load the actual backend instance
75 if ( !isset( $this->backends
[$name]['instance'] ) ) {
76 $class = $this->backends
[$name]['class'];
77 $config = $this->backends
[$name]['config'];
78 $this->backends
[$name]['instance'] = new $class( $config );
80 return $this->backends
[$name]['instance'];
84 * Get an appropriate backend object from a storage path
86 * @param $storagePath string
87 * @return FileBackendBase|null Backend or null on failure
89 public function backendFromPath( $storagePath ) {
90 list( $backend, $c, $p ) = FileBackend
::splitStoragePath( $storagePath );
91 if ( $backend !== null && isset( $this->backends
[$backend] ) ) {
92 return $this->get( $backend );