wiki.techinc.nl/includes/poolcounter/PoolCounterFactory.php
Timo Tijhof 4e40bcdd8f PoolCounter: Adopt PSR logger in PoolCounter objects
* Inject from ServiceWiring via PoolCounterFactory.

* In production we use PoolCounterClient (which is backed by
  our poolcounterd service), we also offer PoolCounterRedis for
  third parties. Replace the local logger in the Redis variant in
  favour of the built-in one.

* Update PoolCounterWork to adopt this as well. Expose it via getter
  to here, so that DI works all the way, including for the existing
  test.

Bug: T358901
Change-Id: I5413825a0172b186d58e85bbc3cc93697b174c27
2024-03-02 00:41:11 +00:00

63 lines
1.8 KiB
PHP

<?php
namespace MediaWiki\PoolCounter;
use Psr\Log\LoggerInterface;
/**
* @since 1.40
*/
class PoolCounterFactory {
private ?PoolCounterConnectionManager $manager = null;
private ?array $typeConfigs;
private array $clientConf;
private LoggerInterface $logger;
/**
* @internal For use by ServiceWiring
* @param array|null $typeConfigs See $wgPoolCounterConf
* @param array $clientConf See $wgPoolCountClientConf
* @param LoggerInterface $logger
*/
public function __construct( ?array $typeConfigs, array $clientConf, LoggerInterface $logger ) {
$this->typeConfigs = $typeConfigs;
$this->clientConf = $clientConf;
$this->logger = $logger;
}
private function getClientManager(): PoolCounterConnectionManager {
$this->manager ??= new PoolCounterConnectionManager( $this->clientConf );
return $this->manager;
}
/**
* Get a PoolCounter.
*
* @internal This should only be called from PoolCounterWork
* @param string $type The class of actions to limit concurrency for (task type)
* @param string $key
* @return PoolCounter
*/
public function create( string $type, string $key ): PoolCounter {
$conf = $this->typeConfigs[$type] ?? null;
if ( $conf === null ) {
return new PoolCounterNull();
}
$class = $conf['class'] ?? null;
if ( $class === 'PoolCounter_Client' ) {
// Since 1.16: Introduce PoolCounter_Client in PoolCounter extension.
// Since 1.40: Move to core as symbolic name, discourage use of class name.
$class = PoolCounterClient::class;
}
/** @var PoolCounter $poolCounter */
$poolCounter = new $class( $conf, $type, $key );
$poolCounter->setLogger( $this->logger );
// Support subclass for back-compat with the extension
if ( $poolCounter instanceof PoolCounterClient ) {
$poolCounter->setManager( $this->getClientManager() );
}
return $poolCounter;
}
}