Ejemplo n.º 1
0
 public function getPackageContents($mimeType, $language, $includeSourceMapComment = true)
 {
     if (!Kwf_Assets_BuildCache::getInstance()->building && !Kwf_Config::getValue('assets.lazyBuild')) {
         if (Kwf_Exception_Abstract::isDebug()) {
             //proper error message on development server
             throw new Kwf_Exception("Building assets is disabled (assets.lazyBuild). Please upload build contents.");
         } else {
             throw new Kwf_Exception_NotFound();
         }
     }
     if (!isset($this->_packageContentsCache[$mimeType])) {
         $this->_packageContentsCache[$mimeType] = $this->_buildPackageContents($mimeType);
     }
     $packageMap = $this->_packageContentsCache[$mimeType]['contents'];
     $trlData = $this->_packageContentsCache[$mimeType]['trlData'];
     if (($mimeType == 'text/javascript' || $mimeType == 'text/javascript; defer') && $trlData) {
         $js = "";
         $uniquePrefix = Kwf_Config::getValue('application.uniquePrefix');
         if ($uniquePrefix) {
             $js = "if (typeof window.{$uniquePrefix} == 'undefined') window.{$uniquePrefix} = {};";
             $uniquePrefix = "window.{$uniquePrefix}.";
             $js .= "if (!{$uniquePrefix}_kwfTrlData) {$uniquePrefix}_kwfTrlData={};";
         } else {
             $js .= "if (!window._kwfTrlData) window._kwfTrlData={};";
         }
         foreach ($trlData as $i) {
             $key = $i->type . '.' . $i->source;
             if (isset($i->context)) {
                 $key .= '.' . $i->context;
             }
             $key .= '.' . str_replace("'", "\\'", $i->text);
             $method = $i->type;
             if ($i->source == 'kwf') {
                 $method .= 'Kwf';
             }
             if ($i->type == 'trl') {
                 $trlText = Kwf_Trl::getInstance()->{$method}($i->text, array(), $language);
                 $js .= "{$uniquePrefix}_kwfTrlData['{$key}']='" . str_replace("'", "\\'", $trlText) . "';";
             } else {
                 if ($i->type == 'trlc') {
                     $trlText = Kwf_Trl::getInstance()->{$method}($i->context, $i->text, array(), $language);
                     $js .= "{$uniquePrefix}_kwfTrlData['{$key}']='" . str_replace("'", "\\'", $trlText) . "';";
                 } else {
                     if ($i->type == 'trlp') {
                         $trlText = Kwf_Trl::getInstance()->getTrlpValues(null, $i->text, $i->plural, $i->source, $language);
                         $js .= "{$uniquePrefix}_kwfTrlData['{$key}']='" . str_replace("'", "\\'", $trlText['single']) . "';";
                         $js .= "{$uniquePrefix}_kwfTrlData['{$key}.plural']='" . str_replace("'", "\\'", $trlText['plural']) . "';";
                     } else {
                         if ($i->type == 'trlcp') {
                             $trlText = Kwf_Trl::getInstance()->getTrlpValues($i->context, $i->text, $i->plural, $i->source, $language);
                             $js .= "{$uniquePrefix}_kwfTrlData['{$key}']='" . str_replace("'", "\\'", $trlText['single']) . "';";
                             $js .= "{$uniquePrefix}_kwfTrlData['{$key}.plural']='" . str_replace("'", "\\'", $trlText['plural']) . "';";
                         } else {
                             throw new Kwf_Exception("Unknown trl type");
                         }
                     }
                 }
             }
         }
         $map = Kwf_SourceMaps_SourceMap::createEmptyMap($js . "\n");
         $map->concat($packageMap);
         $packageMap = $map;
     }
     if ($mimeType == 'text/javascript') {
         $ext = 'js';
     } else {
         if ($mimeType == 'text/javascript; defer') {
             $ext = 'defer.js';
         } else {
             if ($mimeType == 'text/css') {
                 $ext = 'css';
             } else {
                 if ($mimeType == 'text/css; ie8') {
                     $ext = 'ie8.css';
                 } else {
                     throw new Kwf_Exception("Invalid mimeType: '{$mimeType}'");
                 }
             }
         }
     }
     $packageMap->setFile($this->getPackageUrl($ext, $language));
     return $packageMap;
 }
