Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / config / ConfigFactory.php
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 use Wikimedia\Assert\Assert;
24 use Wikimedia\Services\SalvageableService;
25
26 /**
27 * Factory class to create Config objects
28 *
29 * @since 1.23
30 */
31 class ConfigFactory implements SalvageableService {
32
33 /**
34 * Map of config name => callback
35 * @var array
36 */
37 protected $factoryFunctions = [];
38
39 /**
40 * Config objects that have already been created
41 * name => Config object
42 * @var array
43 */
44 protected $configs = [];
45
46 /**
47 * @deprecated since 1.27, use MediaWikiServices::getInstance()->getConfigFactory() instead.
48 *
49 * @return ConfigFactory
50 */
51 public static function getDefaultInstance() {
52 return \MediaWiki\MediaWikiServices::getInstance()->getConfigFactory();
53 }
54
55 /**
56 * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
57 * registered factory function for both is the same. Cache config is not copied,
58 * and only instances of caches defined on this instance with the same config
59 * are copied.
60 *
61 * @see SalvageableService::salvage()
62 *
63 * @param SalvageableService $other The object to salvage state from. $other must have the
64 * exact same type as $this.
65 */
66 public function salvage( SalvageableService $other ) {
67 Assert::parameterType( self::class, $other, '$other' );
68
69 /** @var self $other */
70 '@phan-var self $other';
71 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
72 if ( !isset( $this->factoryFunctions[$name] ) ) {
73 continue;
74 }
75
76 // if the callback function is the same, salvage the Cache object
77 // XXX: Closures are never equal!
78 if ( isset( $other->configs[$name] )
79 && $this->factoryFunctions[$name] == $otherFunc
80 ) {
81 $this->configs[$name] = $other->configs[$name];
82 unset( $other->configs[$name] );
83 }
84 }
85
86 // disable $other
87 $other->factoryFunctions = [];
88 $other->configs = [];
89 }
90
91 /**
92 * @return string[]
93 */
94 public function getConfigNames() {
95 return array_keys( $this->factoryFunctions );
96 }
97
98 /**
99 * Register a new config factory function.
100 * Will override if it's already registered.
101 * Use "*" for $name to provide a fallback config for all unknown names.
102 * @param string $name
103 * @param callable|Config $callback A factory callback that takes this ConfigFactory
104 * as an argument and returns a Config instance, or an existing Config instance.
105 * @throws InvalidArgumentException If an invalid callback is provided
106 */
107 public function register( $name, $callback ) {
108 if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) {
109 if ( is_array( $callback ) ) {
110 $callback = '[ ' . implode( ', ', $callback ) . ' ]';
111 } elseif ( is_object( $callback ) ) {
112 $callback = 'instanceof ' . get_class( $callback );
113 }
114 throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' );
115 }
116
117 unset( $this->configs[$name] );
118 $this->factoryFunctions[$name] = $callback;
119 }
120
121 /**
122 * Create a given Config using the registered callback for $name.
123 * If an object was already created, the same Config object is returned.
124 * @param string $name Name of the extension/component you want a Config object for
125 * 'main' is used for core
126 * @throws ConfigException If a factory function isn't registered for $name
127 * @throws UnexpectedValueException If the factory function returns a non-Config object
128 * @return Config
129 */
130 public function makeConfig( $name ) {
131 if ( !isset( $this->configs[$name] ) ) {
132 $key = $name;
133 if ( !isset( $this->factoryFunctions[$key] ) ) {
134 $key = '*';
135 }
136 if ( !isset( $this->factoryFunctions[$key] ) ) {
137 throw new ConfigException( "No registered builder available for $name." );
138 }
139
140 if ( $this->factoryFunctions[$key] instanceof Config ) {
141 $conf = $this->factoryFunctions[$key];
142 } else {
143 $conf = call_user_func( $this->factoryFunctions[$key], $this );
144 }
145
146 if ( $conf instanceof Config ) {
147 $this->configs[$name] = $conf;
148 } else {
149 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
150 }
151 }
152
153 return $this->configs[$name];
154 }
155
156 }