X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FStubObject.php;h=92130761a62e1594edbbb58341314b3822ad63a6;hb=daf15e14ab481e9b8811bc825bb448df5ec80421;hp=0b4d048e74cdaa877fbd6f64b090f2956e992e96;hpb=42324aff4e173b70aeed4f520ff0f5b96557d24e;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/StubObject.php b/includes/StubObject.php index 0b4d048e74..92130761a6 100644 --- a/includes/StubObject.php +++ b/includes/StubObject.php @@ -19,6 +19,7 @@ * * @file */ +use Wikimedia\ObjectFactory; /** * Class to implement stub globals, which are globals that delay loading the @@ -48,19 +49,25 @@ class StubObject { /** @var null|string */ protected $class; + /** @var null|callable */ + protected $factory; + /** @var array */ protected $params; /** - * Constructor. - * - * @param string $global Name of the global variable. - * @param string $class Name of the class of the real object. + * @param string|null $global Name of the global variable. + * @param string|callable|null $class Name of the class of the real object + * or a factory function to call * @param array $params Parameters to pass to constructor of the real object. */ public function __construct( $global = null, $class = null, $params = [] ) { $this->global = $global; - $this->class = $class; + if ( is_callable( $class ) ) { + $this->factory = $class; + } else { + $this->class = $class; + } $this->params = $params; } @@ -80,7 +87,7 @@ class StubObject { * infinite loop when unstubbing an object or to avoid reference parameter * breakage. * - * @param object $obj Object to check. + * @param object &$obj Object to check. * @return void */ public static function unstub( &$obj ) { @@ -110,8 +117,10 @@ class StubObject { * @return object */ public function _newObject() { - return ObjectFactory::getObjectFromSpec( [ - 'class' => $this->class, + $params = $this->factory + ? [ 'factory' => $this->factory ] + : [ 'class' => $this->class ]; + return ObjectFactory::getObjectFromSpec( $params + [ 'args' => $this->params, 'closure_expansion' => false, ] );