michalcarson/symfony-xml-response 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

michalcarson/symfony-xml-response

Composer 安装命令:

composer require michalcarson/symfony-xml-response

包简介

A simple XML Response formatter for Symfony.

README 文档

README

A simple XML Response formatter for Symfony.

Is it pointless?

Someday, someone will probably tell me this whole class was unnecessary and if I just knew more about Symfony, I could have done this a much easier way. That probably won't happen until I've poured a thousand hours into this project. But, still, tell me!

The point

This class is an effort to make it possible to return an array from an action and have it rendered into XML. This should happen just the same way you can return an array and have it rendered into JSON. The action (controller...whatever term you like) shouldn't have to know the format of the response, just as it shouldn't know the format of the request.

I'm using this with AOL/ATC. ATC is itself based on Aura.Router. Within ATC, I have a custom presenter that--once it determines the caller wants an XML response--simply does this:

$xr = new XmlResponse();

// changing the default root element
$xr->root_element_name = $view;

// data must be set after the root element
$response = $xr->setData($data);

return $response;

In the code above, $data is an array and $view is a string.

If the default root element name ("document") is acceptable, this can be further simplified to:

$response = new XmlResponse($data);
return $response;

or

return new XmlResponse($data);

or

// using the static create() method
$response = XmlResponse::create($data);
return $response;

The static create() method and the constructor both accept additional optional parameters to specify the status code and an array of headers. (The default status code is 200.)

// specifying optional return code and headers
$response = new XmlResponse($data, 202, array('Foo-Header' => 'bar'));
return $response;

Data array

The data passed to this class should be an associative array. Each array key represents an XML tag name and the array value represents the content of the XML tag.

$data = array(
    'foo' => 'bar',
    'argle' => 'bargle'
);
$response = new XmlResponse($data);
return $response;

The array above will produce this XML output:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <foo>bar</foo>
  <argle>bargle</argle>
</document>

Attributes

You can specify attributes to a tag by passing a specially constructed array for the value of that tag. Within this array, each attribute should be represented as a key value pair with the key prefixed by '@'. The actual value of the tag should have a key which repeats the original key name. All attribute key-value pairs must come before the actual value entry.

Keep in mind, this is crossing some boundaries for an action to supply XML attributes. If this array were rendered as JSON instead of XML, it would probably not be usable. This design is flawed and will need to be changed in the future.

$data = array(
    'foo' => array(
        '@buzz' => 'boom',
        '@bing' => 'bam',
        'foo' => 'bar'       // same key name as parent
    ),
    'argle' => 'bargle'
);
$response = new XmlResponse($data);
return $response;

The array above will produce this XML output:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <foo buzz="boom" bing="bam">bar</foo>
  <argle>bargle</argle>
</document>

Repeating Elements

XML can have repeating elements.

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <lorem>ipsum</lorem>
  <files>
    <file><name>file1.txt</name><size>10K</size></file>
    <file><name>file2.txt</name><size>20K</size></file>
    <file><name>file3.txt</name><size>8K</size></file>
  </files>
  <et>dolore</et>
</document>

PHP arrays (and JavaScript objects) do not support this without some very contrived structures. In order to support repeating fields, use the XmlRepeater class. XmlRepeater acts as a decorator to the XmlResponse class.

You may add as many XmlRepeater instances as you need. Each of them takes a placeholder, an element name and an array of data. The class will turn the array into XML, wrap it in an element with the given name and insert it into the XmlResponse result in place of the placeholder.

// associative array for XmlResponse
$data = [
    'lorem' => 'ipsum',
    'files' => '@filesPlaceHolder@'
    'et' => 'dolore'
];

// indexed array for the repeating data
$file_array = [
    ['name' => 'file1.txt', 'size' => '10K'],
    ['name' => 'file2.txt', 'size' => '20K'],
    ['name' => 'file3.txt', 'size' => '8K']
];

$repeater = new XmlRepeater('@filesPlaceHolder@', 'file', $file_array);

$response = new XmlResponse($data);
$response->addDecorator($repeater);
return $response;

This code would produce the XML document above with repeating "file" elements inside the "files" element.

License

Licensed under the MIT license.

michalcarson/symfony-xml-response 适用场景与选型建议

michalcarson/symfony-xml-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 779 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「xml」 「response」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 michalcarson/symfony-xml-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 michalcarson/symfony-xml-response 我们能提供哪些服务?
定制开发 / 二次开发

基于 michalcarson/symfony-xml-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 779
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-03