XmlView.php 4.4 KB

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