Object.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * @package Cake.Core
  13. * @since CakePHP(tm) v 0.2.9
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('CakeLog', 'Log');
  17. App::uses('Dispatcher', 'Routing');
  18. App::uses('Set', 'Utility');
  19. App::uses('CakeLog', 'Log');
  20. /**
  21. * Object class provides a few generic methods used in several subclasses.
  22. *
  23. * Also includes methods for logging and the special method RequestAction,
  24. * to call other Controllers' Actions from anywhere.
  25. *
  26. * @package Cake.Core
  27. */
  28. class Object {
  29. /**
  30. * constructor, no-op
  31. *
  32. */
  33. public function __construct() {
  34. }
  35. /**
  36. * Object-to-string conversion.
  37. * Each class can override this method as necessary.
  38. *
  39. * @return string The name of this class
  40. */
  41. public function toString() {
  42. $class = get_class($this);
  43. return $class;
  44. }
  45. /**
  46. * Calls a controller's method from any location. Can be used to connect controllers together
  47. * or tie plugins into a main application. requestAction can be used to return rendered views
  48. * or fetch the return value from controller actions.
  49. *
  50. * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  51. * URL. You should use URL formats that are compatible with Router::reverse()
  52. *
  53. * #### Passing POST and GET data
  54. *
  55. * POST and GET data can be simulated in requestAction. Use `$extra['url']` for
  56. * GET data. The `$extra['data']` parameter allows POST data simulation.
  57. *
  58. * @param string|array $url String or array-based URL. Unlike other URL arrays in CakePHP, this
  59. * URL will not automatically handle passed and named arguments in the $url parameter.
  60. * @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
  61. * also be used to submit GET/POST data, and named/passed arguments.
  62. * @return mixed Boolean true or false on success/failure, or contents
  63. * of rendered action if 'return' is set in $extra.
  64. */
  65. public function requestAction($url, $extra = array()) {
  66. if (empty($url)) {
  67. return false;
  68. }
  69. if (($index = array_search('return', $extra)) !== false) {
  70. $extra['return'] = 0;
  71. $extra['autoRender'] = 1;
  72. unset($extra[$index]);
  73. }
  74. $arrayUrl = is_array($url);
  75. if ($arrayUrl && !isset($extra['url'])) {
  76. $extra['url'] = array();
  77. }
  78. if ($arrayUrl && !isset($extra['data'])) {
  79. $extra['data'] = array();
  80. }
  81. $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
  82. $data = isset($extra['data']) ? $extra['data'] : null;
  83. unset($extra['data']);
  84. if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
  85. $url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
  86. }
  87. if (is_string($url)) {
  88. $request = new CakeRequest($url);
  89. } elseif (is_array($url)) {
  90. $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
  91. $params = array_merge($params, $extra);
  92. $request = new CakeRequest(Router::reverse($params));
  93. }
  94. if (isset($data)) {
  95. $request->data = $data;
  96. }
  97. $dispatcher = new Dispatcher();
  98. $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
  99. Router::popRequest();
  100. return $result;
  101. }
  102. /**
  103. * Calls a method on this object with the given parameters. Provides an OO wrapper
  104. * for `call_user_func_array`
  105. *
  106. * @param string $method Name of the method to call
  107. * @param array $params Parameter list to use when calling $method
  108. * @return mixed Returns the result of the method call
  109. */
  110. public function dispatchMethod($method, $params = array()) {
  111. switch (count($params)) {
  112. case 0:
  113. return $this->{$method}();
  114. case 1:
  115. return $this->{$method}($params[0]);
  116. case 2:
  117. return $this->{$method}($params[0], $params[1]);
  118. case 3:
  119. return $this->{$method}($params[0], $params[1], $params[2]);
  120. case 4:
  121. return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
  122. case 5:
  123. return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
  124. default:
  125. return call_user_func_array(array(&$this, $method), $params);
  126. }
  127. }
  128. /**
  129. * Stop execution of the current script. Wraps exit() making
  130. * testing easier.
  131. *
  132. * @param integer|string $status see http://php.net/exit for values
  133. * @return void
  134. */
  135. protected function _stop($status = 0) {
  136. exit($status);
  137. }
  138. /**
  139. * Convenience method to write a message to CakeLog. See CakeLog::write()
  140. * for more information on writing to logs.
  141. *
  142. * @param string $msg Log message
  143. * @param integer $type Error type constant. Defined in app/Config/core.php.
  144. * @return boolean Success of log write
  145. */
  146. public function log($msg, $type = LOG_ERR, $scope = null) {
  147. if (!is_string($msg)) {
  148. $msg = print_r($msg, true);
  149. }
  150. return CakeLog::write($type, $msg, $scope);
  151. }
  152. /**
  153. * Allows setting of multiple properties of the object in a single line of code. Will only set
  154. * properties that are part of a class declaration.
  155. *
  156. * @param array $properties An associative array containing properties and corresponding values.
  157. * @return void
  158. */
  159. protected function _set($properties = array()) {
  160. if (is_array($properties) && !empty($properties)) {
  161. $vars = get_object_vars($this);
  162. foreach ($properties as $key => $val) {
  163. if (array_key_exists($key, $vars)) {
  164. $this->{$key} = $val;
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * Merges this objects $property with the property in $class' definition.
  171. * This classes value for the property will be merged on top of $class'
  172. *
  173. * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
  174. * this method as an empty function.
  175. *
  176. * @param array $properties The name of the properties to merge.
  177. * @param string $class The class to merge the property with.
  178. * @param boolean $normalize Set to true to run the properties through Hash::normalize() before merging.
  179. * @return void
  180. */
  181. protected function _mergeVars($properties, $class, $normalize = true) {
  182. $classProperties = get_class_vars($class);
  183. foreach ($properties as $var) {
  184. if (
  185. isset($classProperties[$var]) &&
  186. !empty($classProperties[$var]) &&
  187. is_array($this->{$var}) &&
  188. $this->{$var} != $classProperties[$var]
  189. ) {
  190. if ($normalize) {
  191. $classProperties[$var] = Hash::normalize($classProperties[$var]);
  192. $this->{$var} = Hash::normalize($this->{$var});
  193. }
  194. $this->{$var} = Hash::merge($classProperties[$var], $this->{$var});
  195. }
  196. }
  197. }
  198. }