Leafo\ScssPhp\Server::serve PHP Method

serve() public method

Compile requested scss and serve css. Outputs HTTP response.
public serve ( string $salt = '' )
$salt string Prefix a string to the filename for creating the cache name hash
    public function serve($salt = '')
    {
        $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
        if ($input = $this->findInput()) {
            $output = $this->cacheName($salt . $input);
            $etag = $noneMatch = trim($this->getIfNoneMatchHeader(), '"');
            if ($this->needsCompile($output, $etag)) {
                try {
                    list($css, $etag) = $this->compile($input, $output);
                    $lastModified = gmdate('D, d M Y H:i:s', filemtime($output)) . ' GMT';
                    header('Last-Modified: ' . $lastModified);
                    header('Content-type: text/css');
                    header('ETag: "' . $etag . '"');
                    echo $css;
                } catch (\Exception $e) {
                    if ($this->showErrorsAsCSS) {
                        header('Content-type: text/css');
                        echo $this->createErrorCSS($e);
                    } else {
                        header($protocol . ' 500 Internal Server Error');
                        header('Content-type: text/plain');
                        echo 'Parse error: ' . $e->getMessage() . "\n";
                    }
                }
                return;
            }
            header('X-SCSS-Cache: true');
            header('Content-type: text/css');
            header('ETag: "' . $etag . '"');
            if ($etag === $noneMatch) {
                header($protocol . ' 304 Not Modified');
                return;
            }
            $modifiedSince = $this->getIfModifiedSinceHeader();
            $mtime = filemtime($output);
            if (strtotime($modifiedSince) === $mtime) {
                header($protocol . ' 304 Not Modified');
                return;
            }
            $lastModified = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
            header('Last-Modified: ' . $lastModified);
            echo file_get_contents($output);
            return;
        }
        header($protocol . ' 404 Not Found');
        header('Content-type: text/plain');
        $v = Version::VERSION;
        echo "/* INPUT NOT FOUND scss {$v} */\n";
    }

Usage Example

コード例 #1
0
 public function serve($resetSalt = false)
 {
     if ($resetSalt) {
         $this->salt = time();
     }
     $this->server->serve($this->salt);
 }