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