直接输出plist与写入文件系统

发布于 2024-10-23 18:30:00 字数 935 浏览 4 评论 0原文

我正在使用 CFPropertyList PHP 库,默认方法似乎是写入/保存 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);
}

I am working with the CFPropertyList PHP libraries and it seems the default methods are to write/save the plist files.

Since I can control output caching, I think it would be more efficient to skip the writing of the plist file and simply render it directly.

How would I go about modifying the CFPropertyList to do this?

Here is the current save function:

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-10-30 18:30:00

可能:

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;
    }
}

编辑:重构。

Possibly:

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;
    }
}

Edit: Refactoring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文