Object.php 6.0 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.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 (in_array('return', $extra, true)) {
  64. $extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
  65. }
  66. if (is_array($url) && !isset($extra['url'])) {
  67. $extra['url'] = array();
  68. }
  69. $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
  70. if (is_string($url)) {
  71. $request = new CakeRequest($url);
  72. } elseif (is_array($url)) {
  73. $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
  74. $params = array_merge($params, $extra);
  75. $request = new CakeRequest(Router::reverse($params), false);
  76. if (isset($params['data'])) {
  77. $request->data = $params['data'];
  78. }
  79. }
  80. $dispatcher = new Dispatcher();
  81. $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
  82. Router::popRequest();
  83. return $result;
  84. }
  85. /**
  86. * Calls a method on this object with the given parameters. Provides an OO wrapper
  87. * for `call_user_func_array`
  88. *
  89. * @param string $method Name of the method to call
  90. * @param array $params Parameter list to use when calling $method
  91. * @return mixed Returns the result of the method call
  92. */
  93. public function dispatchMethod($method, $params = array()) {
  94. switch (count($params)) {
  95. case 0:
  96. return $this->{$method}();
  97. case 1:
  98. return $this->{$method}($params[0]);
  99. case 2:
  100. return $this->{$method}($params[0], $params[1]);
  101. case 3:
  102. return $this->{$method}($params[0], $params[1], $params[2]);
  103. case 4:
  104. return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
  105. case 5:
  106. return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
  107. default:
  108. return call_user_func_array(array(&$this, $method), $params);
  109. break;
  110. }
  111. }
  112. /**
  113. * Stop execution of the current script. Wraps exit() making
  114. * testing easier.
  115. *
  116. * @param integer|string $status see http://php.net/exit for values
  117. * @return void
  118. */
  119. protected function _stop($status = 0) {
  120. exit($status);
  121. }
  122. /**
  123. * Convience method to write a message to CakeLog. See CakeLog::write()
  124. * for more information on writing to logs.
  125. *
  126. * @param string $msg Log message
  127. * @param integer $type Error type constant. Defined in app/Config/core.php.
  128. * @return boolean Success of log write
  129. */
  130. public function log($msg, $type = LOG_ERROR) {
  131. if (!class_exists('CakeLog')) {
  132. require CAKE . 'cake_log.php';
  133. }
  134. if (!is_string($msg)) {
  135. $msg = print_r($msg, true);
  136. }
  137. return CakeLog::write($type, $msg);
  138. }
  139. /**
  140. * Allows setting of multiple properties of the object in a single line of code. Will only set
  141. * properties that are part of a class declaration.
  142. *
  143. * @param array $properties An associative array containing properties and corresponding values.
  144. * @return void
  145. */
  146. protected function _set($properties = array()) {
  147. if (is_array($properties) && !empty($properties)) {
  148. $vars = get_object_vars($this);
  149. foreach ($properties as $key => $val) {
  150. if (array_key_exists($key, $vars)) {
  151. $this->{$key} = $val;
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * Merges this objects $property with the property in $class' definition.
  158. * This classes value for the property will be merged on top of $class'
  159. *
  160. * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
  161. * this method as an empty function.
  162. *
  163. * @param array $properties The name of the properties to merge.
  164. * @param string $class The class to merge the property with.
  165. * @param boolean $normalize Set to true to run the properties through Set::normalize() before merging.
  166. * @return void
  167. */
  168. protected function _mergeVars($properties, $class, $normalize = true) {
  169. $classProperties = get_class_vars($class);
  170. foreach ($properties as $var) {
  171. if (
  172. isset($classProperties[$var]) &&
  173. !empty($classProperties[$var]) &&
  174. is_array($this->{$var}) &&
  175. $this->{$var} != $classProperties[$var]
  176. ) {
  177. if ($normalize) {
  178. $classProperties[$var] = Set::normalize($classProperties[$var]);
  179. $this->{$var} = Set::normalize($this->{$var});
  180. }
  181. $this->{$var} = Set::merge($classProperties[$var], $this->{$var});
  182. }
  183. }
  184. }
  185. }