/** * @param array $files * @param string $type * @return string */ public function getCompiledFile(array $files, $type) { $includedFiles = $this->cache->load($files); if ($includedFiles) { $modified = $this->getLastModification($includedFiles); $cacheKey = $this->getCacheKey($files); $cacheFile = $this->public->load($cacheKey, $modified, $type); if ($cacheFile) { return $this->public->getUrl() . '/' . pathinfo($cacheFile, PATHINFO_BASENAME) . '?' . $modified; } } $output = ''; $includedFiles = array(); $filters =& $this->{$type . 'Filters'}; foreach ($files as $file) { $file = $this->resourcesDirectory . '/' . $file; if (!is_readable($file)) { throw new FileNotFoundException("File '{$file}' not found."); } $input = file_get_contents($file); $extension = pathinfo($file, PATHINFO_EXTENSION); if (isset($filters[$extension])) { if (!is_array($filters[$extension])) { $filters[$extension] = (array) $filters[$extension]; } foreach ($filters[$extension] as &$filter) { if (is_string($filter)) { $filter = $this->container->getService($filter); } $input = $filter($input, $file); $includedFiles = array_merge($includedFiles, $filter->getIncludedFiles()); } } elseif ($extension == 'css' || $extension == 'js') { $includedFiles[] = $file; } else { throw new InvalidStateException("Unknown extension '{$extension}'."); } $output .= $input; } $cacheKey = $this->getCacheKey($files); $this->cache->save($files, $includedFiles, array(\Nette\Caching\Cache::EXPIRE => '+ 1 month')); $cacheFile = $this->public->save($cacheKey, $output, $type); return $this->public->getUrl() . '/' . pathinfo($cacheFile, PATHINFO_BASENAME) . '?' . $this->getLastModification($includedFiles); }