wiki.techinc.nl/includes/config/ConfigFactory.php
Kunal Mehta 8977e56a6b Make abstract Config class truly implementation-agnostic
Follow up to I13baec0b6 ("Config: Add Config and GlobalConfig classes"):

Config:
* Rather than returning Status objects, Config::set will now throw an exception
  if an error is encountered
* Config::factory was moved into it's own ConfigFactory class.
* Since there are no more functions in it, Config was turned into an interface.

GlobalConfig:
* Remove $prefix args from Config::set and ::get. The idea of having an
  abstract Config class is to abstract some notion of configuration data from
  the particular way in which it is currently implemented (global variables).
  So the abstract base class has no business dealing with variable name
  prefixes.
** Instead GlobalVarConfig's implementations of get and set call getWithPrefix
   and setWithPrefix internally, which are now protected
* Rename GlobalConfig to GlobalVarConfig, which makes it clearer that it isn't
  referring to the scope of the configuration value, but to the scope of the
  variable name which provides it.

ConfigFactory:
* ConfigFactory is where Config objects are registered, and later constructed.
* Config objects are registered with a given name, and a callback factory function.
  This allows for implementations to construct the object with the parameters they want,
  and avoids the overhead of needing an entire class.
** The name 'main' is the default object returned by RequestContext::getConfig(),
   and is intended to be used by core.
* This is a singleton class, the main instance can be obtained with:
  ConfigFactory::getDefaultInstance()

In addition to the above:
* $wgConfigClass was removed, and $wgConfigRegistry was introduced, which
  stores a name => callback. The name is to be what the Config instance is
  registered with, and the callback should return an implementation of Config.
* Tests were written for the new ConfigFactory, and GlobalVarConfig's tests
  were improved.

Co-Authored-By: Ori Livneh <ori@wikimedia.org>
Co-Authored-By: Chad Horohoe <chadh@wikimedia.org>
Co-Authored-By: Mattflaschen <mflaschen@wikimedia.org>
Co-Authored-By: Parent5446 <tylerromeo@gmail.com>
Co-Authored-By: Reedy <reedy@wikimedia.org>
Co-Authored-By: Daniel Kinzler <daniel.kinzler@wikimedia.de>
Change-Id: I5a5857fcfa07598ba4ce9ae5bbb4ce54a567d31e
2014-05-26 02:59:57 -07:00

94 lines
2.8 KiB
PHP

<?php
/**
* Copyright 2014
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* Factory class to create Config objects
*
* @since 1.23
*/
class ConfigFactory {
/**
* Map of config name => callback
* @var array
*/
protected $factoryFunctions = array();
/**
* Config objects that have already been created
* name => Config object
* @var array
*/
protected $configs = array();
public static function getDefaultInstance() {
static $self = null;
if ( !$self ) {
$self = new self;
global $wgConfigRegistry;
foreach ( $wgConfigRegistry as $name => $callback ) {
$self->register( $name, $callback );
}
}
return $self;
}
/**
* Register a new config factory function
* Will override if it's already registered
* @param string $name
* @param callable $callback that takes this ConfigFactory as an argument
* @throws InvalidArgumentException if an invalid callback is provided
*/
public function register( $name, $callback ) {
if ( !is_callable( $callback ) ) {
throw new InvalidArgumentException( 'Invalid callback provided' );
}
$this->factoryFunctions[$name] = $callback;
}
/**
* Create a given Config using the registered callback for $name.
* If an object was already created, the same Config object is returned.
* @param string $name of the extension/component you want a Config object for
* 'main' is used for core
* @throws ConfigException if a factory function isn't registered for $name
* @throws UnexpectedValueException if the factory function returns a non-Config object
* @return Config
*/
public function makeConfig( $name ) {
if ( !isset( $this->configs[$name] ) ) {
if ( !isset( $this->factoryFunctions[$name] ) ) {
throw new ConfigException( "No registered builder available for $name." );
}
$conf = call_user_func( $this->factoryFunctions[$name], $this );
if ( $conf instanceof Config ) {
$this->configs[$name] = $conf;
} else {
throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
}
}
return $this->configs[$name];
}
}