2011-03-17 22 views
0

我正在使用CFPropertyList PHP庫,它似乎默認的方法是寫入/保存plist文件。直接輸出plist與寫入文件系統

由於我可以控制輸出緩存,我認爲這會更有效地跳過plist文件的寫入,並直接渲染它。

我該如何去修改CFPropertyList來做到這一點?

這是目前保存功能:

public function save($file=null,$format=null) { 
$file = $file ? $file : $this->file; 
$format = $format ? $format : $this->format; 

if(!in_array($format, array(self::FORMAT_BINARY, self::FORMAT_XML))) 
    throw new PListException("format {$format} is not supported, use CFPropertyList::FORMAT_BINARY or CFPropertyList::FORMAT_XML"); 

if(!file_exists($file)) { 
    // dirname("file.xml") == "" and is treated as the current working directory 
    if(!is_writable(dirname($file))) throw IOException::notWritable($file); 
} 
else if(!is_writable($file)) throw IOException::notWritable($file); 

$content = $format == self::FORMAT_BINARY ? $this->toBinary() : $this->toXML(); 

$fh = fopen($file, 'wb'); 
fwrite($fh,$content); 
fclose($fh); 
} 

回答

1

可能:

public function screen($file=null,$format=null) { 
    $file = $file ? $file : $this->file; 
    $format = $format ? $format : $this->format; 

    if(!in_array($format, array(self::FORMAT_BINARY, self::FORMAT_XML))) { 
     throw new PListException("format {$format} is not supported, use CFPropertyList::FORMAT_BINARY or CFPropertyList::FORMAT_XML"); 
    } else { 
     $fmt = ($format == self::FORMAT_XML ? 1 : 0); 
     $content = ($fmt ? $this->toXML() : $this->toBinary()); 
     header('Content-Type: ' . ($fmt ? 'text/xml' : 'application/octet-stream')); 
     echo $content; 
    } 
} 

編輯:重構。

+0

是的,這與我剛剛提出的類似,比我懷疑的要容易。也刪除了$ file參數。謝謝! – sterling 2011-03-17 21:09:28