From 3599ce72b0fe2594bd3ae8c902555040b8a91d28 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Sat, 22 Jun 2019 16:08:07 -0700 Subject: [PATCH] Add Handler::getRouter() Instead of providing the Router as a service, as previously proposed, inject it into the handler via init(). Change-Id: I6008a2c5de692c0d56b7db849b28fd82e0196881 --- includes/Rest/Handler.php | 15 ++++++++++++++- includes/Rest/Router.php | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/includes/Rest/Handler.php b/includes/Rest/Handler.php index 472e1cc367..cee403fa2f 100644 --- a/includes/Rest/Handler.php +++ b/includes/Rest/Handler.php @@ -3,6 +3,9 @@ namespace MediaWiki\Rest; abstract class Handler { + /** @var Router */ + private $router; + /** @var RequestInterface */ private $request; @@ -14,15 +17,25 @@ abstract class Handler { /** * Initialise with dependencies from the Router. This is called after construction. + * @internal */ - public function init( RequestInterface $request, array $config, + public function init( Router $router, RequestInterface $request, array $config, ResponseFactory $responseFactory ) { + $this->router = $router; $this->request = $request; $this->config = $config; $this->responseFactory = $responseFactory; } + /** + * Get the Router. The return type declaration causes it to raise + * a fatal error if init() has not yet been called. + */ + protected function getRouter(): Router { + return $this->router; + } + /** * Get the current request. The return type declaration causes it to raise * a fatal error if init() has not yet been called. diff --git a/includes/Rest/Router.php b/includes/Rest/Router.php index 39bee899c1..279c15e6e7 100644 --- a/includes/Rest/Router.php +++ b/includes/Rest/Router.php @@ -237,8 +237,9 @@ class Router { $spec = $match['userData']; $objectFactorySpec = array_intersect_key( $spec, [ 'factory' => true, 'class' => true, 'args' => true ] ); + /** @var $handler Handler (annotation for PHPStorm) */ $handler = ObjectFactory::getObjectFromSpec( $objectFactorySpec ); - $handler->init( $request, $spec, $this->responseFactory ); + $handler->init( $this, $request, $spec, $this->responseFactory ); try { return $this->executeHandler( $handler ); -- 2.20.1