Support for specifying extra modules as gadget's dependencies

This commit is contained in:
Max Semenik 2010-11-13 18:45:21 +00:00
parent 4b478672bf
commit 7a5af63a10
2 changed files with 40 additions and 6 deletions

View file

@ -1,4 +1,4 @@
<?php
<?php
/**
* Gadgets extension - lets users select custom javascript gadgets
*
@ -192,11 +192,12 @@ class Gadget {
/**
* Increment this when changing class structure
*/
const GADGET_CLASS_VERSION = 1;
const GADGET_CLASS_VERSION = 2;
private $version = self::GADGET_CLASS_VERSION,
$scripts = array(),
$styles = array(),
$dependencies = array(),
$name,
$definition,
$resourceLoaded = false;
@ -218,7 +219,15 @@ class Gadget {
$gadget->definition = $definition;
$params = trim( $m[2], ' []' );
foreach ( preg_split( '/\s*\|\s*/', $params, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
if ( $option == 'ResourceLoader' ) $gadget->resourceLoaded = true;
if ( $option == 'ResourceLoader' ) {
$gadget->resourceLoaded = true;
} elseif ( preg_match( '/dependencies\s*=/', $option ) ) {
$option = preg_replace( '/dependencies\s*=\s*/', '', $option );
$deps = preg_split( '/\s*,\s*/', $option, -1, PREG_SPLIT_NO_EMPTY );
if ( $deps ) {
$gadget->dependencies = $deps;
}
}
}
foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
$page = "Gadget-$page";
@ -323,7 +332,7 @@ class Gadget {
if ( !count( $pages ) ) {
return null;
}
return new GadgetResourceLoaderModule( $pages );
return new GadgetResourceLoaderModule( $pages, $this->dependencies );
}
/**
@ -337,6 +346,14 @@ class Gadget {
return $this->scripts;
}
/**
* Returns names of resources this gadget depends on
* @return Array
*/
public function getDependencies() {
return $this->dependencies;
}
/**
* Loads and returns a list of all gadgets
* @return Mixed: Array of gadgets or false
@ -449,7 +466,7 @@ class Gadget {
* Class representing a list of resources for one gadget
*/
class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
private $pages;
private $pages, $dependencies;
/**
* Creates an instance of this class
@ -459,9 +476,11 @@ class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
* 'Gadget-foo.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
* 'Gadget-foo.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
* )
* @param $dependencies Array: Names of resources this module depends on
*/
public function __construct( $pages ) {
public function __construct( $pages, $dependencies ) {
$this->pages = $pages;
$this->dependencies = $dependencies;
}
/**
@ -471,4 +490,12 @@ class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
protected function getPages( ResourceLoaderContext $context ) {
return $this->pages;
}
/**
* Overrides ResourceLoaderModule::getDependencies()
* @return Array: Names of resources this module depends on
*/
public function getDependencies() {
return $this->dependencies;
}
}

View file

@ -36,4 +36,11 @@ class GadgetsTest extends PHPUnit_Framework_TestCase {
$this->assertTrue( $g->supportsResourceLoader() );
$this->assertEquals(0, count( $g->getLegacyScripts() ) );
}
function testDependencies() {
$g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
$this->assertEquals( array( 'Gadget-bar.js' ), $g->getScripts() );
$this->assertTrue( $g->supportsResourceLoader() );
$this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() );
}
}