Author: bhofmann
Date: Fri Aug 20 08:32:48 2010
New Revision: 987404

URL: http://svn.apache.org/viewvc?rev=987404&view=rev
Log:
PHP: fixed forcedJsLibs

- forcedJsLibs are now again included even when the gadget does not request 
them explicitly
- added config switch forcedAppendJsLibs to force a js lib to be appended at 
the end of inline javascript (useful for overriding some js methods in other 
packages with container specific implementations)
- added unit test for this

Modified:
    shindig/trunk/php/config/container.php
    shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php
    shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php

Modified: shindig/trunk/php/config/container.php
URL: 
http://svn.apache.org/viewvc/shindig/trunk/php/config/container.php?rev=987404&r1=987403&r2=987404&view=diff
==============================================================================
--- shindig/trunk/php/config/container.php (original)
+++ shindig/trunk/php/config/container.php Fri Aug 20 08:32:48 2010
@@ -130,8 +130,14 @@ $shindigConfig = array(
   'jsondb_path' => realpath(dirname(__FILE__) . '/../../content/sampledata') . 
'/canonicaldb.json',
 
   // Force these libraries to be external (included through <script src="..."> 
tags), this way they could be cached by the browser
+  // these libraries will be included regardless of the features the gadget 
requests
+  // example: 'dynamic-height:views' includes the features dynamic-height and 
views
   'forcedJsLibs' => '',
- 
+
+  // Force these js libraries to be appended to each gadget regardless if the 
gadget requested them or not
+  // This can be useful to overwrite existing methods of other javascript 
packages
+  'forcedAppendedJsLibs' => array(),
+
   // After checking the internal __autoload function, shindig can also call 
the 'extension_autoloader' function to load an
   // unknown custom class, this is particuarly useful for when intergrating 
shindig into an existing framework that also depends on autoloading
   'extension_autoloader' => false,

Modified: shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php
URL: 
http://svn.apache.org/viewvc/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php?rev=987404&r1=987403&r2=987404&view=diff
==============================================================================
--- shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php (original)
+++ shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php Fri Aug 20 
08:32:48 2010
@@ -300,12 +300,12 @@ abstract class GadgetBaseRenderer extend
   public function getJavaScripts() {
     $registry = $this->context->getRegistry();
     $forcedJsLibs = $this->getForcedJsLibs();
-    $externFeatures = array();
+    $forcedAppendJsLibs = Config::get('forcedAppendedJsLibs');
+    $externFeatures = $forcedJsLibs;
     $inlineFeatures = array();
     foreach ($this->gadget->features as $feature) {
-      if (in_array($feature, $forcedJsLibs)) {
-        $externFeatures[] = $feature;
-      } else {
+      if (! in_array($feature, $forcedJsLibs) &&
+          ! in_array($feature, $forcedAppendJsLibs)) {
         $inlineFeatures[] = $feature;
       }
     }
@@ -313,7 +313,13 @@ abstract class GadgetBaseRenderer extend
     $sortedInlineFeatures = array();
     $registry->sortFeatures($externFeatures, $sortedExternFeatures);
     $registry->sortFeatures($inlineFeatures, $sortedInlineFeatures);
-    
+
+    // append additional js libs from config to the end of the javascript block
+    // this allows custom overloading of other javascript libraries
+    foreach ($forcedAppendJsLibs as $jsLib) {
+      $sortedInlineFeatures[] = $jsLib;
+    }
+
     // if some of the feature libraries are externalized (through a browser 
cachable <script src="/gadgets/js/opensocial-0.9:settitle.js"> type url)
     // we inject the tag and don't inline those libs (and their dependencies)
     $scripts = array();

Modified: shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php
URL: 
http://svn.apache.org/viewvc/shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php?rev=987404&r1=987403&r2=987404&view=diff
==============================================================================
--- shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php (original)
+++ shindig/trunk/php/test/gadgets/GadgetHtmlRendererTest.php Fri Aug 20 
08:32:48 2010
@@ -29,6 +29,8 @@ class MockHtmlGadgetFactory extends Gadg
   <ModulePrefs title="title">
     <Require feature="opensocial-0.8" />
     <Require feature="dynamic-height" />
+    <Require feature="flash" />
+    <Require feature="minimessage" />
   </ModulePrefs>
   <Content type="html" view="home">
   <![CDATA[
@@ -113,6 +115,57 @@ class GadgetHtmlRendererTest extends PHP
     parent::tearDown();
   }
 
+  public function testGetJavaScriptsExternal() {
+    $oldForcedJsLibs = Config::get('forcedJsLibs');
+    $oldForcedAppendJsLibs = Config::get('forcedAppendedJsLibs');
+    Config::set('forcedJsLibs', 'dynamic-height:views');
+    Config::set('forcedAppendedJsLibs', array('flash'));
+    $this->gadgetHtmlRenderer->dataContext = array(
+        'Msg' => array(
+            'message1' => 'one',
+            'message2' => 'two',
+         ),
+        'UserPrefs' => array(
+            'key1' => 'value1',
+            'key2' => 'value2',
+         ),
+    );
+    $this->gadgetHtmlRenderer->gadget = $this->gadget;
+    $javaScripts = $this->gadgetHtmlRenderer->getJavaScripts();
+    Config::set('forcedJsLibs', $oldForcedJsLibs);
+    Config::set('forcedAppendedJsLibs', $oldForcedAppendJsLibs);
+    $hasExtern = false;
+    $hasInline = false;
+    foreach ($javaScripts as $script) {
+        switch ($script['type']) {
+            case 'extern':
+                if ($hasExtern) {
+                    $this->fail('two entries with script type extern');
+                }
+                $hasExtern = true;
+                $this->assertEquals(0, strpos($script['content'], 
'/gadgets/js/dynamic-height:views.js?'));
+                break;
+            case 'inline':
+                if ($hasInline) {
+                    $this->fail('two entries with script type inline');
+                }
+                //this is from dynamic height and should not be included
+                $this->assertFalse(strpos($script['content'], 
'gadgets.window=gadgets.window||{};'));
+                //minimessage should be included
+                $miniMessagePos = strpos($script['content'], 
'gadgets.MiniMessage=function');
+                $this->assertTrue($miniMessagePos > 0);
+                //we force flash to be appended, so it should be after 
minimessage
+                $this->assertTrue(strpos($script['content'], 
'gadgets.flash=gadgets.flash||{};') > $miniMessagePos);
+                $hasInline = true;
+                break;
+            default:
+                $this->fail('invalid script type ' . $script['type']);
+        }
+    }
+    $this->assertTrue($hasExtern);
+    $this->assertTrue($hasInline);
+  }
+
   /**
    * Tests GadgetHtmlRenderer->renderGadget()
    */


Reply via email to