Ejemplo n.º 2
0
 public function getPackageContents($mimeType, $language, $includeSourceMapComment = true)
 {
     if (!Kwf_Assets_BuildCache::getInstance()->building && !Kwf_Config::getValue('assets.lazyBuild')) {
         if (Kwf_Exception_Abstract::isDebug()) {
             //proper error message on development server
             throw new Kwf_Exception("Building assets is disabled (assets.lazyBuild). Please upload build contents.");
         } else {
             throw new Kwf_Exception_NotFound();
         }
     }
     $packageMap = Kwf_SourceMaps_SourceMap::createEmptyMap('');
     if ($mimeType == 'text/javascript') {
         $ext = 'js';
     } else {
         if ($mimeType == 'text/javascript; defer') {
             $ext = 'defer.js';
         } else {
             if ($mimeType == 'text/css') {
                 $ext = 'css';
             } else {
                 if ($mimeType == 'text/css; media=print') {
                     $ext = 'printcss';
                 }
             }
         }
     }
     $packageMap->setFile($this->getPackageUrl($ext, $language));
     $maxMTime = 0;
     $commonJsData = array();
     $commonJsDeps = array();
     foreach ($this->_getFilteredUniqueDependencies($mimeType) as $i) {
         if ($i->getIncludeInPackage()) {
             if (($mimeType == 'text/javascript' || $mimeType == 'text/javascript') && $i->isCommonJsEntry()) {
                 $c = $i->getContentsPacked($language)->getFileContentsInlineMap(false);
                 $commonJsDeps = $this->_getCommonJsDeps($i, $language);
                 $commonJsData[$i->__toString()] = array('id' => $i->__toString(), 'source' => $c, 'sourceFile' => $i->__toString(), 'deps' => $commonJsDeps['deps'], 'entry' => true);
                 foreach ($commonJsDeps['data'] as $k => $j) {
                     if (!isset($commonJsData[$k])) {
                         $commonJsData[$k] = $j;
                     }
                 }
             } else {
                 $map = $i->getContentsPacked($language);
                 if (strpos($map->getFileContents(), "//@ sourceMappingURL=") !== false && strpos($map->getFileContents(), "//# sourceMappingURL=") !== false) {
                     throw new Kwf_Exception("contents must not contain sourceMappingURL");
                 }
                 foreach ($map->getMapContentsData(false)->sources as &$s) {
                     $s = '/assets/' . $s;
                 }
                 // $ret .= "/* *** $i */\n"; // attention: commenting this in breaks source maps
                 $packageMap->concat($map);
             }
         }
         $mTime = $i->getMTime();
         if ($mTime) {
             $maxMTime = max($maxMTime, $mTime);
         }
     }
     if ($commonJsData) {
         if ($mimeType == 'text/javascript; defer') {
             //in defer.js don't include deps that are already loaded in non-defer
             foreach ($this->_getFilteredUniqueDependencies('text/javascript') as $i) {
                 $commonJsDeps = $this->_getCommonJsDeps($i, $language);
                 foreach (array_keys($commonJsDeps['data']) as $key) {
                     if (isset($commonJsData[$key])) {
                         unset($commonJsData[$key]);
                     }
                 }
                 $c = $i->getContentsPacked($language)->getFileContentsInlineMap(false);
                 $commonJsData[$i->__toString()] = array('id' => $i->__toString(), 'source' => $c, 'sourceFile' => $i->__toString(), 'deps' => $commonJsDeps['deps'], 'entry' => true);
             }
         }
         $contents = 'window.require = ' . Kwf_Assets_CommonJs_BrowserPack::pack(array_values($commonJsData));
         $map = Kwf_SourceMaps_SourceMap::createFromInline($contents);
         $packageMap->concat($map);
     }
     if ($mimeType == 'text/javascript' || $mimeType == 'text/javascript; defer') {
         if ($uniquePrefix = Kwf_Config::getValue('application.uniquePrefix')) {
             $packageMap = Kwf_Assets_Package_Filter_UniquePrefix::filter($packageMap, $uniquePrefix);
         }
     }
     if ($includeSourceMapComment) {
         $contents = $packageMap->getFileContents();
         if ($mimeType == 'text/javascript') {
             $ext = 'js';
         } else {
             if ($mimeType == 'text/javascript; defer') {
                 $ext = 'defer.js';
             } else {
                 if ($mimeType == 'text/css') {
                     $ext = 'css';
                 } else {
                     if ($mimeType == 'text/css; media=print') {
                         $ext = 'printcss';
                     } else {
                         throw new Kwf_Exception_NotYetImplemented();
                     }
                 }
             }
         }
         if ($ext == 'js' || $ext == 'defer.js') {
             $contents .= "\n//# sourceMappingURL=" . $this->getPackageUrl($ext . '.map', $language) . "\n";
         } else {
             if ($ext == 'css' || $ext == 'printcss') {
                 $contents .= "\n/*# sourceMappingURL=" . $this->getPackageUrl($ext . '.map', $language) . " */\n";
             }
         }
         $packageMap->setFileContents($contents);
     }
     return $packageMap;
 }