squeezr/lib/Tollwerk/Squeezr/Css.php
/**
     * Parse, squeeze and send the CSS file to the client
     *
     * @param boolean $cache Cache a copy of the parsed CSS stylesheet
     * @return void
     */
Parse, squeeze and send the CSS file to the client
@param boolean $cache Cache a copy of the parsed CSS stylesheet
@return void
Parse squeeze and send the CSS file to the client @param boolean $cache Cache a copy of the parsed CSS stylesheet @return void
public function send($cache = true)
    {
        $returnCssFile = $this->_absoluteCssPath;
        $returnCss = '';
        try {
            if ((!@is_dir($this->_absoluteCacheCssDir) && !@mkdir($this->_absoluteCacheCssDir, 0777,
                        true)) || !@is_writable($this->_absoluteCacheCssDir)
            ) {
                $this->_addErrorHeader(\Tollwerk\Squeezr\Exception::INVALID_TARGET_CACHE_DIRECTORY_MSG,
                    \Tollwerk\Squeezr\Exception::INVALID_TARGET_CACHE_DIRECTORY);
            } elseif (empty($_COOKIE['squeezr_css']) || !preg_match("%^(\d+)x(\d+)\@(\d+(?:\.\d+)?)$%",
                    $_COOKIE['squeezr_css'], $squeezr)
            ) {
                $this->_addErrorHeader(\Tollwerk\Squeezr\Exception::MISSING_METRICS_COOKIE_MSG,
                    \Tollwerk\Squeezr\Exception::MISSING_METRICS_COOKIE);
            } else {
                if (@is_file($this->_absoluteCachePhpPath)) {
                    $cacheInstance = include $this->_absoluteCachePhpPath;
                } else {
                    $cacheClassCode = trim($this->_compileCacheClassCode());
                    if ($cache && !@file_put_contents($this->_absoluteCachePhpPath, $cacheClassCode)) {
                        $this->_addErrorHeader(sprintf(\Tollwerk\Squeezr\Exception::FAILED_WRITING_CACHE_FILE_STR,
                            $this->_absoluteCachePhpPath), \Tollwerk\Squeezr\Exception::FAILED_WRITING_CACHE_FILE);
                        $cache = false;
                    }
                    $cacheInstance = eval(substr($cacheClassCode, 5));
                }
                if (is_object($cacheInstance)) {
                    $returnCss = strval($cacheInstance);
                    $breakpointsCachePath = $this->_absoluteCachePathBase.$cacheInstance->getMatchingBreakpoints().'.css';
                    if ($cache) {
                        if (!@is_file($breakpointsCachePath) && !@file_put_contents($breakpointsCachePath,
                                $returnCss)
                        ) {
                            $this->_addErrorHeader(sprintf(\Tollwerk\Squeezr\Exception::FAILED_WRITING_CACHE_FILE_STR,
                                $breakpointsCachePath), \Tollwerk\Squeezr\Exception::FAILED_WRITING_CACHE_FILE);
                        } elseif (!@symlink($breakpointsCachePath, $this->_absoluteCacheCssPath)) {
                            $this->_addErrorHeader(sprintf(\Tollwerk\Squeezr\Exception::FAILED_WRITING_CACHE_FILE_STR,
                                $this->_absoluteCacheCssPath), \Tollwerk\Squeezr\Exception::FAILED_WRITING_CACHE_FILE);
                        } else {
                            $returnCssFile = $this->_absoluteCacheCssPath;
                        }
                    }
                }
            }
        } catch (\Tollwerk\Squeezr\Exception $e) {
            $this->_addErrorHeader($e->getMessage(), $e->getCode());
            $returnCssFile = $this->_absoluteCssPath;
        }
        if ($returnCssFile) {
            $this->_sendFile($returnCssFile, 'text/css', true);
            exit;
        } else {
            header('Content-Type: text/css');
            header('Content-Length: '.strlen($returnCss));
            header('Cache-Control: no-cache, must-revalidate');
            header('Expires: '.gmdate('D, d M Y H:i:s', time() - SQUEEZR_CACHE_LIFETIME).' GMT');
            die($returnCss);
        }
    }
