XmlView.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. App::uses('View', 'View');
  14. App::uses('Xml', 'Utility');
  15. /**
  16. * A view class that is used for creating XML responses.
  17. *
  18. * By setting the '_serialize' key in your controller, you can specify a view variable
  19. * that should be serialized to XML and used as the response for the request.
  20. * This allows you to omit views + layouts, if your just need to emit a single view
  21. * variable as the XML response.
  22. *
  23. * In your controller, you could do the following:
  24. *
  25. * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
  26. *
  27. * When the view is rendered, the `$posts` view variable will be serialized
  28. * into XML.
  29. *
  30. * **Note** The view variable you specify must be compatible with Xml::fromArray().
  31. *
  32. * You can also define `'_serialize'` as an array. This will create an additional
  33. * top level element named `<response>` containing all the named view variables:
  34. *
  35. * {{{
  36. * $this->set(compact('posts', 'users', 'stuff'));
  37. * $this->set('_serialize', array('posts', 'users'));
  38. * }}}
  39. *
  40. * The above would generate a XML object that looks like:
  41. *
  42. * `<response><posts>...</posts><users>...</users></response>`
  43. *
  44. * If you don't use the `_serialize` key, you will need a view. You can use extended
  45. * views to provide layout like functionality.
  46. *
  47. * @package Cake.View
  48. * @since CakePHP(tm) v 2.1.0
  49. */
  50. class XmlView extends View {
  51. /**
  52. * The subdirectory. XML views are always in xml.
  53. *
  54. * @var string
  55. */
  56. public $subDir = 'xml';
  57. /**
  58. * Constructor
  59. *
  60. * @param Controller $controller
  61. */
  62. public function __construct(Controller $controller = null) {
  63. parent::__construct($controller);
  64. if (isset($controller->response) && $controller->response instanceof CakeResponse) {
  65. $controller->response->type('xml');
  66. }
  67. }
  68. /**
  69. * Render a XML view.
  70. *
  71. * Uses the special '_serialize' parameter to convert a set of
  72. * view variables into a XML response. Makes generating simple
  73. * XML responses very easy. You can omit the '_serialize' parameter,
  74. * and use a normal view + layout as well.
  75. *
  76. * @param string $view The view being rendered.
  77. * @param string $layout The layout being rendered.
  78. * @return string The rendered view.
  79. */
  80. public function render($view = null, $layout = null) {
  81. if (isset($this->viewVars['_serialize'])) {
  82. return $this->_serialize($this->viewVars['_serialize']);
  83. }
  84. if ($view !== false && $this->_getViewFileName($view)) {
  85. return parent::render($view, false);
  86. }
  87. }
  88. /**
  89. * Serialize view vars
  90. *
  91. * @param array $serialize The viewVars that need to be serialized
  92. * @return string The serialized data
  93. */
  94. protected function _serialize($serialize) {
  95. $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
  96. if (is_array($serialize)) {
  97. $data = array($rootNode => array());
  98. foreach ($serialize as $key) {
  99. $data[$rootNode][$key] = $this->viewVars[$key];
  100. }
  101. } else {
  102. $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  103. if (is_array($data) && Set::numeric(array_keys($data))) {
  104. $data = array($rootNode => array($serialize => $data));
  105. }
  106. }
  107. return Xml::fromArray($data)->asXML();
  108. }
  109. }