コード例 #1
0
 public function js($position = 'header', $src = false)
 {
     if ($this->cache->canCache() && !$this->cache->getDeveloperMode() && $this->minify_js) {
         /* generate file if not exits */
         $combined_js_file = '_' . $this->getHash($this->scripts[$position], 'js', $this->cache->getLanguageId());
         if (!file_exists($this->path . $combined_js_file)) {
             /* parse all scripts, generate corresponding minified js file */
             foreach ($this->scripts[$position] as $script) {
                 $file = $this->path . $this->getHash($script, 'js');
                 if (!file_exists($file)) {
                     $js_file = realpath(DIR_SYSTEM . '../' . $this->removeQueryString($script));
                     if (!file_exists($js_file)) {
                         continue;
                     }
                     $content = file_get_contents($js_file);
                     $content_min = JSMin::minify($content);
                     file_put_contents($file, $content_min, LOCK_EX);
                 }
             }
             /* combine all scripts into one file */
             $fh = @fopen($this->path . $combined_js_file, 'w');
             flock($fh, LOCK_EX);
             foreach ($this->scripts[$position] as $script) {
                 $file = $this->path . $this->getHash($script, 'js');
                 if (!file_exists($file)) {
                     continue;
                 }
                 $content = file_get_contents($file);
                 fwrite($fh, ';' . $content);
             }
             /* append journal.js at the end */
             if ($position === 'footer' && is_file($this->path . $this->cache->getJournalAssetsFileName('js'))) {
                 fwrite($fh, file_get_contents($this->path . $this->cache->getJournalAssetsFileName('js')));
             }
             flock($fh, LOCK_UN);
             fclose($fh);
         }
         /* return link tag */
         if ($src) {
             return Journal2Utils::staticAsset($this->dir . $combined_js_file);
         }
         if ($position === 'footer') {
             return $this->printScript($this->dir . $combined_js_file, ' defer');
         }
         return $this->printScript($this->dir . $combined_js_file);
     }
     $assets = '';
     foreach ($this->scripts[$position] as $script) {
         $assets .= $this->printScript($script);
     }
     return $assets;
 }