/**
  * test that compressor "Jshrink" works
  *
  * @return void
  */
 public function testCompressorJshrink()
 {
     $comp = ShrinkType::getCompressor('jshrink', []);
     // verify the instance
     $this->assertInstanceOf('\\Shrink\\Lib\\ShrinkCompressor\\ShrinkCompressorInterface', $comp);
     if ($comp->isAvailable()) {
         // actual result
         $file = new File(WWW_ROOT . 'js/base.js');
         $code = $file->read();
         $result = $comp->compress($code);
         $file->close();
         // expected result
         $minfile = new File(WWW_ROOT . 'js/base.jshrink.js');
         $expect = $minfile->read();
         $minfile->close();
         $this->assertEquals($expect, $result, 'Compressed js does not match. Ensure jshrink package is available via composer.');
     } else {
         echo "\nSkipping Jshrink tests, no Jshrink available via composer.\n";
     }
 }
示例#2
0
 /**
  * build - Compile, Compress, Combile the array of files
  * @param array $files - filenames to process
  * @param string $type - js or css to indicate how to compress and which options to use
  * @param string $cacheFile - filename to write the results to, relative to cachePath option
  * @return array - array with the cache file object and the new web path ['file','webPath']
  */
 function build($files, $type, $cacheFile = '')
 {
     // determine the cache file path
     if ($cacheFile === '') {
         $cacheFile = $this->settings['prefix'] . md5(implode('_', $files)) . '.' . $type;
     }
     $cacheFilePath = preg_replace('/(\\/+|\\+)/', DS, WWW_ROOT . DS . $this->settings[$type]['cachePath'] . DS . $cacheFile);
     $cacheFileObj = new File($cacheFilePath);
     $webCacheFilePath = $this->settings['url'] . preg_replace('/\\/+/', '/', '/' . $this->extraPath . '/' . $this->settings[$type]['cachePath'] . $cacheFile);
     // create Cake file objects and get the max date
     $maxAge = 0;
     foreach ($files as $k => $v) {
         $tmpf = new File(preg_replace('/(\\/+|\\+)/', DS, WWW_ROOT . DS . ($v[0] == '/' ? '' : $this->settings[$type]['path']) . DS . $v));
         $files[$k] = ['file' => $tmpf, 'rel_path' => $v];
         $srcMod = $tmpf->lastChange();
         if ($srcMod > $maxAge) {
             $maxAge = $srcMod;
         }
     }
     // has the cache expired (we're debugging, the cache doesn't exist, or too old)?
     $expired = false;
     if ($this->debugging || !$cacheFileObj->exists() || $maxAge > $cacheFileObj->lastChange()) {
         $expired = true;
     }
     // rebuild if it has expired
     if ($expired) {
         $output = '';
         foreach ($files as $k => $v) {
             $lang = $v['file']->ext();
             // load compiler if it is not already
             if (!isset($this->compilers[$lang])) {
                 $this->compilers[$lang] = ShrinkType::getCompiler($lang, $this->settings);
             }
             $resultType = $this->compilers[$lang]->resultType;
             // load the compressor if it is not already
             $compressorName = $this->settings[$type]['minifier'];
             if (!isset($this->compressors[$compressorName])) {
                 $this->compressors[$compressorName] = ShrinkType::getCompressor($compressorName, $this->settings);
             }
             // compile, compress, combine
             if ($resultType == $type && $v['file']->exists()) {
                 $output .= "/* " . $v['rel_path'] . " */\n";
                 $code = $this->compilers[$lang]->compile($v['file']);
                 $code = $this->compressors[$compressorName]->compress($code);
                 $output .= $code . "\n";
             }
             // we are done with this file, close it
             $v['file']->close();
         }
         // be sure no duplicate charsets
         if ($type == 'css') {
             $output = preg_replace('/@charset\\s+[\'"].+?[\'"];?/i', '', $output);
             $output = '@charset "' . $this->settings['css']['charset'] . "\";\n" . $output;
         }
         // write the file
         $cacheFileObj->write($output);
     }
     $ret = ['path' => $cacheFileObj->path, 'webPath' => $webCacheFilePath];
     $cacheFileObj->close();
     return $ret;
 }
