2011-10-29 20:51:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 20:51:44 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-10-29 20:51:44 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 20:51:44 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-10-29 20:51:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('View', 'View');
|
|
|
|
App::uses('Xml', 'Utility');
|
|
|
|
|
|
|
|
/**
|
2011-11-07 03:13:35 +00:00
|
|
|
* A view class that is used for creating XML responses.
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* By setting the '_serialize' key in your controller, you can specify a view variable
|
2011-11-07 03:13:35 +00:00
|
|
|
* that should be serialized to XML and used as the response for the request.
|
|
|
|
* This allows you to omit views + layouts, if your just need to emit a single view
|
|
|
|
* variable as the XML response.
|
|
|
|
*
|
|
|
|
* In your controller, you could do the following:
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
|
2011-11-07 03:13:35 +00:00
|
|
|
*
|
2012-05-23 09:39:02 +00:00
|
|
|
* When the view is rendered, the `$posts` view variable will be serialized
|
2011-11-07 03:13:35 +00:00
|
|
|
* into XML.
|
|
|
|
*
|
|
|
|
* **Note** The view variable you specify must be compatible with Xml::fromArray().
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* You can also define `'_serialize'` as an array. This will create an additional
|
2011-11-18 03:24:49 +00:00
|
|
|
* top level element named `<response>` containing all the named view variables:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* $this->set(compact('posts', 'users', 'stuff'));
|
2011-11-23 01:56:25 +00:00
|
|
|
* $this->set('_serialize', array('posts', 'users'));
|
2011-11-18 03:24:49 +00:00
|
|
|
* }}}
|
2012-05-23 09:39:02 +00:00
|
|
|
*
|
2011-11-18 03:24:49 +00:00
|
|
|
* The above would generate a XML object that looks like:
|
|
|
|
*
|
|
|
|
* `<response><posts>...</posts><users>...</users></response>`
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* If you don't use the `_serialize` key, you will need a view. You can use extended
|
2011-11-18 03:24:49 +00:00
|
|
|
* views to provide layout like functionality.
|
2011-10-29 20:51:44 +00:00
|
|
|
*
|
|
|
|
* @package Cake.View
|
2011-11-07 03:13:35 +00:00
|
|
|
* @since CakePHP(tm) v 2.1.0
|
2011-10-29 20:51:44 +00:00
|
|
|
*/
|
|
|
|
class XmlView extends View {
|
|
|
|
|
2011-11-08 03:40:39 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* The subdirectory. XML views are always in xml.
|
2011-11-08 03:40:39 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $subDir = 'xml';
|
|
|
|
|
2011-10-29 20:51:44 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param Controller $controller
|
|
|
|
*/
|
2012-02-23 14:06:25 +00:00
|
|
|
public function __construct(Controller $controller = null) {
|
2011-10-29 20:51:44 +00:00
|
|
|
parent::__construct($controller);
|
|
|
|
|
2011-11-29 02:36:13 +00:00
|
|
|
if (isset($controller->response) && $controller->response instanceof CakeResponse) {
|
2011-10-29 20:51:44 +00:00
|
|
|
$controller->response->type('xml');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a XML view.
|
|
|
|
*
|
2011-11-23 01:56:25 +00:00
|
|
|
* Uses the special '_serialize' parameter to convert a set of
|
2012-12-22 22:48:15 +00:00
|
|
|
* view variables into a XML response. Makes generating simple
|
|
|
|
* XML responses very easy. You can omit the '_serialize' parameter,
|
2011-10-29 20:51:44 +00:00
|
|
|
* and use a normal view + layout as well.
|
|
|
|
*
|
|
|
|
* @param string $view The view being rendered.
|
|
|
|
* @param string $layout The layout being rendered.
|
|
|
|
* @return string The rendered view.
|
|
|
|
*/
|
|
|
|
public function render($view = null, $layout = null) {
|
2011-11-23 01:56:25 +00:00
|
|
|
if (isset($this->viewVars['_serialize'])) {
|
2012-08-10 07:54:05 +00:00
|
|
|
return $this->_serialize($this->viewVars['_serialize']);
|
2011-11-08 03:40:39 +00:00
|
|
|
}
|
2012-12-20 12:47:03 +00:00
|
|
|
if ($view !== false && $this->_getViewFileName($view)) {
|
2012-08-22 11:13:49 +00:00
|
|
|
return parent::render($view, false);
|
2011-10-29 20:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-10 07:54:05 +00:00
|
|
|
/**
|
2013-07-05 15:19:22 +00:00
|
|
|
* Serialize view vars.
|
2012-08-10 07:54:05 +00:00
|
|
|
*
|
2013-07-05 15:19:22 +00:00
|
|
|
* @param array $serialize The viewVars that need to be serialized.
|
2012-08-10 07:54:05 +00:00
|
|
|
* @return string The serialized data
|
|
|
|
*/
|
|
|
|
protected function _serialize($serialize) {
|
2012-11-02 18:14:49 +00:00
|
|
|
$rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
|
|
|
|
|
2012-08-10 07:54:05 +00:00
|
|
|
if (is_array($serialize)) {
|
2012-11-02 18:14:49 +00:00
|
|
|
$data = array($rootNode => array());
|
2013-07-06 16:57:46 +00:00
|
|
|
foreach ($serialize as $alias => $key) {
|
|
|
|
if (is_numeric($alias)) {
|
|
|
|
$alias = $key;
|
|
|
|
}
|
|
|
|
$data[$rootNode][$alias] = $this->viewVars[$key];
|
2012-08-10 07:54:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
|
|
|
|
if (is_array($data) && Set::numeric(array_keys($data))) {
|
2012-11-02 18:14:49 +00:00
|
|
|
$data = array($rootNode => array($serialize => $data));
|
2012-08-10 07:54:05 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-02 13:39:54 +00:00
|
|
|
|
|
|
|
$options = array();
|
|
|
|
if (Configure::read('debug')) {
|
|
|
|
$options['pretty'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Xml::fromArray($data, $options)->asXML();
|
2012-08-10 07:54:05 +00:00
|
|
|
}
|
|
|
|
|
2011-10-29 20:51:44 +00:00
|
|
|
}
|