Object.php 6.4 KB

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