<?php
+
use MediaWiki\Logger\LegacySpi;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Logger\MonologSpi;
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
+use Wikimedia\TestingAccessWrapper;
/**
* @since 1.18
protected static function resetGlobalServices( Config $bootstrapConfig = null ) {
$oldServices = MediaWikiServices::getInstance();
$oldConfigFactory = $oldServices->getConfigFactory();
+ $oldLoadBalancerFactory = $oldServices->getDBLoadBalancerFactory();
$testConfig = self::makeTestConfig( $bootstrapConfig );
$serviceLocator = MediaWikiServices::getInstance();
self::installTestServices(
$oldConfigFactory,
+ $oldLoadBalancerFactory,
$serviceLocator
);
return $serviceLocator;
/**
* @param ConfigFactory $oldConfigFactory
+ * @param LBFactory $oldLoadBalancerFactory
* @param MediaWikiServices $newServices
*
* @throws MWException
*/
private static function installTestServices(
ConfigFactory $oldConfigFactory,
+ LBFactory $oldLoadBalancerFactory,
MediaWikiServices $newServices
) {
// Use bootstrap config for all configuration.
[ 'main' => $bootstrapConfig ]
)
);
+ $newServices->resetServiceForTesting( 'DBLoadBalancerFactory' );
+ $newServices->redefineService(
+ 'DBLoadBalancerFactory',
+ function ( MediaWikiServices $services ) use ( $oldLoadBalancerFactory ) {
+ return $oldLoadBalancerFactory;
+ }
+ );
}
/**
ConfigFactory $oldFactory,
array $configurations
) {
- return function( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
+ return function ( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
$factory = new ConfigFactory();
// clone configurations from $oldFactory that are not overwritten by $configurations
$GLOBALS[$globalKey] instanceof FauxRequest
) {
$this->mwGlobals[$globalKey] = clone $GLOBALS[$globalKey];
+ } elseif ( $this->containsClosure( $GLOBALS[$globalKey] ) ) {
+ // Serializing Closure only gives a warning on HHVM while
+ // it throws an Exception on Zend.
+ // Workaround for https://github.com/facebook/hhvm/issues/6206
+ $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
} else {
try {
$this->mwGlobals[$globalKey] = unserialize( serialize( $GLOBALS[$globalKey] ) );
}
}
+ /**
+ * @param mixed $var
+ * @param int $maxDepth
+ *
+ * @return bool
+ */
+ private function containsClosure( $var, $maxDepth = 15 ) {
+ if ( $var instanceof Closure ) {
+ return true;
+ }
+ if ( !is_array( $var ) || $maxDepth === 0 ) {
+ return false;
+ }
+
+ foreach ( $var as $value ) {
+ if ( $this->containsClosure( $value, $maxDepth - 1 ) ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
/**
* Merges the given values into a MW global array variable.
* Useful for setting some entries in a configuration array, instead of
$oldInstance = MediaWikiServices::getInstance();
$oldConfigFactory = $oldInstance->getConfigFactory();
+ $oldLoadBalancerFactory = $oldInstance->getDBLoadBalancerFactory();
$testConfig = self::makeTestConfig( null, $configOverrides );
$newInstance = new MediaWikiServices( $testConfig );
self::installTestServices(
$oldConfigFactory,
+ $oldLoadBalancerFactory,
$newInstance
);
MediaWikiServices::forceGlobalInstance( $newInstance );