示例#3
0
 /**
  * Processes/minify/combines queued files of the requested type.
  * @param string type - 'js' or 'css'. This should be the end result type
  * @param string how - 'link' for <script src="">, 'async' for <script src="" async>, 'embed' for <script>...js code...</script>
  * @param array files - string name of a file or array containing multiple string of files
  * @return string - the <script> or <link>
  */
 function fetch($type, $how = 'link', $files = array())
 {
     if ($type == 'script') {
         $type = 'js';
     }
     if (!$files) {
         $files =& $this->files;
     }
     if (!$files) {
         return '';
     }
     // ensure the layout files are before the view files
     $files[$type] = array_merge($files[$type]['layout'], $files[$type]['view']);
     // determine the cache file path
     $cacheFile = $this->settings['prefix'] . md5(implode('_', $files[$type])) . '.' . $type;
     $cacheFilePath = preg_replace('/(\\/+|\\+)/', DS, WWW_ROOT . DS . $this->settings[$type]['cachePath'] . DS . $cacheFile);
     $cacheFileObj = new File($cacheFilePath);
     $webCacheFilePath = $this->settings['url'] . preg_replace('/\\/+/', '/', '/' . $this->extraPath . '/' . $this->settings[$type]['cachePath'] . $cacheFile);
     // create Cake file objects and get the max date
     $maxAge = 0;
     foreach ($files[$type] as $k => $v) {
         //$caminho = preg_replace('/(\/+|\\+)/', DS, WWW_ROOT .DS. ($v[0]=='/'? '':$this->settings[$type]['path']) .DS. $v);
         $tmpf = new File(preg_replace('/(\\/+|\\+)/', DS, WWW_ROOT . DS . ($v[0] == '/' ? '' : $this->settings[$type]['path']) . DS . $v));
         //var_dump($tmpf); //path
         //echo '<br><br><br>';
         $files[$type][$k] = array('file' => $tmpf, 'rel_path' => $v);
         $srcMod = $tmpf->lastChange();
         if ($srcMod > $maxAge) {
             $maxAge = $srcMod;
         }
     }
     // has the cache expired (we're debugging, the cache doesn't exist, or too old)?
     $expired = false;
     if ($this->debugging || !$cacheFileObj->exists() || $maxAge > $cacheFileObj->lastChange()) {
         $expired = true;
     }
     // rebuild if it has expired
     if ($expired) {
         $output = '';
         foreach ($files[$type] as $k => $v) {
             $lang = $v['file']->ext();
             // load compiler if it is not already
             if (!isset($this->compilers[$lang])) {
                 $this->compilers[$lang] = ShrinkType::getCompiler($lang, $this->settings);
             }
             $resultType = $this->compilers[$lang]->resultType;
             // load the compressor if it is not already
             $compressorName = $this->settings[$type]['minifier'];
             if (!isset($this->compressors[$compressorName])) {
                 $this->compressors[$compressorName] = ShrinkType::getCompressor($compressorName, $this->settings);
             }
             // compile, compress, combine
             if ($resultType == $type && $v['file']->exists()) {
                 $output .= "/* " . $v['rel_path'] . " */\n";
                 $code = $this->compilers[$lang]->compile($v['file']);
                 // INICIA MODIFICAÇÃO FEITA PELO AUTOR DO "PROJETO STORES"
                 //$code = $this->compressors[$compressorName]->compress($code);
                 $isMinified = strpos($v['file']->path, '.min');
                 if ($type == 'css' && $isMinified === false) {
                     $code = $this->compressors[$compressorName]->compress($code);
                 }
                 if ($type == 'js' && $isMinified === false) {
                     $jshrinkCompressor = new ShrinkCompressorJshrink();
                     $code = $jshrinkCompressor->compress($code);
                 }
                 if ($isMinified !== false) {
                     $patter = '/\\/\\*(.|[\\r\\n])*?\\*\\//';
                     $replacement = ' ';
                     $code = preg_replace($patter, $replacement, $code);
                 }
                 // TERMINA MODIFICAÇÃO FEITA PELO AUTOR DO "PROJETO STORES"
                 $output .= $code . "\n";
             }
         }
         // be sure no duplicate charsets
         if ($type == 'css') {
             $output = preg_replace('/@charset\\s+[\'"].+?[\'"];?/i', '', $output);
             $output = '@charset "' . $this->settings['css']['charset'] . "\";\n" . $output;
         }
         // write the file
         $cacheFileObj->write($output);
     }
     // files will be @$this->files, so this clears them
     $files[$type] = array('layout' => array(), 'view' => array());
     // print them how the user wants
     if ($how == 'embed') {
         $output = $cacheFileObj->read();
         if ($type == 'css') {
             return '<style type="text/css">' . $output . '</style>';
         } else {
             return '<script type="text/javascript">' . $output . '</script>';
         }
     } else {
         if ($type == 'css') {
             return '<link href="' . $webCacheFilePath . '" rel="stylesheet" type="text/css" />';
         } else {
             // MODIFICAÇÃO FEITA PELO AUTOR DO "PROJETO STORES"
             //return '<script src="'. $webCacheFilePath .'" type="text/javascript"'. ($how=='async'? ' async ':'') .'></script>';
             return '<script src="' . $webCacheFilePath . '" ' . ($how == 'async' ? ' async ' : ' defer ') . '></script>';
         }
     }
 }
 /**
  * test that compiler "ts" works
  *
  * @return void
  */
 public function testCompilerTypescript()
 {
     $compiler = ShrinkType::getCompiler('ts', []);
     // verify the instance
     $this->assertInstanceOf('\\Shrink\\Lib\\ShrinkCompiler\\ShrinkCompilerInterface', $compiler);
     if ($compiler->isAvailable()) {
         // get the result
         $file = new File(WWW_ROOT . 'js/base.ts');
         $result = $compiler->compile($file);
         $file->close();
         // get the expected result
         $jsfile = new File(WWW_ROOT . 'js/base.ts.js');
         $expect = $jsfile->read();
         $jsfile->close();
         $this->assertEquals($expect, $result, 'Compiled Typescript does not match. Ensure Typescript script command line utility is available. npm install -g typescript');
     } else {
         echo "\nSkipping Typescript tests, no typescript available: npm install -g typescript\n";
     }
 }