Use wikimedia/object-factory 1.0.0

Deprecate the unnamespaced version and move it to includes/compat.

Bug: T147167
Depends-On: I39c805bfb98b32f32f3d0dc1eee9e823afe1c21a
Change-Id: I3780c7adf51683f3f7adb35a88f9a25a0a2e2530
This commit is contained in:
Kunal Mehta 2018-02-02 02:27:27 -08:00
parent d5050065d2
commit 79de8fd02f
20 changed files with 49 additions and 385 deletions

View file

@ -60,6 +60,7 @@ production.
* …
==== New external libraries ====
* Added wikimedia/object-factory 1.0.0
* …
==== Removed and replaced external libraries ====
@ -211,6 +212,8 @@ changes to languages because of Phabricator reports.
* OutputPage::readOnlyPage(); deprecated in 1.25
* OutputPage::rateLimited(); deprecated in 1.25
* The no-op method Skin::showIPinHeader(), deprecated in 1.27, was removed.
* \ObjectFactory (no namespace) is deprecated, the namespaced \Wikimedia\ObjectFactory
from the wikimedia/object-factory library should be used instead.
== Compatibility ==
MediaWiki 1.31 requires PHP 5.5.9 or later. Although HHVM 3.18.5 or later is supported,

View file

@ -1070,7 +1070,7 @@ $wgAutoloadLocalClasses = [
'ORAField' => __DIR__ . '/includes/db/ORAField.php',
'ORAResult' => __DIR__ . '/includes/db/ORAResult.php',
'ObjectCache' => __DIR__ . '/includes/objectcache/ObjectCache.php',
'ObjectFactory' => __DIR__ . '/includes/libs/ObjectFactory.php',
'ObjectFactory' => __DIR__ . '/includes/compat/ObjectFactory.php',
'OldChangesList' => __DIR__ . '/includes/changes/OldChangesList.php',
'OldLocalFile' => __DIR__ . '/includes/filerepo/file/OldLocalFile.php',
'OracleInstaller' => __DIR__ . '/includes/installer/OracleInstaller.php',

View file

@ -36,6 +36,7 @@
"wikimedia/composer-merge-plugin": "1.4.1",
"wikimedia/html-formatter": "1.0.1",
"wikimedia/ip-set": "1.2.0",
"wikimedia/object-factory": "1.0.0",
"wikimedia/php-session-serializer": "1.0.4",
"wikimedia/purtle": "1.0.6",
"wikimedia/relpath": "2.1.1",

View file

@ -47,6 +47,7 @@ use MediaWiki\Shell\CommandFactory;
use MediaWiki\Storage\BlobStoreFactory;
use MediaWiki\Storage\RevisionStore;
use MediaWiki\Storage\SqlBlobStore;
use Wikimedia\ObjectFactory;
return [
'DBLoadBalancerFactory' => function ( MediaWikiServices $services ) {

View file

@ -19,6 +19,7 @@
*
* @file
*/
use Wikimedia\ObjectFactory;
/**
* Class to implement stub globals, which are globals that delay loading the

View file

@ -31,6 +31,7 @@ use Status;
use StatusValue;
use User;
use WebRequest;
use Wikimedia\ObjectFactory;
/**
* This serves as the entry point to the authentication system.
@ -2292,7 +2293,7 @@ class AuthManager implements LoggerAwareInterface {
$ret = [];
foreach ( $specs as $spec ) {
$provider = \ObjectFactory::getObjectFromSpec( $spec );
$provider = ObjectFactory::getObjectFromSpec( $spec );
if ( !$provider instanceof $class ) {
throw new \RuntimeException(
"Expected instance of $class, got " . get_class( $provider )

View file

@ -0,0 +1,27 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* Construct objects from configuration instructions.
*
* @deprecated since 1.31, use \Wikimedia\ObjectFactory instead
*/
class ObjectFactory extends \Wikimedia\ObjectFactory {
}

View file

@ -20,6 +20,7 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Wikimedia\ObjectFactory;
use Wikimedia\WaitConditionLoop;
/**

View file

@ -20,7 +20,7 @@
namespace MediaWiki\Logger;
use ObjectFactory;
use Wikimedia\ObjectFactory;
/**
* PSR-3 logger instance factory.

View file

@ -22,7 +22,7 @@ namespace MediaWiki\Logger;
use MediaWiki\Logger\Monolog\BufferHandler;
use Monolog\Logger;
use ObjectFactory;
use Wikimedia\ObjectFactory;
/**
* LoggerFactory service provider that creates loggers implemented by

View file

@ -21,6 +21,8 @@
* @file
*/
use Wikimedia\ObjectFactory;
/**
* Object handling generic submission, CSRF protection, layout and
* other logic for UI forms. in a reusable manner.

View file

@ -1,198 +0,0 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* Construct objects from configuration instructions.
*
* @copyright © 2014 Wikimedia Foundation and contributors
*/
class ObjectFactory {
/**
* Instantiate an object based on a specification array.
*
* The specification array must contain a 'class' key with string value
* that specifies the class name to instantiate or a 'factory' key with
* a callable (is_callable() === true). It can optionally contain
* an 'args' key that provides arguments to pass to the
* constructor/callable.
*
* Values in the arguments collection which are Closure instances will be
* expanded by invoking them with no arguments before passing the
* resulting value on to the constructor/callable. This can be used to
* pass IDatabase instances or other live objects to the
* constructor/callable. This behavior can be suppressed by adding
* closure_expansion => false to the specification.
*
* The specification may also contain a 'calls' key that describes method
* calls to make on the newly created object before returning it. This
* pattern is often known as "setter injection". The value of this key is
* expected to be an associative array with method names as keys and
* argument lists as values. The argument list will be expanded (or not)
* in the same way as the 'args' key for the main object.
*
* @param array $spec Object specification
* @return object
* @throws InvalidArgumentException when object specification does not
* contain 'class' or 'factory' keys
* @throws ReflectionException when 'args' are supplied and 'class'
* constructor is non-public or non-existent
*/
public static function getObjectFromSpec( $spec ) {
$args = isset( $spec['args'] ) ? $spec['args'] : [];
$expandArgs = !isset( $spec['closure_expansion'] ) ||
$spec['closure_expansion'] === true;
if ( $expandArgs ) {
$args = static::expandClosures( $args );
}
if ( isset( $spec['class'] ) ) {
$clazz = $spec['class'];
if ( !$args ) {
$obj = new $clazz();
} else {
$obj = static::constructClassInstance( $clazz, $args );
}
} elseif ( isset( $spec['factory'] ) ) {
$obj = call_user_func_array( $spec['factory'], $args );
} else {
throw new InvalidArgumentException(
'Provided specification lacks both factory and class parameters.'
);
}
if ( isset( $spec['calls'] ) && is_array( $spec['calls'] ) ) {
// Call additional methods on the newly created object
foreach ( $spec['calls'] as $method => $margs ) {
if ( $expandArgs ) {
$margs = static::expandClosures( $margs );
}
call_user_func_array( [ $obj, $method ], $margs );
}
}
return $obj;
}
/**
* Iterate a list and call any closures it contains.
*
* @param array $list List of things
* @return array List with any Closures replaced with their output
*/
protected static function expandClosures( $list ) {
return array_map( function ( $value ) {
if ( is_object( $value ) && $value instanceof Closure ) {
// If $value is a Closure, call it.
return $value();
} else {
return $value;
}
}, $list );
}
/**
* Construct an instance of the given class using the given arguments.
*
* PHP's `call_user_func_array()` doesn't work with object construction so
* we have to use other measures. Starting with PHP 5.6.0 we could use the
* "splat" operator (`...`) to unpack the array into an argument list.
* Sadly there is no way to conditionally include a syntax construct like
* a new operator in a way that allows older versions of PHP to still
* parse the file. Instead, we will try a loop unrolling technique that
* works for 0-10 arguments. If we are passed 11 or more arguments we will
* take the performance penalty of using
* `ReflectionClass::newInstanceArgs()` to construct the desired object.
*
* @param string $clazz Class name
* @param array $args Constructor arguments
* @return mixed Constructed instance
*/
public static function constructClassInstance( $clazz, $args ) {
// $args should be a non-associative array; show nice error if that's not the case
if ( $args && array_keys( $args ) !== range( 0, count( $args ) - 1 ) ) {
throw new InvalidArgumentException( __METHOD__ . ': $args cannot be an associative array' );
}
// TODO: when PHP min version supported is >=5.6.0 replace this
// with `return new $clazz( ... $args );`.
$obj = null;
switch ( count( $args ) ) {
case 0:
$obj = new $clazz();
break;
case 1:
$obj = new $clazz( $args[0] );
break;
case 2:
$obj = new $clazz( $args[0], $args[1] );
break;
case 3:
$obj = new $clazz( $args[0], $args[1], $args[2] );
break;
case 4:
$obj = new $clazz( $args[0], $args[1], $args[2], $args[3] );
break;
case 5:
$obj = new $clazz(
$args[0], $args[1], $args[2], $args[3], $args[4]
);
break;
case 6:
$obj = new $clazz(
$args[0], $args[1], $args[2], $args[3], $args[4],
$args[5]
);
break;
case 7:
$obj = new $clazz(
$args[0], $args[1], $args[2], $args[3], $args[4],
$args[5], $args[6]
);
break;
case 8:
$obj = new $clazz(
$args[0], $args[1], $args[2], $args[3], $args[4],
$args[5], $args[6], $args[7]
);
break;
case 9:
$obj = new $clazz(
$args[0], $args[1], $args[2], $args[3], $args[4],
$args[5], $args[6], $args[7], $args[8]
);
break;
case 10:
$obj = new $clazz(
$args[0], $args[1], $args[2], $args[3], $args[4],
$args[5], $args[6], $args[7], $args[8], $args[9]
);
break;
default:
// Fall back to using ReflectionClass and curse the developer
// who decided that 11+ args was a reasonable method
// signature.
$ref = new ReflectionClass( $clazz );
$obj = $ref->newInstanceArgs( $args );
}
return $obj;
}
}

View file

@ -20,6 +20,7 @@
* @file
* @ingroup Cache
*/
use Wikimedia\ObjectFactory;
/**
* A cache class that replicates all writes to multiple child caches. Reads

View file

@ -18,6 +18,7 @@
* @file
* @ingroup Cache
*/
use Wikimedia\ObjectFactory;
/**
* A cache class that directs writes to one set of servers and reads to

View file

@ -32,6 +32,7 @@ use Config;
use FauxRequest;
use User;
use WebRequest;
use Wikimedia\ObjectFactory;
/**
* This serves as the entry point to the MediaWiki session handling system.
@ -429,7 +430,7 @@ final class SessionManager implements SessionManagerInterface {
if ( $this->sessionProviders === null ) {
$this->sessionProviders = [];
foreach ( $this->config->get( 'SessionProviders' ) as $spec ) {
$provider = \ObjectFactory::getObjectFromSpec( $spec );
$provider = ObjectFactory::getObjectFromSpec( $spec );
$provider->setLogger( $this->logger );
$provider->setConfig( $this->config );
$provider->setManager( $this );

View file

@ -22,6 +22,7 @@
* @defgroup SpecialPage SpecialPage
*/
use MediaWiki\Linker\LinkRenderer;
use Wikimedia\ObjectFactory;
/**
* Factory for handling the special page list and generating SpecialPage objects.

View file

@ -19,6 +19,7 @@
*
* @file
*/
use Wikimedia\ObjectFactory;
/**
* The CentralIdLookup service allows for connecting local users with

View file

@ -1,5 +1,6 @@
<?php
use Wikimedia\ObjectFactory;
use Wikimedia\TestingAccessWrapper;
/**

View file

@ -1,182 +0,0 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
class ObjectFactoryTest extends PHPUnit_Framework_TestCase {
use MediaWikiCoversValidator;
/**
* @covers ObjectFactory::getObjectFromSpec
*/
public function testClosureExpansionDisabled() {
$obj = ObjectFactory::getObjectFromSpec( [
'class' => ObjectFactoryTestFixture::class,
'args' => [
function () {
return 'wrapped';
},
'unwrapped',
],
'calls' => [
'setter' => [ function () {
return 'wrapped';
}, ],
],
'closure_expansion' => false,
] );
$this->assertInstanceOf( 'Closure', $obj->args[0] );
$this->assertSame( 'wrapped', $obj->args[0]() );
$this->assertSame( 'unwrapped', $obj->args[1] );
$this->assertInstanceOf( 'Closure', $obj->setterArgs[0] );
$this->assertSame( 'wrapped', $obj->setterArgs[0]() );
}
/**
* @covers ObjectFactory::getObjectFromSpec
* @covers ObjectFactory::expandClosures
*/
public function testClosureExpansionEnabled() {
$obj = ObjectFactory::getObjectFromSpec( [
'class' => ObjectFactoryTestFixture::class,
'args' => [
function () {
return 'wrapped';
},
'unwrapped',
],
'calls' => [
'setter' => [ function () {
return 'wrapped';
}, ],
],
'closure_expansion' => true,
] );
$this->assertInternalType( 'string', $obj->args[0] );
$this->assertSame( 'wrapped', $obj->args[0] );
$this->assertSame( 'unwrapped', $obj->args[1] );
$this->assertInternalType( 'string', $obj->setterArgs[0] );
$this->assertSame( 'wrapped', $obj->setterArgs[0] );
$obj = ObjectFactory::getObjectFromSpec( [
'class' => ObjectFactoryTestFixture::class,
'args' => [ function () {
return 'unwrapped';
}, ],
'calls' => [
'setter' => [ function () {
return 'unwrapped';
}, ],
],
] );
$this->assertInternalType( 'string', $obj->args[0] );
$this->assertSame( 'unwrapped', $obj->args[0] );
$this->assertInternalType( 'string', $obj->setterArgs[0] );
$this->assertSame( 'unwrapped', $obj->setterArgs[0] );
}
/**
* @covers ObjectFactory::getObjectFromSpec
*/
public function testGetObjectFromFactory() {
$args = [ 'a', 'b' ];
$obj = ObjectFactory::getObjectFromSpec( [
'factory' => function ( $a, $b ) {
return new ObjectFactoryTestFixture( $a, $b );
},
'args' => $args,
] );
$this->assertSame( $args, $obj->args );
}
/**
* @covers ObjectFactory::getObjectFromSpec
* @expectedException InvalidArgumentException
*/
public function testGetObjectFromInvalid() {
$args = [ 'a', 'b' ];
$obj = ObjectFactory::getObjectFromSpec( [
// Missing 'class' or 'factory'
'args' => $args,
] );
}
/**
* @covers ObjectFactory::getObjectFromSpec
* @dataProvider provideConstructClassInstance
*/
public function testGetObjectFromClass( $args ) {
$obj = ObjectFactory::getObjectFromSpec( [
'class' => ObjectFactoryTestFixture::class,
'args' => $args,
] );
$this->assertSame( $args, $obj->args );
}
/**
* @covers ObjectFactory::constructClassInstance
* @dataProvider provideConstructClassInstance
*/
public function testConstructClassInstance( $args ) {
$obj = ObjectFactory::constructClassInstance(
'ObjectFactoryTestFixture', $args
);
$this->assertSame( $args, $obj->args );
}
public static function provideConstructClassInstance() {
// These args go to 11. I thought about making 10 one louder, but 11!
return [
'0 args' => [ [] ],
'1 args' => [ [ 1, ] ],
'2 args' => [ [ 1, 2, ] ],
'3 args' => [ [ 1, 2, 3, ] ],
'4 args' => [ [ 1, 2, 3, 4, ] ],
'5 args' => [ [ 1, 2, 3, 4, 5, ] ],
'6 args' => [ [ 1, 2, 3, 4, 5, 6, ] ],
'7 args' => [ [ 1, 2, 3, 4, 5, 6, 7, ] ],
'8 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, ] ],
'9 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, ] ],
'10 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ] ],
'11 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ] ],
];
}
/**
* @covers ObjectFactory::constructClassInstance
* @expectedException InvalidArgumentException
*/
public function testNamedArgs() {
$args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
$obj = ObjectFactory::constructClassInstance(
'ObjectFactoryTestFixture', $args
);
}
}
class ObjectFactoryTestFixture {
public $args;
public $setterArgs;
public function __construct( /*...*/ ) {
$this->args = func_get_args();
}
public function setter( /*...*/ ) {
$this->setterArgs = func_get_args();
}
}

View file

@ -3,6 +3,7 @@
use MediaWiki\Auth\AuthManager;
use MediaWiki\MediaWikiServices;
use MediaWiki\Preferences\DefaultPreferencesFactory;
use Wikimedia\ObjectFactory;
use Wikimedia\TestingAccessWrapper;
/**