Add Handler::getRouter()
authorTim Starling <tstarling@wikimedia.org>
Sat, 22 Jun 2019 23:08:07 +0000 (16:08 -0700)
committerTim Starling <tstarling@wikimedia.org>
Wed, 26 Jun 2019 05:06:19 +0000 (15:06 +1000)
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
includes/Rest/Router.php

index 472e1cc..cee403f 100644 (file)
@@ -3,6 +3,9 @@
 namespace MediaWiki\Rest;
 
 abstract class Handler {
 namespace MediaWiki\Rest;
 
 abstract class Handler {
+       /** @var Router */
+       private $router;
+
        /** @var RequestInterface */
        private $request;
 
        /** @var RequestInterface */
        private $request;
 
@@ -14,15 +17,25 @@ abstract class Handler {
 
        /**
         * Initialise with dependencies from the Router. This is called after construction.
 
        /**
         * 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
        ) {
                ResponseFactory $responseFactory
        ) {
+               $this->router = $router;
                $this->request = $request;
                $this->config = $config;
                $this->responseFactory = $responseFactory;
        }
 
                $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.
        /**
         * Get the current request. The return type declaration causes it to raise
         * a fatal error if init() has not yet been called.
index 39bee89..279c15e 100644 (file)
@@ -237,8 +237,9 @@ class Router {
                $spec = $match['userData'];
                $objectFactorySpec = array_intersect_key( $spec,
                        [ 'factory' => true, 'class' => true, 'args' => true ] );
                $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 = ObjectFactory::getObjectFromSpec( $objectFactorySpec );
-               $handler->init( $request, $spec, $this->responseFactory );
+               $handler->init( $this, $request, $spec, $this->responseFactory );
 
                try {
                        return $this->executeHandler( $handler );
 
                try {
                        return $this->executeHandler( $handler );