/** * Process host CSS file and return a new compiled file * * @param string $file URL or System path to the host CSS file * @param mixed $options An array of options or null * @return string The public path to the compiled file or an empty string */ public static function file($file, $options = null) { $config = self::$config; // Since we're comparing strings, we need to iron out OS differences $file = str_replace('\\', '/', $file); $docRoot = $config->docRoot; $pathtest = true; if (strpos($file, $docRoot) === 0) { // System path $pathtest = self::setPath(dirname($file)); } else { if (strpos($file, '/') === 0) { // WWW root path $pathtest = self::setPath(dirname($docRoot . $file)); } else { // Relative path $pathtest = self::setPath(dirname(dirname(__FILE__) . '/' . $file)); } } if (!$pathtest) { // Main directory not found or is not writable // Return an empty string return ''; } self::loadConfig(); self::parseOptions($options); // Make basic information about the hostfile accessible $hostfile = new stdClass(); $hostfile->name = basename($file); $hostfile->dir = $config->baseDir; $hostfile->path = "{$config->baseDir}/{$hostfile->name}"; if (!file_exists($hostfile->path)) { // If host file is not found return an empty string trigger_error(__METHOD__ . ": File '{$hostfile->name}' not found.\n", E_USER_WARNING); return ''; } else { // Capture the modified time $hostfile->mtime = filemtime($hostfile->path); } // Compiled filename we're searching for self::$compileName = basename($hostfile->name, '.css') . self::$COMPILE_SUFFIX; // If cache is enabled check for a valid compiled file if (self::$options['cache'] === true) { $validCompliledFile = self::validateCache($hostfile); if (is_string($validCompliledFile)) { return $validCompliledFile; } } // Collate hostfile and imports $stream = CssCrush_Importer::hostfile($hostfile); // Compile $stream = self::compile($stream); // Add in boilerplate if (self::$options['boilerplate']) { $stream = self::getBoilerplate() . "\n{$stream}"; } // Create file and return path. Return empty string on failure if (file_put_contents("{$config->baseDir}/" . self::$compileName, $stream)) { return "{$config->baseURL}/" . self::$compileName . (self::$options['versioning'] ? '?' . time() : ''); } else { return ''; } }