/** * generates the library string (core:caja:etc.js) including a checksum of all the * javascript content (?v=<md5 of js>) for cache busting * * @param array $features * @return string the list of libraries in core:caja:etc.js?v=checksum> format */ protected function getJsUrl($features) { if (!is_array($features) || !count($features)) { return 'null'; } $registry = $this->context->getRegistry(); // Given the JsServlet automatically expends the js library, we just need // to include the "leaf" nodes of the features. $ret = $features; foreach ($features as $feature) { $depFeatures = $registry->features[$feature]['deps']; $ret = array_diff($ret, $depFeatures); } $ret = implode(':', $ret); $cache = Cache::createCache(Config::get('feature_cache'), 'FeatureCache'); if (($md5 = $cache->get(md5('getJsUrlMD5'))) === false) { $features = $registry->features; // Build a version string from the md5() checksum of all included javascript // to ensure the client always has the right version $inlineJs = ''; foreach ($features as $feature => $content) { $inlineJs .= $registry->getFeatureContent($feature, $this->context, true); } $md5 = md5($inlineJs); $cache->set(md5('getJsUrlMD5'), $md5); } $ret .= ".js?v=" . $md5; return $ret; }
/** * Resolves the Required and Optional features and their dependencies into a real feature list using * the GadgetFeatureRegistry, which can be used to construct the javascript for the gadget * * @param Gadget $gadget */ protected function parseFeatures(Gadget &$gadget) { $found = $missing = array(); if (!$this->context->getRegistry()->resolveFeatures($this->getNeededFeaturesForView($gadget), $found, $missing)) { $requiredMissing = false; foreach ($missing as $featureName) { if (isset($gadget->gadgetSpec->requiredFeatures[$featureName])) { $requiredMissing = true; break; } } if ($requiredMissing) { throw new GadgetException("Unknown features: " . implode(',', $missing)); } } $gadget->features = $found; }
/** * Resolves the Required and Optional features and their dependencies into a real feature list using * the GadgetFeatureRegistry, which can be used to construct the javascript for the gadget * * @param Gadget $gadget */ protected function parseFeatures(Gadget &$gadget) { $found = $missing = array(); if (!$this->context->getRegistry()->resolveFeatures(array_merge($gadget->gadgetSpec->requiredFeatures, $gadget->gadgetSpec->optionalFeatures), $found, $missing)) { $requiredMissing = false; foreach ($missing as $featureName) { if (in_array($featureName, $gadget->gadgetSpec->requiredFeatures)) { $requiredMissing = true; break; } } if ($requiredMissing) { throw new GadgetException("Unknown features: " . implode(',', $missing)); } } unset($gadget->gadgetSpec->optionalFeatures); unset($gadget->gadgetSpec->requiredFeatures); $gadget->features = $found; }
/** * Tests GadgetHtmlRenderer->addHeadTags() */ public function testAddHeadTags() { ob_start(); $this->GadgetHtmlRenderer->renderGadget($this->gadget, $this->view); ob_end_clean(); $this->GadgetHtmlRenderer->addHeadTags($this->domElement, $this->domDocument); $tmpNodeList = $this->domElement->getElementsByTagName("style"); $tmpNodeList = $this->domElement->getElementsByTagName("script"); $script = ''; foreach ($this->GadgetHtmlRenderer->gadget->features as $feature) { $script .= $this->gadgetContext->getRegistry()->getFeatureContent($feature, $this->gadgetContext, true); } foreach ($tmpNodeList as $tmpNode) { $this->assertEquals('text/javascript', $tmpNode->getAttribute('type')); $nodeValue = substr($tmpNode->nodeValue, 0, strpos($tmpNode->nodeValue, 'gadgets.config.init(')); $this->assertEquals(trim($script), trim($nodeValue)); } }
/** * Tests GadgetContext->getRegistry() */ public function testGetRegistry() { $this->assertNotNull($this->GadgetContext->getRegistry()); }