MediaWiki REL1_28
ConfigFactory.php
Go to the documentation of this file.
1<?php
2
24use Wikimedia\Assert\Assert;
25
32
37 protected $factoryFunctions = [];
38
44 protected $configs = [];
45
51 public static function getDefaultInstance() {
52 return \MediaWiki\MediaWikiServices::getInstance()->getConfigFactory();
53 }
54
66 public function salvage( SalvageableService $other ) {
67 Assert::parameterType( self::class, $other, '$other' );
68
70 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
71 if ( !isset( $this->factoryFunctions[$name] ) ) {
72 continue;
73 }
74
75 // if the callback function is the same, salvage the Cache object
76 // XXX: Closures are never equal!
77 if ( isset( $other->configs[$name] )
78 && $this->factoryFunctions[$name] == $otherFunc
79 ) {
80 $this->configs[$name] = $other->configs[$name];
81 unset( $other->configs[$name] );
82 }
83 }
84
85 // disable $other
86 $other->factoryFunctions = [];
87 $other->configs = [];
88 }
89
93 public function getConfigNames() {
94 return array_keys( $this->factoryFunctions );
95 }
96
106 public function register( $name, $callback ) {
107 if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) {
108 throw new InvalidArgumentException( 'Invalid callback provided' );
109 }
110
111 unset( $this->configs[$name] );
112 $this->factoryFunctions[$name] = $callback;
113 }
114
124 public function makeConfig( $name ) {
125 if ( !isset( $this->configs[$name] ) ) {
126 $key = $name;
127 if ( !isset( $this->factoryFunctions[$key] ) ) {
128 $key = '*';
129 }
130 if ( !isset( $this->factoryFunctions[$key] ) ) {
131 throw new ConfigException( "No registered builder available for $name." );
132 }
133
134 if ( $this->factoryFunctions[$key] instanceof Config ) {
135 $conf = $this->factoryFunctions[$key];
136 } else {
137 $conf = call_user_func( $this->factoryFunctions[$key], $this );
138 }
139
140 if ( $conf instanceof Config ) {
141 $this->configs[$name] = $conf;
142 } else {
143 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
144 }
145 }
146
147 return $this->configs[$name];
148 }
149
150}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Exceptions for config failures.
Factory class to create Config objects.
static getDefaultInstance()
makeConfig( $name)
Create a given Config using the registered callback for $name.
salvage(SalvageableService $other)
Re-uses existing Cache objects from $other.
array $factoryFunctions
Map of config name => callback.
array $configs
Config objects that have already been created name => Config object.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:304
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for configuration instances.
Definition Config.php:28
SalvageableService defines an interface for services that are able to salvage state from a previous i...