XmlView.php 4.1 KB

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