ResourceLoaderImage: Allow shorthand syntax
authorBartosz Dziewoński <matma.rex@gmail.com>
Thu, 2 Apr 2015 17:54:07 +0000 (19:54 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Thu, 2 Apr 2015 19:27:06 +0000 (21:27 +0200)
array( "en,de,fr" => "foo.svg" ) now means the same as
array( "en" => "foo.svg", "de" => "foo.svg", "fr" => "foo.svg" ).

Bug: T76539
Change-Id: I0bf82e06be3c5f94b6ac88bbc0437b5229ceb284

includes/resourceloader/ResourceLoaderImage.php
tests/phpunit/includes/resourceloader/ResourceLoaderImageModuleTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderImageTest.php

index d14b7a8..12d1e82 100644 (file)
@@ -53,6 +53,20 @@ class ResourceLoaderImage {
                $this->basePath = $basePath;
                $this->variants = $variants;
 
+               // Expand shorthands:
+               // array( "en,de,fr" => "foo.svg" ) → array( "en" => "foo.svg", "de" => "foo.svg", "fr" => "foo.svg" )
+               if ( is_array( $this->descriptor ) && isset( $this->descriptor['lang'] ) ) {
+                       foreach ( array_keys( $this->descriptor['lang'] ) as $langList ) {
+                               if ( strpos( $langList, ',' ) !== false ) {
+                                       $this->descriptor['lang'] += array_fill_keys(
+                                               explode( ',', $langList ),
+                                               $this->descriptor['lang'][ $langList ]
+                                       );
+                                       unset( $this->descriptor['lang'][ $langList ] );
+                               }
+                       }
+               }
+
                // Ensure that all files have common extension.
                $extensions = array();
                $descriptor = (array)$descriptor;
index 8410663..d0bc210 100644 (file)
@@ -31,7 +31,7 @@ class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase {
                                'default' => 'bold-a.svg',
                                'lang' => array(
                                        'en' => 'bold-b.svg',
-                                       'de' => 'bold-f.svg',
+                                       'ar,de' => 'bold-f.svg',
                                )
                        ),
                )
index 404ae9d..758cfe1 100644 (file)
@@ -33,6 +33,7 @@ class ResourceLoaderImageTest extends ResourceLoaderTestCase {
                        array( 'help', 'he', 'help-ltr.svg' ),
                        array( 'bold', 'en', 'bold-b.svg' ),
                        array( 'bold', 'de', 'bold-f.svg' ),
+                       array( 'bold', 'ar', 'bold-f.svg' ),
                        array( 'bold', 'fr', 'bold-a.svg' ),
                        array( 'bold', 'he', 'bold-a.svg' ),
                );