Object.php 6.0 KB

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