Author: bhofmann
Date: Fri Jan 14 14:27:26 2011
New Revision: 1059017

URL: http://svn.apache.org/viewvc?rev=1059017&view=rev
Log:
PHP: added additional Substitution of Preloads, DataPipeLining URLs and Proxied 
Content URLs

Modified:
    shindig/trunk/php/src/gadgets/GadgetFactory.php
    shindig/trunk/php/src/gadgets/Substitutions.php
    shindig/trunk/php/src/gadgets/render/GadgetHrefRenderer.php

Modified: shindig/trunk/php/src/gadgets/GadgetFactory.php
URL: 
http://svn.apache.org/viewvc/shindig/trunk/php/src/gadgets/GadgetFactory.php?rev=1059017&r1=1059016&r2=1059017&view=diff
==============================================================================
--- shindig/trunk/php/src/gadgets/GadgetFactory.php (original)
+++ shindig/trunk/php/src/gadgets/GadgetFactory.php Fri Jan 14 14:27:26 2011
@@ -38,7 +38,7 @@ class GadgetFactory {
   /**
    * Returns the processed gadget spec
    *
-   * @return GadgetSpec
+   * @return Gadget
    */
   public function createGadget() {
     $gadgetUrl = $this->context->getUrl();
@@ -54,10 +54,10 @@ class GadgetFactory {
     $gadget = new $gadgetClass($gadgetSpec, $this->context);
 
     // Process the gadget: fetching remote resources, processing & applying 
the correct translations, user prefs and feature resolving
+    $this->addSubstitutions($gadget);
     $this->fetchResources($gadget);
     $this->mergeLocales($gadget);
     $this->parseUserPrefs($gadget);
-    $this->addSubstitutions($gadget);
     $this->applySubstitutions($gadget);
     $this->parseFeatures($gadget);
     return $gadget;
@@ -93,10 +93,15 @@ class GadgetFactory {
   /**
    * Applies the substitutions to the complex types (preloads, user prefs, 
etc). Simple
    * types (author, title, etc) are translated on the fly in the gadget's 
getFoo() functions
+   *
+   * @param Gadget $gadget
    */
   protected function applySubstitutions(Gadget &$gadget) {
     // Apply the substitutions to the UserPrefs
     foreach ($gadget->gadgetSpec->userPrefs as $key => $pref) {
+
+      $gadget->substitutions->addSubstitution('UP', 
$gadget->substitutions->substitute($pref['name']), 
$gadget->substitutions->substitute($pref['value']));
+
       $gadget->gadgetSpec->userPrefs[$key]['name'] = 
$gadget->substitutions->substitute($pref['name']);
       $gadget->gadgetSpec->userPrefs[$key]['displayName'] = 
$gadget->substitutions->substitute($pref['displayName']);
       $gadget->gadgetSpec->userPrefs[$key]['required'] = 
$gadget->substitutions->substitute($pref['required']);
@@ -110,6 +115,7 @@ class GadgetFactory {
         }
       }
     }
+
     // Apply substitutions to the preloads
     foreach ($gadget->gadgetSpec->preloads as $key => $preload) {
       $gadget->gadgetSpec->preloads[$key]['body'] = 
$gadget->substitutions->substitute($preload['body']);
@@ -118,6 +124,8 @@ class GadgetFactory {
 
   /**
    * Seeds the substitutions class with the user prefs, messages, bidi and 
module id
+   *
+   * @param Gadget $gadget
    */
   protected function addSubstitutions(Gadget &$gadget) {
     $substiutionClass = Config::get('substitution_class');
@@ -134,9 +142,6 @@ class GadgetFactory {
     $gadget->substitutions->addSubstitution('BIDI', "END_EDGE", 
$gadget->rightToLeft ? "left" : "right");
     $gadget->substitutions->addSubstitution('BIDI', "DIR", 
$gadget->rightToLeft ? "rtl" : "ltr");
     $gadget->substitutions->addSubstitution('BIDI', "REVERSE_DIR", 
$gadget->rightToLeft ? "ltr" : "rtl");
-    foreach ($gadget->gadgetSpec->userPrefs as $pref) {
-      $gadget->substitutions->addSubstitution('UP', 
$gadget->substitutions->substitute($pref['name']), 
$gadget->substitutions->substitute($pref['value']));
-    }
   }
 
   /**
@@ -191,7 +196,6 @@ class GadgetFactory {
    * be reduced to only the GadgetContext->getLocale matching entries.
    *
    * @param Gadget $gadget
-   * @param GadgetContext $context
    */
   protected function fetchResources(Gadget &$gadget) {
     $contextLocale = $this->context->getLocale();
@@ -206,6 +210,7 @@ class GadgetFactory {
              unset($gadget->gadgetSpec->locales[$key]);
              continue;
           } else {
+            $transformedUrl = 
$gadget->substitutions->substitute($transformedUrl);
             $gadget->gadgetSpec->locales[$key]['messages'] = $transformedUrl;
            }
           // locale matches the current context, add it to the requests queue
@@ -223,6 +228,7 @@ class GadgetFactory {
       // Add preloads to the request queue
       foreach ($gadget->getPreloads() as $preload) {
         if (! empty($preload['href'])) {
+          $preload['href'] = 
$gadget->substitutions->substitute($preload['href']);
           $request = new RemoteContentRequest($preload['href']);
           if (! empty($preload['authz']) && $preload['authz'] == 'SIGNED') {
             if ($this->token == '') {
@@ -250,7 +256,8 @@ class GadgetFactory {
       // Add template libraries to the request queue
       if ($gadget->gadgetSpec->templatesRequireLibraries) {
         foreach ($gadget->gadgetSpec->templatesRequireLibraries as $key => 
$libraryUrl) {
-               $request = new RemoteContentRequest($libraryUrl);
+          $gadget->gadgetSpec->templatesRequireLibraries[$key] = $libraryUrl = 
$gadget->substitutions->substitute($libraryUrl);
+          $request = new RemoteContentRequest($libraryUrl);
           $transformedUrl = 
RemoteContentRequest::transformRelativeUrl($libraryUrl, 
$this->context->getUrl());
           if (! $transformedUrl) {
             continue;
@@ -340,7 +347,7 @@ class GadgetFactory {
   /**
    * Fetches the gadget xml for the requested URL using the http fetcher
    *
-   * @param unknown_type $gadgetUrl
+   * @param string $gadgetUrl
    * @return string gadget's xml content
    */
   protected function fetchGadget($gadgetUrl) {

Modified: shindig/trunk/php/src/gadgets/Substitutions.php
URL: 
http://svn.apache.org/viewvc/shindig/trunk/php/src/gadgets/Substitutions.php?rev=1059017&r1=1059016&r2=1059017&view=diff
==============================================================================
--- shindig/trunk/php/src/gadgets/Substitutions.php (original)
+++ shindig/trunk/php/src/gadgets/Substitutions.php Fri Jan 14 14:27:26 2011
@@ -19,8 +19,14 @@
  */
 
 class Substitutions {
+  /**
+   * @var array
+   */
   protected $types = array('MESSAGE' => 'MSG', 'BIDI' => 'BIDI', 'USER_PREF' 
=> 'UP', 'MODULE' => 'MODULE');
-  
+
+  /**
+   * @var array
+   */
   protected $substitutions = array();
 
   public function __construct() {
@@ -29,16 +35,32 @@ class Substitutions {
     }
   }
 
+  /**
+   *
+   * @param string $type
+   * @param string $key
+   * @param string $value
+   */
   public function addSubstitution($type, $key, $value) {
     $this->substitutions[$type]["__{$type}_{$key}__"] = $value;
   }
 
+  /**
+   *
+   * @param string $type
+   * @param array $array
+   */
   public function addSubstitutions($type, $array) {
     foreach ($array as $key => $value) {
       $this->addSubstitution($type, $key, $value);
     }
   }
 
+  /**
+   *
+   * @param string $input
+   * @return string
+   */
   public function substitute($input) {
     foreach ($this->types as $type) {
       $input = $this->substituteType($type, $input);
@@ -46,18 +68,26 @@ class Substitutions {
     return $input;
   }
 
+  /**
+   *
+   * @param string $type
+   * @param string $input
+   * @return string
+   */
   public function substituteType($type, $input) {
-    if (empty($this->substitutions[$type])) {
-      return $input;
+    foreach ($this->substitutions[$type] as $key => $value) {
+      if (! is_array($value)) {
+        $input = str_replace($key, $value, $input);
+      }
     }
-    return str_replace(array_keys($this->substitutions[$type]), 
array_values($this->substitutions[$type]), $input);
+    return $input;
   }
 
   /**
    * Substitutes a uri
-   * @param type The type to substitute, or null for all types.
-   * @param uri
-   * @return The substituted uri, or a dummy value if the result is invalid.
+   * @param string $type The type to substitute, or null for all types.
+   * @param string $uri
+   * @return string The substituted uri, or a dummy value if the result is 
invalid.
    */
   public function substituteUri($type, $uri) {
     if (empty($uri)) {

Modified: shindig/trunk/php/src/gadgets/render/GadgetHrefRenderer.php
URL: 
http://svn.apache.org/viewvc/shindig/trunk/php/src/gadgets/render/GadgetHrefRenderer.php?rev=1059017&r1=1059016&r2=1059017&view=diff
==============================================================================
--- shindig/trunk/php/src/gadgets/render/GadgetHrefRenderer.php (original)
+++ shindig/trunk/php/src/gadgets/render/GadgetHrefRenderer.php Fri Jan 14 
14:27:26 2011
@@ -61,7 +61,7 @@ class GadgetHrefRenderer extends GadgetB
     */
     $authz = $this->getAuthz($view);
     $refreshInterval = $this->getRefreshInterval($view);
-    $href = $this->buildHref($view, $authz);
+    $href = $this->buildHref($view, $authz, $gadget);
     if (count($view['dataPipelining'])) {
       $request = new RemoteContentRequest($href, "Content-type: 
application/json\n");
       $request->setMethod('POST');
@@ -91,11 +91,18 @@ class GadgetHrefRenderer extends GadgetB
     $basicRemoteContent->setCachePostRequest(true);
     if (($response = $basicRemoteContent->getCachedRequest($request)) == 
false) {
       // Don't fetch the data-pipelining social data unless we don't have a 
cached version of the gadget's content
-      $dataPipeliningResults = DataPipelining::fetch($view['dataPipelining'], 
$this->context);
-      // spec stats that the proxied content data-pipelinging data is *not* 
available to templates (to avoid duplicate posting
-      // of the data to the gadget dev's server and once to js space), so we 
don't assign it to the data context, and just
-      // post the json encoded results to the remote url.
-      $request->setPostBody(json_encode($dataPipeliningResults));
+      if (isset($view['dataPipelining']) && is_array($view['dataPipelining'])) 
{
+        foreach ($view['dataPipelining'] as &$pipeliningRequest) {
+          if (isset($pipeliningRequest['href'])) {
+            $pipeliningRequest['href'] = 
$gadget->substitutions->substituteUri(null, $pipeliningRequest['href']);
+          }
+        }
+        $dataPipeliningResults = 
DataPipelining::fetch($view['dataPipelining'], $this->context);
+        // spec stats that the proxied content data-pipelinging data is *not* 
available to templates (to avoid duplicate posting
+        // of the data to the gadget dev's server and once to js space), so we 
don't assign it to the data context, and just
+        // post the json encoded results to the remote url.
+        $request->setPostBody(json_encode($dataPipeliningResults));
+      }
       $response = $basicRemoteContent->fetch($request);
     }
     if ($response->getHttpCode() != '200') {
@@ -117,10 +124,11 @@ class GadgetHrefRenderer extends GadgetB
    *
    * @param array $view
    * @param SecurityToken $token
+   * @param Gadget $gadget
    * @return string the url
    */
-  private function buildHref($view, $authz) {
-    $href = RemoteContentRequest::transformRelativeUrl($view['href'], 
$this->context->getUrl());
+  private function buildHref($view, $authz, $gadget) {
+    $href = 
RemoteContentRequest::transformRelativeUrl($gadget->substitutions->substituteUri(null,
 $view['href']), $this->context->getUrl());
     if (empty($href)) {
       throw new Exception("Invalid empty href in the gadget view");
     } // add the required country and lang param to the URL


Reply via email to