ViewVarsTrait.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c), Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\View;
  15. use Cake\Event\EventDispatcherInterface;
  16. /**
  17. * Provides the set() method for collecting template context.
  18. *
  19. * Once collected context data can be passed to another object.
  20. * This is done in Controller, TemplateTask and View for example.
  21. *
  22. * @property array $_validViewOptions
  23. */
  24. trait ViewVarsTrait
  25. {
  26. /**
  27. * The name of default View class.
  28. *
  29. * @var string
  30. * @deprecated 3.1.0 Use `$this->viewBuilder()->getClassName()`/`$this->viewBuilder()->setClassName()` instead.
  31. */
  32. public $viewClass;
  33. /**
  34. * Variables for the view
  35. *
  36. * @var array
  37. */
  38. public $viewVars = [];
  39. /**
  40. * The view builder instance being used.
  41. *
  42. * @var \Cake\View\ViewBuilder
  43. */
  44. protected $_viewBuilder;
  45. /**
  46. * Get the view builder being used.
  47. *
  48. * @return \Cake\View\ViewBuilder
  49. */
  50. public function viewBuilder()
  51. {
  52. if (!isset($this->_viewBuilder)) {
  53. $this->_viewBuilder = new ViewBuilder();
  54. }
  55. return $this->_viewBuilder;
  56. }
  57. /**
  58. * Constructs the view class instance based on the current configuration.
  59. *
  60. * @param string|null $viewClass Optional namespaced class name of the View class to instantiate.
  61. * @return \Cake\View\View
  62. * @throws \Cake\View\Exception\MissingViewException If view class was not found.
  63. */
  64. public function createView($viewClass = null)
  65. {
  66. $builder = $this->viewBuilder();
  67. if ($viewClass === null && $builder->getClassName() === null) {
  68. $builder->setClassName($this->viewClass);
  69. unset($this->viewClass);
  70. }
  71. if ($viewClass) {
  72. $builder->setClassName($viewClass);
  73. }
  74. $validViewOptions = isset($this->_validViewOptions) ? $this->_validViewOptions : [];
  75. $viewOptions = [];
  76. foreach ($validViewOptions as $option) {
  77. if (property_exists($this, $option)) {
  78. $viewOptions[$option] = $this->{$option};
  79. }
  80. }
  81. $deprecatedOptions = [
  82. 'layout' => 'setLayout',
  83. 'view' => 'setTemplate',
  84. 'theme' => 'setTheme',
  85. 'autoLayout' => 'enableAutoLayout',
  86. 'viewPath' => 'setTemplatePath',
  87. 'layoutPath' => 'setLayoutPath',
  88. ];
  89. foreach ($deprecatedOptions as $old => $new) {
  90. if (property_exists($this, $old)) {
  91. $builder->{$new}($this->{$old});
  92. $message = sprintf(
  93. 'Property $%s is deprecated. Use $this->viewBuilder()->%s() instead in beforeRender().',
  94. $old,
  95. $new
  96. );
  97. deprecationWarning($message);
  98. }
  99. }
  100. foreach (['name', 'helpers', 'plugin'] as $prop) {
  101. if (isset($this->{$prop})) {
  102. $method = 'set' . ucfirst($prop);
  103. $builder->{$method}($this->{$prop});
  104. }
  105. }
  106. $builder->setOptions($viewOptions);
  107. return $builder->build(
  108. $this->viewVars,
  109. isset($this->request) ? $this->request : null,
  110. isset($this->response) ? $this->response : null,
  111. $this instanceof EventDispatcherInterface ? $this->getEventManager() : null
  112. );
  113. }
  114. /**
  115. * Saves a variable or an associative array of variables for use inside a template.
  116. *
  117. * @param string|array $name A string or an array of data.
  118. * @param mixed $value Value in case $name is a string (which then works as the key).
  119. * Unused if $name is an associative array, otherwise serves as the values to $name's keys.
  120. * @return $this
  121. */
  122. public function set($name, $value = null)
  123. {
  124. if (is_array($name)) {
  125. if (is_array($value)) {
  126. $data = array_combine($name, $value);
  127. } else {
  128. $data = $name;
  129. }
  130. } else {
  131. $data = [$name => $value];
  132. }
  133. $this->viewVars = $data + $this->viewVars;
  134. return $this;
  135. }
  136. /**
  137. * Get/Set valid view options in the object's _validViewOptions property. The property is
  138. * created as an empty array if it is not set. If called without any parameters it will
  139. * return the current list of valid view options. See `createView()`.
  140. *
  141. * @param string|array|null $options string or array of string to be appended to _validViewOptions.
  142. * @param bool $merge Whether to merge with or override existing valid View options.
  143. * Defaults to `true`.
  144. * @return array The updated view options as an array.
  145. * @deprecated 3.7.0 Use ViewBuilder::setOptions() or any one of it's setter methods instead.
  146. */
  147. public function viewOptions($options = null, $merge = true)
  148. {
  149. deprecationWarning(
  150. 'ViewVarsTrait::viewOptions() is deprecated, used ViewBuilder::setOptions() instead.'
  151. );
  152. if (!isset($this->_validViewOptions)) {
  153. $this->_validViewOptions = [];
  154. }
  155. if ($options === null) {
  156. return $this->_validViewOptions;
  157. }
  158. if (!$merge) {
  159. return $this->_validViewOptions = (array)$options;
  160. }
  161. return $this->_validViewOptions = array_merge($this->_validViewOptions, (array)$options);
  162. }
  163. }