XmlView.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * Render a XML view.
  71. *
  72. * Uses the special '_serialize' parameter to convert a set of
  73. * view variables into a XML response. Makes generating simple
  74. * XML responses very easy. You can omit the '_serialize' parameter,
  75. * and use a normal view + layout as well.
  76. *
  77. * @param string $view The view being rendered.
  78. * @param string $layout The layout being rendered.
  79. * @return string The rendered view.
  80. */
  81. public function render($view = null, $layout = null) {
  82. if (isset($this->viewVars['_serialize'])) {
  83. return $this->_serialize($this->viewVars['_serialize']);
  84. }
  85. if ($view !== false && $this->_getViewFileName($view)) {
  86. return parent::render($view, false);
  87. }
  88. }
  89. /**
  90. * Serialize view vars.
  91. *
  92. * @param array $serialize The viewVars that need to be serialized.
  93. * @return string The serialized data
  94. */
  95. protected function _serialize($serialize) {
  96. $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
  97. if (is_array($serialize)) {
  98. $data = array($rootNode => array());
  99. foreach ($serialize as $key) {
  100. $data[$rootNode][$key] = $this->viewVars[$key];
  101. }
  102. } else {
  103. $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  104. if (is_array($data) && Set::numeric(array_keys($data))) {
  105. $data = array($rootNode => array($serialize => $data));
  106. }
  107. }
  108. return Xml::fromArray($data)->asXML();
  109. }
  110. }