resourceloader: Replace ClientHtml 'target' param with 'options' array
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 27 Mar 2018 21:28:03 +0000 (14:28 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 28 Mar 2018 01:17:43 +0000 (18:17 -0700)
In preparation for passing down 'safemode' from OutputPage.

Only used in one place in Wikimedia Git: OutputPage::getRlClient().

Bug: T185303
Change-Id: If01eca96986ff8d7dcdaab6910bf183ba7c7311f

includes/OutputPage.php
includes/resourceloader/ResourceLoaderClientHtml.php
tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php

index 4d6db4c..99dd4a7 100644 (file)
@@ -2788,7 +2788,9 @@ class OutputPage extends ContextSource {
                                $this->rlUserModuleState = $exemptStates['user'] = $userState;
                        }
 
-                       $rlClient = new ResourceLoaderClientHtml( $context, $this->getTarget() );
+                       $rlClient = new ResourceLoaderClientHtml( $context, [
+                               'target' => $this->getTarget(),
+                       ] );
                        $rlClient->setConfig( $this->getJSVars() );
                        $rlClient->setModules( $this->getModules( /*filter*/ true ) );
                        $rlClient->setModuleStyles( $moduleStyles );
index a9e2f92..545fd3b 100644 (file)
@@ -33,8 +33,8 @@ class ResourceLoaderClientHtml {
        /** @var ResourceLoader */
        private $resourceLoader;
 
-       /** @var string|null */
-       private $target;
+       /** @var array */
+       private $options;
 
        /** @var array */
        private $config = [];
@@ -56,12 +56,13 @@ class ResourceLoaderClientHtml {
 
        /**
         * @param ResourceLoaderContext $context
-        * @param string|null $target [optional] Custom 'target' parameter for the startup module
+        * @param array $options [optional] Array of options
+        *  - 'target': Custom parameter passed to StartupModule.
         */
-       public function __construct( ResourceLoaderContext $context, $target = null ) {
+       public function __construct( ResourceLoaderContext $context, array $options = [] ) {
                $this->context = $context;
                $this->resourceLoader = $context->getResourceLoader();
-               $this->target = $target;
+               $this->options = $options;
        }
 
        /**
@@ -309,8 +310,10 @@ class ResourceLoaderClientHtml {
                }
 
                // Async scripts. Once the startup is loaded, inline RLQ scripts will run.
-               // Pass-through a custom target from OutputPage (T143066).
-               $startupQuery = $this->target ? [ 'target' => $this->target ] : [];
+               // Pass-through a custom 'target' from OutputPage (T143066).
+               $startupQuery = isset( $this->options['target'] )
+                       ? [ 'target' => (string)$this->options['target'] ]
+                       : [];
                $chunks[] = $this->getLoad(
                        'startup',
                        ResourceLoaderModule::TYPE_SCRIPTS,
index 4f66bd9..07956f1 100644 (file)
@@ -225,7 +225,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase {
        public function testGetHeadHtmlWithTarget() {
                $client = new ResourceLoaderClientHtml(
                        self::makeContext(),
-                       'example'
+                       [ 'target' => 'example' ]
                );
 
                // phpcs:disable Generic.Files.LineLength
@@ -244,7 +244,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit\Framework\TestCase {
        public function testGetHeadHtmlWithNullTarget() {
                $client = new ResourceLoaderClientHtml(
                        self::makeContext(),
-                       null
+                       [ 'target' => null ]
                );
 
                // phpcs:disable Generic.Files.LineLength