2012-03-12 46 views
1

我創建了一個自定義應用程序,我喜歡將一些PHP變量傳遞給CSS。PHP頭過期檢查

我使用的方法,是一種style.php文件服務CSS頭,所以HTTP請求它解決了請求作爲一個正常的CSS文件。

這種方法的問題是,在每一個請求,我必須加載在內存中的一些大型PHP文件。

我喜歡做的是檢查文件的截止日期,如果該文件還沒有到期,則不能運行該腳本,而是發送緩存的文件。

那麼,有沒有對這個問題的任何解決方案?

我的PHP文件,我喜歡看起來像這樣的事情:

<?php 
    // Enter here several headers to cache the file and other operations 
    // Check here if the file must be re-executed or load a cached version instead 
    // Enter here the inclution of my application scripts with include_once(....) 
?> 
body 
{ 
    background-color: <?php echo $options->css['bg_color']; ?> 
} 

親切的問候 Merianos尼科斯

+0

CSS您多久改變一次?通常,在部署應用程序時,更容易重新創建樣式表,因爲在部署之間它們不會改變。 – KingCrunch 2012-03-12 11:28:36

+0

檢查確切文件的到期日期?您如何期待維持到期日期? – deed02392 2012-03-12 11:32:05

+0

我不是指文件的到期時間。將爲CSS提供服務的php文件將有頭文件(....)用於過期,並且我希望檢查頭文件過期。 – 2012-03-12 11:40:34

回答

1

我不得不這樣做類似於你在做什麼東西,解決了解析CSS問題文件服務器端。這是一個link到一個簡單的類,就是這樣(這是我現在使用的)。它基本上解析目錄中的所有文件,並使用strtr()替換css文件中的值。

class Css { 
    //this is an associative array that's used when replacing 
    private $settings; 
    //This is the path (relative to this file) to the dir that holds the css files to parse 
    private $cssDir = "css/"; 
    //private varray to store the files 
    private $cssFiles = array(); 
    //This is the path (relative to this file) to the dir where the parsed css files wil be written 
    private $outCssDir = "../css/"; 

    /** 
    * the class thake only one parameters, an associative array of keys to replace with their respective values 
    */ 
    function __construct($settings) { 
     $this -> settings = $settings; 
     $this -> readFiles(); 
     $this -> replaceValuesInCssStrings(); 

    } 

    private function readFiles() { 
     $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $this -> cssDir; 
     if (file_exists($dir)) { 
      if ($handle = opendir($dir)) { 
       /* This is the correct way to loop over the directory. */ 
       while (false !== ($entry = readdir($handle))) { 
        //filter out .. and . 
        if ($entry !== "." && $entry !== "..") { 
         $cssFiles[$entry] = file_get_contents($dir . $entry); 
        } 
       } 

       closedir($handle); 
      } 
     } 
     $this -> cssFiles = $cssFiles; 

    } 

    private function replaceValuesInCssStrings() { 
     foreach ($this->cssFiles as $fileName => $fileContent) { 
      $this->cssFiles[$fileName] = strtr($fileContent, $this-> settings); 

     } 
    } 
    /** 
    * renders the parsed css files in a <style> tag 
    */ 
    public function render() { 
     $css = "<style>\n"; 
     foreach ($this -> cssFiles as $fileContent) { 
      $css .= $fileContent; 
     } 
     $css .= "</style>"; 
     echo($css); 
    } 

    /** 
    * writes the parsed css files to the $outCssDir (keeping their names intact) 
    */ 
    public function renderToFile() { 
     $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $this -> outCssDir; 
     if (file_exists($dir)) { 
      foreach($this->cssFiles as $fileName => $fileContent){ 
       file_put_contents($dir . $fileName, $fileContent); 

      } 
     } 
    } 
    /** 
    * this is just in case you want to call echo ($css) 
    */ 
    public function __toString() { 
     $this -> render(); 
    } 

} 
+0

這是一個非常好的辦法,我已經在另一個Web應用程序我做了使用它。 一個我現在創建,我需要的CSS編寫與解放軍imediatly值的PHP文件CE。 我改變了上面的代碼,告訴你我的意思。無論如何,謝謝你的代碼! :) – 2012-03-12 11:38:33