PageRenderTime 52ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Core/Object.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 209 lines | 97 code | 12 blank | 100 comment | 14 complexity | 68066e2aec1413e1d7afbb303190144a MD5 | raw file
  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. * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  54. * URL. You should use URL formats that are compatible with Router::reverse()
  55. *
  56. * #### Passing POST and GET data
  57. *
  58. * POST and GET data can be simulated in requestAction. Use `$extra['url']` for
  59. * GET data. The `$extra['data']` parameter allows POST data simulation.
  60. *
  61. * @param mixed $url String or array-based url. Unlike other url arrays in CakePHP, this
  62. * url will not automatically handle passed and named arguments in the $url parameter.
  63. * @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
  64. * also be used to submit GET/POST data, and named/passed arguments.
  65. * @return mixed Boolean true or false on success/failure, or contents
  66. * of rendered action if 'return' is set in $extra.
  67. */
  68. public function requestAction($url, $extra = array()) {
  69. if (empty($url)) {
  70. return false;
  71. }
  72. App::uses('Dispatcher', 'Routing');
  73. if (($index = array_search('return', $extra)) !== false) {
  74. $extra['return'] = 0;
  75. $extra['autoRender'] = 1;
  76. unset($extra[$index]);
  77. }
  78. if (is_array($url) && !isset($extra['url'])) {
  79. $extra['url'] = 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)) {
  85. $request = new CakeRequest($url);
  86. } elseif (is_array($url)) {
  87. $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
  88. $params = array_merge($params, $extra);
  89. $request = new CakeRequest(Router::reverse($params), false);
  90. }
  91. if (isset($data)) {
  92. $request->data = $data;
  93. }
  94. $dispatcher = new Dispatcher();
  95. $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
  96. Router::popRequest();
  97. return $result;
  98. }
  99. /**
  100. * Calls a method on this object with the given parameters. Provides an OO wrapper
  101. * for `call_user_func_array`
  102. *
  103. * @param string $method Name of the method to call
  104. * @param array $params Parameter list to use when calling $method
  105. * @return mixed Returns the result of the method call
  106. */
  107. public function dispatchMethod($method, $params = array()) {
  108. switch (count($params)) {
  109. case 0:
  110. return $this->{$method}();
  111. case 1:
  112. return $this->{$method}($params[0]);
  113. case 2:
  114. return $this->{$method}($params[0], $params[1]);
  115. case 3:
  116. return $this->{$method}($params[0], $params[1], $params[2]);
  117. case 4:
  118. return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
  119. case 5:
  120. return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
  121. default:
  122. return call_user_func_array(array(&$this, $method), $params);
  123. break;
  124. }
  125. }
  126. /**
  127. * Stop execution of the current script. Wraps exit() making
  128. * testing easier.
  129. *
  130. * @param integer|string $status see http://php.net/exit for values
  131. * @return void
  132. */
  133. protected function _stop($status = 0) {
  134. exit($status);
  135. }
  136. /**
  137. * Convenience method to write a message to CakeLog. See CakeLog::write()
  138. * for more information on writing to logs.
  139. *
  140. * @param string $msg Log message
  141. * @param integer $type Error type constant. Defined in app/Config/core.php.
  142. * @return boolean Success of log write
  143. */
  144. public function log($msg, $type = LOG_ERROR) {
  145. App::uses('CakeLog', 'Log');
  146. if (!is_string($msg)) {
  147. $msg = print_r($msg, true);
  148. }
  149. return CakeLog::write($type, $msg);
  150. }
  151. /**
  152. * Allows setting of multiple properties of the object in a single line of code. Will only set
  153. * properties that are part of a class declaration.
  154. *
  155. * @param array $properties An associative array containing properties and corresponding values.
  156. * @return void
  157. */
  158. protected function _set($properties = array()) {
  159. if (is_array($properties) && !empty($properties)) {
  160. $vars = get_object_vars($this);
  161. foreach ($properties as $key => $val) {
  162. if (array_key_exists($key, $vars)) {
  163. $this->{$key} = $val;
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Merges this objects $property with the property in $class' definition.
  170. * This classes value for the property will be merged on top of $class'
  171. *
  172. * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
  173. * this method as an empty function.
  174. *
  175. * @param array $properties The name of the properties to merge.
  176. * @param string $class The class to merge the property with.
  177. * @param boolean $normalize Set to true to run the properties through Set::normalize() before merging.
  178. * @return void
  179. */
  180. protected function _mergeVars($properties, $class, $normalize = true) {
  181. $classProperties = get_class_vars($class);
  182. foreach ($properties as $var) {
  183. if (
  184. isset($classProperties[$var]) &&
  185. !empty($classProperties[$var]) &&
  186. is_array($this->{$var}) &&
  187. $this->{$var} != $classProperties[$var]
  188. ) {
  189. if ($normalize) {
  190. $classProperties[$var] = Set::normalize($classProperties[$var]);
  191. $this->{$var} = Set::normalize($this->{$var});
  192. }
  193. $this->{$var} = Set::merge($classProperties[$var], $this->{$var});
  194. }
  195. }
  196. }
  197. }