XmlView.php 4.0 KB

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