Rest: Add Handler::postInitSetup

Overriding init() in a Handler subclass defeats the purpose, which is to
hide the dependency list from handlers so that it can change without
breaking the handlers. If init() was overridable, we could have just
merged it with the constructor.

Make init() final to enforce this. Add postInitSetup() to provide a hook
point which serves SearchHandler's requirements without exposing internal
details.

Change-Id: I91d95ad48b3741ef811ba5517f3a9ef2b435499e
This commit is contained in:
Tim Starling 2020-05-12 11:41:54 +10:00
parent b02b3f9f98
commit 827ab362b2
2 changed files with 11 additions and 18 deletions

View file

@ -43,13 +43,14 @@ abstract class Handler {
* @param array $config
* @param ResponseFactory $responseFactory
*/
public function init( Router $router, RequestInterface $request, array $config,
final public function init( Router $router, RequestInterface $request, array $config,
ResponseFactory $responseFactory
) {
$this->router = $router;
$this->request = $request;
$this->config = $config;
$this->responseFactory = $responseFactory;
$this->postInitSetup();
}
/**
@ -270,6 +271,13 @@ abstract class Handler {
return true;
}
/**
* The handler can override this to do any necessary setup after init()
* is called to inject the dependencies.
*/
protected function postInitSetup() {
}
/**
* Execute the handler. This is called after parameter validation. The
* return value can either be a Response or any type accepted by

View file

@ -10,10 +10,7 @@ use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Rest\Entity\SearchResultPageIdentityValue;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\ResponseFactory;
use MediaWiki\Rest\Router;
use MediaWiki\Search\Entity\SearchResultThumbnail;
use RequestContext;
use SearchEngine;
@ -104,20 +101,8 @@ class SearchHandler extends Handler {
$this->completionCacheExpiry = $config->get( 'SearchSuggestCacheExpiry' );
}
public function init(
Router $router,
RequestInterface $request,
array $config,
ResponseFactory $responseFactory
) {
parent::init(
$router,
$request,
$config,
$responseFactory
);
$this->mode = $config['mode'] ?? self::FULLTEXT_MODE;
protected function postInitSetup() {
$this->mode = $this->getConfig()['mode'] ?? self::FULLTEXT_MODE;
if ( !in_array( $this->mode, self::SUPPORTED_MODES ) ) {
throw new InvalidArgumentException(