XmlView.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * @since 2.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View;
  16. use Cake\Core\Configure;
  17. use Cake\Event\EventManager;
  18. use Cake\Network\Request;
  19. use Cake\Network\Response;
  20. use Cake\Utility\Hash;
  21. use Cake\Utility\Xml;
  22. /**
  23. * A view class that is used for creating XML responses.
  24. *
  25. * By setting the '_serialize' key in your controller, you can specify a view variable
  26. * that should be serialized to XML and used as the response for the request.
  27. * This allows you to omit views + layouts, if your just need to emit a single view
  28. * variable as the XML response.
  29. *
  30. * In your controller, you could do the following:
  31. *
  32. * ```
  33. * $this->set(['posts' => $posts, '_serialize' => true]);
  34. * ```
  35. *
  36. * When the view is rendered, the `$posts` view variable will be serialized
  37. * into XML.
  38. *
  39. * **Note** The view variable you specify must be compatible with Xml::fromArray().
  40. *
  41. * You can also define `'_serialize'` as an array. This will create an additional
  42. * top level element named `<response>` containing all the named view variables:
  43. *
  44. * ```
  45. * $this->set(compact('posts', 'users', 'stuff'));
  46. * $this->set('_serialize', true);
  47. * ```
  48. *
  49. * The above would generate a XML object that looks like:
  50. *
  51. * `<response><posts>...</posts><users>...</users></response>`
  52. *
  53. * You can also set `'_serialize'` to a string or array to serialize only the
  54. * specified view variables.
  55. *
  56. * If you don't use the `_serialize` key, you will need a view. You can use extended
  57. * views to provide layout like functionality.
  58. */
  59. class XmlView extends SerializedView
  60. {
  61. /**
  62. * XML layouts are located in the xml sub directory of `Layouts/`
  63. *
  64. * @var string
  65. */
  66. public $layoutPath = 'xml';
  67. /**
  68. * XML views are located in the 'xml' sub directory for controllers' views.
  69. *
  70. * @var string
  71. */
  72. public $subDir = 'xml';
  73. /**
  74. * Response type.
  75. *
  76. * @var string
  77. */
  78. protected $_responseType = 'xml';
  79. /**
  80. * List of special view vars.
  81. *
  82. * @var array
  83. */
  84. protected $_specialVars = ['_serialize', '_rootNode', '_xmlOptions'];
  85. /**
  86. * Serialize view vars.
  87. *
  88. * ### Special parameters
  89. * `_xmlOptions` You can set an array of custom options for Xml::fromArray() this way, e.g.
  90. * 'format' as 'attributes' instead of 'tags'.
  91. *
  92. * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
  93. * @return string The serialized data
  94. */
  95. protected function _serialize($serialize)
  96. {
  97. $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
  98. if ($serialize === true) {
  99. $serialize = array_diff(
  100. array_keys($this->viewVars),
  101. $this->_specialVars
  102. );
  103. if (empty($serialize)) {
  104. $serialize = null;
  105. } elseif (count($serialize) === 1) {
  106. $serialize = current($serialize);
  107. }
  108. }
  109. if (is_array($serialize)) {
  110. $data = [$rootNode => []];
  111. foreach ($serialize as $alias => $key) {
  112. if (is_numeric($alias)) {
  113. $alias = $key;
  114. }
  115. if (array_key_exists($key, $this->viewVars)) {
  116. $data[$rootNode][$alias] = $this->viewVars[$key];
  117. }
  118. }
  119. } else {
  120. $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  121. if (is_array($data) && Hash::numeric(array_keys($data))) {
  122. $data = [$rootNode => [$serialize => $data]];
  123. }
  124. }
  125. $options = [];
  126. if (isset($this->viewVars['_xmlOptions'])) {
  127. $options = $this->viewVars['_xmlOptions'];
  128. }
  129. if (Configure::read('debug')) {
  130. $options['pretty'] = true;
  131. }
  132. return Xml::fromArray($data, $options)->asXML();
  133. }
  134. }