Exemplo n.º 1
0
 /**
  * Load file
  *
  * @param string $file filepath
  * @return string
  * @throws FileNotFoundException
  */
 protected function loadFile($file)
 {
     if (FALSE === ($content = file_get_contents($this->sourcePath . '/' . $file))) {
         if ($this->throwExceptions) {
             if ($this->getPresenter(FALSE)->context->params['productionMode']) {
                 throw new \Nette\FileNotFoundException("File '{$this->sourcePath}/{$file}' doesn't exist.");
             } else {
                 \Tracy\Debugger::processException(new \Nette\FileNotFoundException("File '{$this->sourcePath}/{$file}' doesn't exist."));
                 return '';
             }
         }
         return '';
     }
     foreach ($this->preFileFilters as $filter) {
         $fcontent = call_user_func($filter, $content, $this, $this->sourcePath . '/' . $file);
         $content = is_array($fcontent) ? $fcontent[PreFileFilter::CONTENT] : $fcontent;
         foreach ($this->fileFilters as $filter) {
             $content = call_user_func($filter, $content, $this, $this->sourcePath . '/' . $file);
         }
     }
     return $content;
 }
Exemplo n.º 2
0
 /**
  * Generates link
  * @return string
  */
 public function getLink()
 {
     if ($hasArgs = func_num_args() > 0) {
         $backup = $this->files;
         $this->clear();
         $this->addFiles(func_get_args());
     }
     $filenames = [];
     $content = '';
     if (($cnt = count($this->files)) > 0) {
         if ($this->enableDirect && $cnt == 1 && $this->files[0][1] == self::COMPACT) {
             $link = $this->sourceUri . $this->files[0][0];
             if ($hasArgs) {
                 $this->files = $backup;
             }
             return $link;
         } else {
             $dc = get_declared_classes();
             // u javascriptu zalezi na poradi
             foreach ($this->files as $file) {
                 switch ($file[1]) {
                     case self::COMPACT:
                         $content .= $this->loadFile($file[0]);
                         break;
                     case self::MINIFY:
                         // dean edwards packer neumi cz/sk znaky!!
                         if (Strings::endsWith($file[0], '.min.js')) {
                             // already minified ?
                             $content .= $this->loadFile($file[0]);
                         } elseif (is_file($mfile = $this->sourcePath . '/' . substr($file[0], 0, -3) . '.min.js')) {
                             // have minified ?
                             $content .= file_get_contents($mfile);
                         } elseif (in_array('JSMin', $dc) || class_exists('JSMin')) {
                             // minify
                             $content .= \JSMin::minify($this->loadFile($file[0]));
                         } else {
                             if ($this->throwExceptions) {
                                 if ($this->getPresenter(FALSE)->context->params['productionMode']) {
                                     throw new \Nette\FileNotFoundException("Don't have JSMin class.");
                                 } else {
                                     Debugger::processException(new \Nette\FileNotFoundException("Don't have JSMin class"));
                                 }
                             }
                             $content .= $this->loadFile($file[0]);
                         }
                         break;
                     case self::PACK:
                         if (Strings::endsWith($file[0], '.pack.js')) {
                             // already packed ?
                             $content .= $this->loadFile($file[0]);
                         } elseif (is_file($pfile = $this->sourcePath . '/' . substr($file[0], 0, -3) . '.pack.js')) {
                             // have packed ?
                             $content .= file_get_contents($pfile);
                         } elseif (in_array('JavaScriptPacker', $dc) || class_exists('JavaScriptPacker')) {
                             $jsp = new \JavaScriptPacker($this->loadFile($file[0]));
                             $content .= $jsp->pack();
                         } else {
                             if ($this->throwExceptions) {
                                 if ($this->getPresenter(FALSE)->context->params['productionMode']) {
                                     throw new \Nette\FileNotFoundException("Don't have JavaScriptPacker class.");
                                 } else {
                                     Debugger::processException(new \Nette\FileNotFoundException("Don't have JavaScriptPacker class"));
                                 }
                             }
                             $content .= $this->loadFile($file[0]);
                         }
                         break;
                     default:
                         if ($hasArgs) {
                             $this->files = $backup;
                         }
                         return;
                 }
                 $filenames[] = $file[0];
             }
             $link = $this->getPresenter()->link(':Lohini:WebLoader:', $this->generate($filenames, $content));
             if ($hasArgs) {
                 $this->files = $backup;
             }
             return $link;
         }
     }
     if ($hasArgs) {
         $this->files = $backup;
     }
 }