Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Allow registration of Actions using a callback that returns an 
Action instance
......................................................................

Allow registration of Actions using a callback that returns an Action instance

Basically implementing what Brion suggested on wikitech

This allows for injecting dependencies while still only loading the actual 
class when needed.

Simple example:

$wgActions['epundo'] = function( Page $page, IContextSource $context = null ) 
use ( $differ ) {
        $undoAction = new \EducationProgram\UndoAction( $page, $context );
        $undoAction->setDiffer( $differ );
        return $undoAction;
};

Change-Id: I6c0f4022f1df1ebaf9cd1a5fe4bd362d0ecc0d62
---
M RELEASE-NOTES-1.21
M includes/Action.php
2 files changed, 24 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/85/49785/1

diff --git a/RELEASE-NOTES-1.21 b/RELEASE-NOTES-1.21
index 7e06218..fa0a7bc 100644
--- a/RELEASE-NOTES-1.21
+++ b/RELEASE-NOTES-1.21
@@ -90,6 +90,8 @@
 * Allow memory of shell subprocesses to be limited using Linux cgroups
   instead of ulimit -v, which tends to cause deadlocks in recent versions
   of ImageMagick. Configurable with $wgShellCgroup.
+* Actions modules can now be registered using a callback that returns an 
instance
+  of Action instead of providing a class name.
 * Added $wgWhitelistReadRegexp for regex whitelisting.
 * (bug 5346) Categories that are redirects will be displayed italic in
   the category links section at the bottom of a page.
diff --git a/includes/Action.php b/includes/Action.php
index 0bc5ba2..911572c 100644
--- a/includes/Action.php
+++ b/includes/Action.php
@@ -59,7 +59,7 @@
         * the action is disabled, or null if it's not recognised
         * @param $action String
         * @param $overrides Array
-        * @return bool|null|string
+        * @return bool|null|string|callable
         */
        final private static function getClass( $action, array $overrides ) {
                global $wgActions;
@@ -89,12 +89,24 @@
         *     if it is not recognised
         */
        final public static function factory( $action, Page $page, 
IContextSource $context = null ) {
-               $class = self::getClass( $action, $page->getActionOverrides() );
-               if ( $class ) {
-                       $obj = new $class( $page, $context );
+               $classOrCallable = self::getClass( $action, 
$page->getActionOverrides() );
+
+               if ( is_string( $classOrCallable ) ) {
+                       $obj = new $classOrCallable( $page, $context );
                        return $obj;
                }
-               return $class;
+
+               if ( is_callable( $classOrCallable ) ) {
+                       $instance = call_user_func_array( $classOrCallable, 
array( $page, $context ) );
+
+                       if ( !( $instance instanceof Action ) ) {
+                               throw new MWException( 'Callbacks registered 
for actions should return an Action instance' );
+                       }
+
+                       return $instance;
+               }
+
+               return $classOrCallable;
        }
 
        /**
@@ -241,12 +253,14 @@
        }
 
        /**
-        * Protected constructor: use Action::factory( $action, $page ) to 
actually build
-        * these things in the real world
+        * Constructor.
+        *
+        * Only public since 1.21
+        *
         * @param $page Page
         * @param $context IContextSource
         */
-       protected function __construct( Page $page, IContextSource $context = 
null ) {
+       public function __construct( Page $page, IContextSource $context = null 
) {
                $this->page = $page;
                $this->context = $context;
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6c0f4022f1df1ebaf9cd1a5fe4bd362d0ecc0d62
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to