public function send ( $ cache = true ) { $ returnCssFile = $ this -> _absoluteCssPath ; $ returnCss = '' ; try { if ( ( ! @ is_dir ( $ this -> _absoluteCacheCssDir ) && ! @ mkdir ( $ this -> _absoluteCacheCssDir , 0777 , true ) ) || ! @ is_writable ( $ this -> _absoluteCacheCssDir ) ) { $ this -> _addErrorHeader ( \ Tollwerk \ Squeezr \ Exception :: INVALID_TARGET_CACHE_DIRECTORY_MSG , \ Tollwerk \ Squeezr \ Exception :: INVALID_TARGET_CACHE_DIRECTORY ) ; } elseif ( empty ( $ _COOKIE [ 'squeezr_css' ] ) || ! preg_match ( " %^(\d+)x(\d+)\@(\d+(?:\.\d+)?)$% " , $ _COOKIE [ 'squeezr_css' ] , $ squeezr ) ) { $ this -> _addErrorHeader ( \ Tollwerk \ Squeezr \ Exception :: MISSING_METRICS_COOKIE_MSG , \ Tollwerk \ Squeezr \ Exception :: MISSING_METRICS_COOKIE ) ; } else { if ( @ is_file ( $ this -> _absoluteCachePhpPath ) ) { $ cacheInstance = include $ this -> _absoluteCachePhpPath ; } else { $ cacheClassCode = trim ( $ this -> _compileCacheClassCode ( ) ) ; if ( $ cache && ! @ file_put_contents ( $ this -> _absoluteCachePhpPath , $ cacheClassCode ) ) { $ this -> _addErrorHeader ( sprintf ( \ Tollwerk \ Squeezr \ Exception :: FAILED_WRITING_CACHE_FILE_STR , $ this -> _absoluteCachePhpPath ) , \ Tollwerk \ Squeezr \ Exception :: FAILED_WRITING_CACHE_FILE ) ; $ cache = false ; } $ cacheInstance = eval ( substr ( $ cacheClassCode , 5 ) ) ; } if ( is_object ( $ cacheInstance ) ) { $ returnCss = strval ( $ cacheInstance ) ; $ breakpointsCachePath = $ this -> _absoluteCachePathBase . $ cacheInstance -> getMatchingBreakpoints ( ) . '.css' ; if ( $ cache ) { if ( ! @ is_file ( $ breakpointsCachePath ) && ! @ file_put_contents ( $ breakpointsCachePath , $ returnCss ) ) { $ this -> _addErrorHeader ( sprintf ( \ Tollwerk \ Squeezr \ Exception :: FAILED_WRITING_CACHE_FILE_STR , $ breakpointsCachePath ) , \ Tollwerk \ Squeezr \ Exception :: FAILED_WRITING_CACHE_FILE ) ; } elseif ( ! @ symlink ( $ breakpointsCachePath , $ this -> _absoluteCacheCssPath ) ) { $ this -> _addErrorHeader ( sprintf ( \ Tollwerk \ Squeezr \ Exception :: FAILED_WRITING_CACHE_FILE_STR , $ this -> _absoluteCacheCssPath ) , \ Tollwerk \ Squeezr \ Exception :: FAILED_WRITING_CACHE_FILE ) ; } else { $ returnCssFile = $ this -> _absoluteCacheCssPath ; } } } } } catch ( \ Tollwerk \ Squeezr \ Exception $ e ) { $ this -> _addErrorHeader ( $ e -> getMessage ( ) , $ e -> getCode ( ) ) ; $ returnCssFile = $ this -> _absoluteCssPath ; } if ( $ returnCssFile ) { $ this -> _sendFile ( $ returnCssFile , 'text/css' , true ) ; exit ; } else { header ( 'Content-Type: text/css' ) ; header ( 'Content-Length: ' . strlen ( $ returnCss ) ) ; header ( 'Cache-Control: no-cache, must-revalidate' ) ; header ( 'Expires: ' . gmdate ( 'D, d M Y H:i:s' , time ( ) - SQUEEZR_CACHE_LIFETIME ) . ' GMT' ) ; die ( $ returnCss ) ; } }
Parse, squeeze and send the CSS file to the client
 @param boolean $cache Cache a copy of the parsed CSS stylesheet
 @return void
Parse squeeze and send the CSS file to the client @param boolean $cache Cache a copy of the parsed CSS stylesheet @return void
// Try to process this CSS file // If the cache directory doesn't exist or is not writable: Error // If the squeezr screen cookie is not available or invalid // Else: Compile and send the breakpoint specific CSS // If there's already a PHP cache file of the requested CSS // Else: Create a PHP cache file of the requested CSS // Compile the cacheable CSS file PHP class // If the file should be cached: Write the PHP cache class file to disk // Disable caching alltogether // Instanciate the cache class // If the PHP cache class could be instanciated ... // Render the breakpoint specific CSS (if not available yet) and create a request specific symlink // If caching is enabled ... // If the breakpoint specific CSS cannot be created: Caching error // Else if the breakpoint specific CSS cannot be symlinked: Caching error // Else: Return the cached file // On errors // If the CSS has been cached to a file // Else: Just send the CSS code along with some appropriate headers