/** * Returns the URL for an image in the current theme. If not found in the current theme, will revert * to looking in the base theme. * * @param string $jsFileName js file name * @param bool $returnURL if true, returns URL with unique image mark, otherwise returns path to the file * @return string path to js file */ public function getJSURL($jsFileName, $returnURL = true) { if (isset($this->_jsCache[$jsFileName]) && sugar_is_file(sugar_cached($this->_jsCache[$jsFileName]))) { if ($returnURL) { return getJSPath("cache/" . $this->_jsCache[$jsFileName]); } else { return sugar_cached($this->_jsCache[$jsFileName]); } } $jsFileContents = ''; $fullFileName = $this->getJSPath() . '/' . $jsFileName; $defaultFileName = $this->getDefaultJSPath() . '/' . $jsFileName; if (isset($this->parentTheme) && SugarThemeRegistry::get($this->parentTheme) instanceof SugarTheme && ($filename = SugarThemeRegistry::get($this->parentTheme)->getJSURL($jsFileName, false)) != '' && !in_array($jsFileName, $this->ignoreParentFiles)) { $jsFileContents .= file_get_contents($filename); } else { if (sugar_is_file($defaultFileName)) { $jsFileContents .= file_get_contents($defaultFileName); } if (sugar_is_file('custom/' . $defaultFileName)) { $jsFileContents .= file_get_contents('custom/' . $defaultFileName); } } if (sugar_is_file($fullFileName)) { $jsFileContents .= file_get_contents($fullFileName); } if (sugar_is_file('custom/' . $fullFileName)) { $jsFileContents .= file_get_contents('custom/' . $fullFileName); } if (empty($jsFileContents)) { $GLOBALS['log']->warn("Javascript File {$jsFileName} not found"); return false; } // create the cached file location $jsFilePath = create_cache_directory($fullFileName); // minify the js if (!inDeveloperMode() && !sugar_is_file(str_replace('.js', '-min.js', $jsFilePath))) { $jsFileContents = SugarMin::minify($jsFileContents); $jsFilePath = str_replace('.js', '-min.js', $jsFilePath); $fullFileName = str_replace('.js', '-min.js', $fullFileName); } // now write the js to cache sugar_file_put_contents($jsFilePath, $jsFileContents); $this->_jsCache[$jsFileName] = $fullFileName; if ($returnURL) { return getJSPath("cache/" . $fullFileName); } return sugar_cached($fullFileName); }
/** * Builds the javascript file used by the clients * * @param array $data The metadata to build from * @param boolean $onlyReturnModuleComponents Indicator to return only module * components * @return string A url to the file that was just built */ protected function buildJavascriptComponentFile(&$data, $onlyReturnModuleComponents = false) { $platform = $this->platforms[0]; $js = "(function(app) {\n SUGAR.jssource = {"; $compJS = $this->buildJavascriptComponentSection($data); if (!$onlyReturnModuleComponents) { $js .= $compJS; } if (!empty($data['modules'])) { if (!empty($compJS) && !$onlyReturnModuleComponents) { $js .= ","; } $js .= "\n\t\"modules\":{"; $allModuleJS = ''; //Grab the keys this way rather than through $key => $value to preserve pass by reference for $data $modules = array_keys($data['modules']); foreach ($modules as $module) { $moduleJS = $this->buildJavascriptComponentSection($data['modules'][$module], true); if (!empty($moduleJS)) { $allModuleJS .= ",\n\t\t\"{$module}\":{{$moduleJS}}"; } } //Chop off the first comma in $allModuleJS $js .= substr($allModuleJS, 1); $js .= "\n\t}"; } $js .= "}})(SUGAR.App);"; $hash = md5($js); //If we are going to be using uglify to minify our JS, we should minify the entire file rather than each component separately. if (!inDeveloperMode() && SugarMin::isMinifyFast()) { $js = SugarMin::minify($js); } $path = "cache/javascript/{$platform}/components_{$hash}.js"; if (!file_exists($path)) { mkdir_recursive(dirname($path)); sugar_file_put_contents_atomic($path, $js); } return $this->getUrlForCacheFile($path); }
/** * @dataProvider minifyProvider */ public function testMinify($unminified, $minified) { require_once 'jssource/jsmin.php'; $this->assertEquals(SugarMin::minify($unminified), $minified); }
function CompressFiles($from_path, $to_path) { if (!defined('JSMIN_AS_LIB')) { define('JSMIN_AS_LIB', true); } //assumes jsmin.php is in same directory if (isset($_REQUEST['root_directory']) || defined('INSTANCE_PATH')) { require_once 'jssource/jsmin.php'; } else { require_once 'jsmin.php'; } $nl = ' '; //check to make sure from path and to path are not empty if (isset($from_path) && !empty($from_path) && isset($to_path) && !empty($to_path)) { $lic_str = ''; $ReadNextLine = true; // Output a minified version of example.js. if (file_exists($from_path) && is_file($from_path)) { //read in license script if (function_exists('sugar_fopen')) { $file_handle = sugar_fopen($from_path, 'r'); } else { $file_handle = fopen($from_path, 'r'); } if ($file_handle) { $beg = false; //Read the file until you hit a line with code. This is meant to retrieve //the initial license string found in the beginning comments of js code. while (!feof($file_handle) && $ReadNextLine) { $newLine = fgets($file_handle, 4096); $newLine = trim($newLine); //See if line contains open or closing comments //if opening comments are found, set $beg to true if (strpos($newLine, '/*') !== false) { $beg = true; } //if closing comments are found, set $beg to false if (strpos($newLine, '*/') !== false) { $beg = false; } //if line is not empty (has code) set the boolean to false if (!empty($newLine)) { $ReadNextLine = false; } //If we are in a comment block, then set boolean back to true if ($beg) { $ReadNextLine = true; //add new line to license string $lic_str .= trim($newLine) . $nl; } else { //if we are here it means that uncommented and non blank line has been reached //Check to see that ReadNextLine is true, if so then add the last line collected //make sure the last line is either the end to a comment block, or starts with '//' //else do not add as it is live code. if (!empty($newLine) && (strpos($newLine, '*/') !== false || $newLine[0] . $newLine[1] == '//')) { //add new line to license string $lic_str .= $newLine; } //set to false because $beg is false, which means the comment block has ended $ReadNextLine = false; } } } if ($file_handle) { fclose($file_handle); } //place license string into array for use with jsmin file. //this will preserve the license in the file $lic_arr = array($lic_str); //minify javascript //$jMin = new JSMin($from_path,$to_path,$lic_arr); $min_file = str_replace('.js', '-min.js', $from_path); if (strpos($from_path, '-min.js') !== FALSE) { $min_file = $from_path; } if (is_file($min_file)) { $out = file_get_contents($min_file); } else { $out = $lic_str . SugarMin::minify(file_get_contents($from_path)); } if (function_exists('sugar_fopen') && ($fh = @sugar_fopen($to_path, 'w'))) { fputs($fh, $out); fclose($fh); } else { file_put_contents($to_path, $out); } } else { //log failure echo "<B> COULD NOT COMPRESS {$from_path}, it is not a file \n"; } } else { //log failure echo "<B> COULD NOT COMPRESS {$from_path}, missing variables \n"; } }
/** * Calls the SugarMin minify function. * * @param string $js Javascript to be minified * @return string Minified javascript */ public static function minify($js, $filename = '') { return SugarMin::minify($js); }