1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10: * @link http://cakephp.org CakePHP(tm) Project
11: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
12: */
13:
14: App::uses('View', 'View');
15: App::uses('Xml', 'Utility');
16:
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: /**
55: * The subdirectory. XML views are always in xml.
56: *
57: * @var string
58: */
59: public $subDir = 'xml';
60:
61: /**
62: * Constructor
63: *
64: * @param Controller $controller
65: */
66: public function __construct(Controller $controller = null) {
67: parent::__construct($controller);
68:
69: if (isset($controller->response) && $controller->response instanceof CakeResponse) {
70: $controller->response->type('xml');
71: }
72: }
73:
74: /**
75: * Render a XML view.
76: *
77: * Uses the special '_serialize' parameter to convert a set of
78: * view variables into a XML response. Makes generating simple
79: * XML responses very easy. You can omit the '_serialize' parameter,
80: * and use a normal view + layout as well.
81: *
82: * @param string $view The view being rendered.
83: * @param string $layout The layout being rendered.
84: * @return string The rendered view.
85: */
86: public function render($view = null, $layout = null) {
87: if (isset($this->viewVars['_serialize'])) {
88: $serialize = $this->viewVars['_serialize'];
89: if (is_array($serialize)) {
90: $data = array('response' => array());
91: foreach ($serialize as $key) {
92: $data['response'][$key] = $this->viewVars[$key];
93: }
94: } else {
95: $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
96: if (is_array($data) && Set::numeric(array_keys($data))) {
97: $data = array('response' => array($serialize => $data));
98: }
99: }
100: $content = Xml::fromArray($data)->asXML();
101: return $content;
102: }
103: if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
104: if (!$this->_helpersLoaded) {
105: $this->loadHelpers();
106: }
107: $content = $this->_render($viewFileName);
108: $this->Blocks->set('content', (string)$content);
109: return $content;
110: }
111: }
112:
113: }
114: