aboutsummaryrefslogtreecommitdiffstats
path: root/includes/jobqueue/Job.php
diff options
context:
space:
mode:
authorLucas Werkmeister <lucas.werkmeister@wikimedia.de>2017-05-04 18:00:28 +0200
committerLucas Werkmeister <lucas.werkmeister@wikimedia.de>2017-05-09 19:26:13 +0200
commita57252a1f521344e53e8776c34f925f522f512f3 (patch)
tree55e6faf7e1c9aa741417ec7afbd1f03dea0e7e3c /includes/jobqueue/Job.php
parenta559a5e406f40016fd9ec466598668d8adfc398c (diff)
downloadmediawikicore-a57252a1f521344e53e8776c34f925f522f512f3.tar.gz
mediawikicore-a57252a1f521344e53e8776c34f925f522f512f3.zip
Allow callback functions for creating jobs
$wgJobClasses can now specify a factory function for creating a job, instead of a class to be instantiated directly. This makes it possible to inject services in a job constructor, and register a factory function that calls the constructor with default services. This follows Ieb85493a7765 and Ia2107dc5af78, which introduced factory functions for API modules and special pages. Change-Id: I0461e59da2a8fa6681e3b1fcdfc38bfed7f3ac32
Diffstat (limited to 'includes/jobqueue/Job.php')
-rw-r--r--includes/jobqueue/Job.php20
1 files changed, 15 insertions, 5 deletions
diff --git a/includes/jobqueue/Job.php b/includes/jobqueue/Job.php
index f814ceeb1b8b..703e48564bde 100644
--- a/includes/jobqueue/Job.php
+++ b/includes/jobqueue/Job.php
@@ -69,12 +69,22 @@ abstract class Job implements IJobSpecification {
global $wgJobClasses;
if ( isset( $wgJobClasses[$command] ) ) {
- $class = $wgJobClasses[$command];
-
- $job = new $class( $title, $params );
- $job->command = $command;
+ $handler = $wgJobClasses[$command];
+
+ if ( is_callable( $handler ) ) {
+ $job = call_user_func( $handler, $title, $params );
+ } elseif ( class_exists( $handler ) ) {
+ $job = new $handler( $title, $params );
+ } else {
+ $job = null;
+ }
- return $job;
+ if ( $job instanceof Job ) {
+ $job->command = $command;
+ return $job;
+ } else {
+ throw new InvalidArgumentException( "Cannot instantiate job '$command': bad spec!" );
+ }
}
throw new InvalidArgumentException( "Invalid job command '{$command}'" );