Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316508

Change subject: POC: Support static conditional dependencies in RL modules
......................................................................

POC: Support static conditional dependencies in RL modules

The most comon usage of the ResourceLoaderRegisterModules hook is to
conditionally register ResourceLoader modules based on the existence of
another module/extension. The downside of this is that those module
definitions are split from the other static ones in extension.json, and
are harder to maintain.

This adds a 'registrationDependencies' property to all
ResourceLoaderModule definitions, which is an array name of all the
modules this one depends upon for registration. Note that a
conditionally loaded module cannot depend upon another conditionally
loaded module, mostly to avoid having to implement a cyclical dependency
resolver for this edge case. A structure test should be added to
implement this.

This is still a POC, needs adding to the extension.json schema, tests,
etc.

Bug: T128012
Change-Id: Ia9385f603319e311b6e104da7fb709288eae3595
---
M includes/resourceloader/ResourceLoader.php
1 file changed, 41 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/08/316508/1

diff --git a/includes/resourceloader/ResourceLoader.php 
b/includes/resourceloader/ResourceLoader.php
index 143f5cc..b977d8f 100644
--- a/includes/resourceloader/ResourceLoader.php
+++ b/includes/resourceloader/ResourceLoader.php
@@ -87,6 +87,14 @@
         */
        private $logger;
 
+       /**
+        * Name => definition of modules that
+        * are conditionally registered
+        *
+        * @var array
+        */
+       protected $conditionalModules = [];
+
        /** @var string JavaScript / CSS pragma to disable minification. **/
        const FILTER_NOMIN = '/*@nomin*/';
 
@@ -260,6 +268,8 @@
                        $this->registerTestModules();
                }
 
+               $this->registerConditionalModules();
+
                $this->setMessageBlobStore( new MessageBlobStore( $this, 
$this->logger ) );
        }
 
@@ -343,7 +353,12 @@
                                $this->modules[$name] = $info;
                        } elseif ( is_array( $info ) ) {
                                // New calling convention
-                               $this->moduleInfos[$name] = $info;
+                               if ( isset( $info['registrationDependencies'] ) 
) {
+                                       // Defer, we'll re-evaluate once 
everything else is loaded
+                                       $this->conditionalModules[$name] = 
$info;
+                               } else {
+                                       $this->moduleInfos[$name] = $info;
+                               }
                        } else {
                                throw new MWException(
                                        'ResourceLoader module info type error 
for module \'' . $name .
@@ -391,6 +406,31 @@
 
        }
 
+       private function registerConditionalModules() {
+               if ( !$this->conditionalModules ) {
+                       return;
+               }
+               $knownModules = array_keys( $this->moduleInfos );
+               $toRegister = [];
+               foreach ( $this->conditionalModules as $name => $info ) {
+                       $registrationDependencies = 
$info['registrationDependencies'];
+                       if ( !array_diff( $registrationDependencies, 
$knownModules ) ) {
+                               // All dependencies are satisfied, yay
+                               // Remove the dependencies to register() doesn't
+                               // defer it again.
+                               unset( $info['registrationDependencies'] );
+                               $toRegister[$name] = $info;
+                       } // else we just silently skip since it was conditional
+               }
+
+               // Discard the list of unfulfilled conditional modules
+               $this->conditionalModules = [];
+
+               if ( $toRegister ) {
+                       $this->register( $toRegister );
+               }
+       }
+
        /**
         */
        public function registerTestModules() {

-- 
To view, visit https://gerrit.wikimedia.org/r/316508
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia9385f603319e311b6e104da7fb709288eae3595
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to