MediaWiki REL1_28
ObjectFactory.php
Go to the documentation of this file.
1<?php
28
59 public static function getObjectFromSpec( $spec ) {
60 $args = isset( $spec['args'] ) ? $spec['args'] : [];
61 $expandArgs = !isset( $spec['closure_expansion'] ) ||
62 $spec['closure_expansion'] === true;
63
64 if ( $expandArgs ) {
65 $args = static::expandClosures( $args );
66 }
67
68 if ( isset( $spec['class'] ) ) {
69 $clazz = $spec['class'];
70 if ( !$args ) {
71 $obj = new $clazz();
72 } else {
73 $obj = static::constructClassInstance( $clazz, $args );
74 }
75 } elseif ( isset( $spec['factory'] ) ) {
76 $obj = call_user_func_array( $spec['factory'], $args );
77 } else {
78 throw new InvalidArgumentException(
79 'Provided specification lacks both factory and class parameters.'
80 );
81 }
82
83 if ( isset( $spec['calls'] ) && is_array( $spec['calls'] ) ) {
84 // Call additional methods on the newly created object
85 foreach ( $spec['calls'] as $method => $margs ) {
86 if ( $expandArgs ) {
87 $margs = static::expandClosures( $margs );
88 }
89 call_user_func_array( [ $obj, $method ], $margs );
90 }
91 }
92
93 return $obj;
94 }
95
102 protected static function expandClosures( $list ) {
103 return array_map( function ( $value ) {
104 if ( is_object( $value ) && $value instanceof Closure ) {
105 // If $value is a Closure, call it.
106 return $value();
107 } else {
108 return $value;
109 }
110 }, $list );
111 }
112
130 public static function constructClassInstance( $clazz, $args ) {
131 // $args should be a non-associative array; show nice error if that's not the case
132 if ( $args && array_keys( $args ) !== range( 0, count( $args ) - 1 ) ) {
133 throw new InvalidArgumentException( __METHOD__ . ': $args cannot be an associative array' );
134 }
135
136 // TODO: when PHP min version supported is >=5.6.0 replace this
137 // with `return new $clazz( ... $args );`.
138 $obj = null;
139 switch ( count( $args ) ) {
140 case 0:
141 $obj = new $clazz();
142 break;
143 case 1:
144 $obj = new $clazz( $args[0] );
145 break;
146 case 2:
147 $obj = new $clazz( $args[0], $args[1] );
148 break;
149 case 3:
150 $obj = new $clazz( $args[0], $args[1], $args[2] );
151 break;
152 case 4:
153 $obj = new $clazz( $args[0], $args[1], $args[2], $args[3] );
154 break;
155 case 5:
156 $obj = new $clazz(
157 $args[0], $args[1], $args[2], $args[3], $args[4]
158 );
159 break;
160 case 6:
161 $obj = new $clazz(
162 $args[0], $args[1], $args[2], $args[3], $args[4],
163 $args[5]
164 );
165 break;
166 case 7:
167 $obj = new $clazz(
168 $args[0], $args[1], $args[2], $args[3], $args[4],
169 $args[5], $args[6]
170 );
171 break;
172 case 8:
173 $obj = new $clazz(
174 $args[0], $args[1], $args[2], $args[3], $args[4],
175 $args[5], $args[6], $args[7]
176 );
177 break;
178 case 9:
179 $obj = new $clazz(
180 $args[0], $args[1], $args[2], $args[3], $args[4],
181 $args[5], $args[6], $args[7], $args[8]
182 );
183 break;
184 case 10:
185 $obj = new $clazz(
186 $args[0], $args[1], $args[2], $args[3], $args[4],
187 $args[5], $args[6], $args[7], $args[8], $args[9]
188 );
189 break;
190 default:
191 // Fall back to using ReflectionClass and curse the developer
192 // who decided that 11+ args was a reasonable method
193 // signature.
194 $ref = new ReflectionClass( $clazz );
195 $obj = $ref->newInstanceArgs( $args );
196 }
197 return $obj;
198 }
199}
if( $line===false) $args
Definition cdb.php:64
Construct objects from configuration instructions.
static getObjectFromSpec( $spec)
Instantiate an object based on a specification array.
static constructClassInstance( $clazz, $args)
Construct an instance of the given class using the given arguments.
static expandClosures( $list)
Iterate a list and call any closures it contains.
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
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