SerializedView.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View;
  16. use Cake\Event\EventManager;
  17. use Cake\Network\Request;
  18. use Cake\Network\Response;
  19. use RuntimeException;
  20. /**
  21. * Parent class for view classes generating serialized outputs like JsonView and XmlView.
  22. */
  23. class SerializedView extends View
  24. {
  25. /**
  26. * Response type.
  27. *
  28. * @var string
  29. */
  30. protected $_responseType;
  31. /**
  32. * Constructor
  33. *
  34. * @param \Cake\Network\Request $request Request instance.
  35. * @param \Cake\Network\Response $response Response instance.
  36. * @param \Cake\Event\EventManager $eventManager EventManager instance.
  37. * @param array $viewOptions An array of view options
  38. */
  39. public function __construct(
  40. Request $request = null,
  41. Response $response = null,
  42. EventManager $eventManager = null,
  43. array $viewOptions = []
  44. ) {
  45. parent::__construct($request, $response, $eventManager, $viewOptions);
  46. if ($response && $response instanceof Response) {
  47. $response->type($this->_responseType);
  48. }
  49. }
  50. /**
  51. * Load helpers only if serialization is disabled.
  52. *
  53. * @return void
  54. */
  55. public function loadHelpers()
  56. {
  57. if (empty($this->viewVars['_serialize'])) {
  58. parent::loadHelpers();
  59. }
  60. }
  61. /**
  62. * Render view template or return serialized data.
  63. *
  64. * ### Special parameters
  65. * `_serialize` To convert a set of view variables into a serialized form.
  66. * Its value can be a string for single variable name or array for multiple
  67. * names. If true all view variables will be serialized. If unset normal
  68. * view template will be rendered.
  69. *
  70. * @param string|null $view The view being rendered.
  71. * @param string|null $layout The layout being rendered.
  72. * @return string|null The rendered view.
  73. */
  74. public function render($view = null, $layout = null)
  75. {
  76. $serialize = false;
  77. if (isset($this->viewVars['_serialize'])) {
  78. $serialize = $this->viewVars['_serialize'];
  79. }
  80. if ($serialize !== false) {
  81. $result = $this->_serialize($serialize);
  82. if ($result === false) {
  83. throw new RuntimeException('Serialization of View data failed.');
  84. }
  85. return (string)$result;
  86. }
  87. if ($view !== false && $this->_getViewFileName($view)) {
  88. return parent::render($view, false);
  89. }
  90. }
  91. }