SerializedView.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View;
  16. use Cake\Event\EventManager;
  17. use Cake\Http\Response;
  18. use Cake\Http\ServerRequest;
  19. use RuntimeException;
  20. /**
  21. * Parent class for view classes generating serialized outputs like JsonView and XmlView.
  22. */
  23. abstract class SerializedView extends View
  24. {
  25. /**
  26. * Response type.
  27. *
  28. * @var string
  29. */
  30. protected $_responseType;
  31. /**
  32. * Constructor
  33. *
  34. * @param \Cake\Http\ServerRequest|null $request Request instance.
  35. * @param \Cake\Http\Response|null $response Response instance.
  36. * @param \Cake\Event\EventManager|null $eventManager EventManager instance.
  37. * @param array $viewOptions An array of view options
  38. */
  39. public function __construct(
  40. ServerRequest $request = null,
  41. Response $response = null,
  42. EventManager $eventManager = null,
  43. array $viewOptions = []
  44. ) {
  45. if ($response && $response instanceof Response) {
  46. $response = $response->withType($this->_responseType);
  47. }
  48. parent::__construct($request, $response, $eventManager, $viewOptions);
  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. * Serialize view vars.
  63. *
  64. * @param array|string $serialize The name(s) of the view variable(s) that
  65. * need(s) to be serialized
  66. * @return string The serialized data
  67. */
  68. abstract protected function _serialize($serialize);
  69. /**
  70. * Render view template or return serialized data.
  71. *
  72. * ### Special parameters
  73. * `_serialize` To convert a set of view variables into a serialized form.
  74. * Its value can be a string for single variable name or array for multiple
  75. * names. If true all view variables will be serialized. If unset normal
  76. * view template will be rendered.
  77. *
  78. * @param string|bool|null $view The view being rendered.
  79. * @param string|null $layout The layout being rendered.
  80. * @return string|null The rendered view.
  81. */
  82. public function render($view = null, $layout = null)
  83. {
  84. $serialize = false;
  85. if (isset($this->viewVars['_serialize'])) {
  86. $serialize = $this->viewVars['_serialize'];
  87. }
  88. if ($serialize !== false) {
  89. $result = $this->_serialize($serialize);
  90. if ($result === false) {
  91. throw new RuntimeException('Serialization of View data failed.');
  92. }
  93. return (string)$result;
  94. }
  95. if ($view !== false && $this->_getViewFileName($view)) {
  96. return parent::render($view, false);
  97. }
  98. }
  99. }