jenkins-bot has submitted this change and it was merged.

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.22
M includes/Action.php
2 files changed, 19 insertions(+), 8 deletions(-)

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/RELEASE-NOTES-1.22 b/RELEASE-NOTES-1.22
index e743b48..44b0fae 100644
--- a/RELEASE-NOTES-1.22
+++ b/RELEASE-NOTES-1.22
@@ -151,6 +151,9 @@
 ** editmyoptions controls whether a user may change their preferences.
 * Add new hook AbortTalkPageEmailNotification, this will be used to determine
   whether to send the regular talk page email notification
+* Action classes registered in $wgActions are now also supported in the form of
+  a callback (which returns an instance of Action) instead of providing the 
name
+  of a subclass of Action.
 * (bug 46513) Vector: Add the collapsibleTabs script from the Vector extension.
 * Added $wgRecentChangesFlags for defining new flags for RecentChanges and
   watchlists.
diff --git a/includes/Action.php b/includes/Action.php
index e996104..23b648f 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,18 @@
         *     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 ) ) {
+                       return call_user_func_array( $classOrCallable, array( 
$page, $context ) );
+               }
+
+               return $classOrCallable;
        }
 
        /**
@@ -241,12 +247,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: merged
Gerrit-Change-Id: I6c0f4022f1df1ebaf9cd1a5fe4bd362d0ecc0d62
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: Daniel Friesen <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: IAlex <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: Platonides <[email protected]>
Gerrit-Reviewer: PleaseStand <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to