Property List (.plist) 是 Apple 生态标准序列化格式,支持字典、数组、字符串、数值、布尔、日期、Data 等类型,广泛用于 iOS/macOS 配置、应用打包、MDM 下发。
<?php
class PropertyList {
private $obj, $xml, $text;
public function __construct($obj) { $this->obj = $obj; }
// 判断关联数组
private static function is_assoc($array) {
return is_array($array) && count(array_diff_key($array, array_keys(array_keys($array)))) > 0;
}
// 生成 XML 格式
public function xml() {
if (isset($this->xml)) return $this->xml;
$x = new XMLWriter();
$x->openMemory();
$x->setIndent(true);
$x->startDocument('1.0', 'UTF-8');
$x->writeDTD('plist', '-//Apple//DTD PLIST 1.0//EN', 'https://www.apple.com/DTDs/PropertyList-1.0.dtd');
$x->startElement('plist');
$x->writeAttribute('version', '1.0');
$this->xmlWriteValue($x, $this->obj);
$x->endElement(); // plist
$x->endDocument();
return $this->xml = $x->outputMemory();
}
// 生成文本格式 (OpenStep 风格)
public function text() {
if (isset($this->text)) return $this->text;
$this->textWriteValue($this->text, $this->obj);
return $this->text;
}
// 递归写入 XML
private function xmlWriteValue(XMLWriter $x, &$v) {
if (is_int($v) || is_long($v)) $x->writeElement('integer', $v);
elseif (is_float($v)) $x->writeElement('real', $v);
elseif (is_string($v)) $x->writeElement('string', $v);
elseif (is_bool($v)) $x->writeElement($v ? 'true' : 'false');
elseif (self::is_assoc($v)) $this->xmlWriteDict($x, $v);
elseif (is_array($v)) $this->xmlWriteArray($x, $v);
elseif ($v instanceof PlistData) $x->writeElement('data', $v->base64EncodedData());
elseif ($v instanceof PlistDate) $x->writeElement('date', $v->encodedDate());
else trigger_error("Unsupported type in plist", E_USER_WARNING);
}
private function xmlWriteDict(XMLWriter $x, &$dict) {
$x->startElement('dict');
foreach ($dict as $k => &$v) {
$x->writeElement('key', $k);
$this->xmlWriteValue($x, $v);
}
$x->endElement();
}
private function xmlWriteArray(XMLWriter $x, &$arr) {
$x->startElement('array');
foreach ($arr as &$v) $this->xmlWriteValue($x, $v);
$x->endElement();
}
// 文本格式写入(简化版)
private function textWriteValue(&$text, &$v, $indent = 0) {
$pad = str_repeat(' ', $indent);
if (is_int($v)) $text .= $pad . $v;
elseif (is_float($v)) $text .= $pad . $v;
elseif (is_string($v)) $text .= $pad . '"' . addcslashes($v, '"\\') . '"';
elseif (is_bool($v)) $text .= $pad . ($v ? 'YES' : 'NO');
elseif (self::is_assoc($v)) {
$text .= $pad . "{\n";
foreach ($v as $k => &$vv) {
$text .= $pad . " $k = ";
$this->textWriteValue($text, $vv, $indent + 1);
$text .= ";\n";
}
$text .= $pad . "}";
} elseif (is_array($v)) {
$text .= $pad . "(\n";
foreach ($v as &$vv) {
$this->textWriteValue($text, $vv, $indent + 1);
$text .= ",\n";
}
$text .= $pad . ")";
}
}
}
// 便捷函数
function plist_encode_xml($obj) { return (new PropertyList($obj))->xml(); }
function plist_encode_text($obj) { return (new PropertyList($obj))->text(); }
$data = array(
'CFBundleIdentifier' => 'com.example.app',
'CFBundleVersion' => '1.0.0',
'CFBundleName' => 'MyApp',
'LSApplicationCategoryType' => 'public.app-category.utilities',
'SupportedDevices' => array('iPhone', 'iPad'),
'MinimumOSVersion' => '13.0',
'BuildDate' => new PlistDate(time()),
'IconData' => new PlistData(file_get_contents('icon.png')),
);
// 输出 XML
header('Content-Type: application/xml; charset=utf-8');
echo plist_encode_xml($data);
// 输出文本
// header('Content-Type: text/plain; charset=utf-8');
// echo plist_encode_text($data);
class PlistDate {
private $timestamp;
public function __construct($timestamp) { $this->timestamp = $timestamp; }
public function encodedDate() { return gmdate('Y-m-d\TH:i:s\Z', $this->timestamp); }
}
class PlistData {
private $data;
public function __construct($binary) { $this->data = $binary; }
public function base64EncodedData() { return base64_encode($this->data); }
}
Info.plist.mobileconfig)