[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/config/ -> ConfigFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Copyright 2014
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19   * http://www.gnu.org/copyleft/gpl.html
  20   *
  21   * @file
  22   */
  23  
  24  /**
  25   * Factory class to create Config objects
  26   *
  27   * @since 1.23
  28   */
  29  class ConfigFactory {
  30  
  31      /**
  32       * Map of config name => callback
  33       * @var array
  34       */
  35      protected $factoryFunctions = array();
  36  
  37      /**
  38       * Config objects that have already been created
  39       * name => Config object
  40       * @var array
  41       */
  42      protected $configs = array();
  43  
  44      /**
  45       * @var ConfigFactory
  46       */
  47      private static $self;
  48  
  49  	public static function getDefaultInstance() {
  50          if ( !self::$self ) {
  51              self::$self = new self;
  52              global $wgConfigRegistry;
  53              foreach ( $wgConfigRegistry as $name => $callback ) {
  54                  self::$self->register( $name, $callback );
  55              }
  56          }
  57          return self::$self;
  58      }
  59  
  60      /**
  61       * Destroy the default instance
  62       * Should only be called inside unit tests
  63       * @throws MWException
  64       * @codeCoverageIgnore
  65       */
  66  	public static function destroyDefaultInstance() {
  67          if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
  68              throw new MWException( __METHOD__ . ' was called outside of unit tests' );
  69          }
  70  
  71          self::$self = null;
  72      }
  73  
  74      /**
  75       * Register a new config factory function
  76       * Will override if it's already registered
  77       * @param string $name
  78       * @param callable $callback That takes this ConfigFactory as an argument
  79       * @throws InvalidArgumentException If an invalid callback is provided
  80       */
  81  	public function register( $name, $callback ) {
  82          if ( !is_callable( $callback ) ) {
  83              throw new InvalidArgumentException( 'Invalid callback provided' );
  84          }
  85          $this->factoryFunctions[$name] = $callback;
  86      }
  87  
  88      /**
  89       * Create a given Config using the registered callback for $name.
  90       * If an object was already created, the same Config object is returned.
  91       * @param string $name Name of the extension/component you want a Config object for
  92       *                     'main' is used for core
  93       * @throws ConfigException If a factory function isn't registered for $name
  94       * @throws UnexpectedValueException If the factory function returns a non-Config object
  95       * @return Config
  96       */
  97  	public function makeConfig( $name ) {
  98          if ( !isset( $this->configs[$name] ) ) {
  99              if ( !isset( $this->factoryFunctions[$name] ) ) {
 100                  throw new ConfigException( "No registered builder available for $name." );
 101              }
 102              $conf = call_user_func( $this->factoryFunctions[$name], $this );
 103              if ( $conf instanceof Config ) {
 104                  $this->configs[$name] = $conf;
 105              } else {
 106                  throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
 107              }
 108          }
 109  
 110          return $this->configs[$name];
 111      }
 112  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1