Object.php 5.9 KB

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