Object.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * Checks for a persistent class file, if found file is opened and true returned
  157. * If file is not found a file is created and false returned
  158. * If used in other locations of the model you should choose a unique name for the persistent file
  159. * There are many uses for this method, see manual for examples
  160. *
  161. * @param string $name name of the class to persist
  162. * @param string $object the object to persist
  163. * @return boolean Success
  164. * @access protected
  165. * @todo add examples to manual
  166. */
  167. protected function _persist($name, $return = null, &$object, $type = null) {
  168. $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
  169. if ($return === null) {
  170. if (!file_exists($file)) {
  171. return false;
  172. } else {
  173. return true;
  174. }
  175. }
  176. if (!file_exists($file)) {
  177. $this->_savePersistent($name, $object);
  178. return false;
  179. } else {
  180. $this->__openPersistent($name, $type);
  181. return true;
  182. }
  183. }
  184. /**
  185. * Merges this objects $property with the property in $class' definition.
  186. * This classes value for the property will be merged on top of $class'
  187. *
  188. * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
  189. * this method as an empty function.
  190. *
  191. * @param array $properties The name of the properties to merge.
  192. * @param sting $class The class to merge the property with.
  193. * @param boolean $normalize Set to true to run the properties through Set::normalize() before merging.
  194. * @return void
  195. */
  196. protected function _mergeVars($properties, $class, $normalize = true) {
  197. $classProperties = get_class_vars($class);
  198. foreach ($properties as $var) {
  199. if (
  200. isset($classProperties[$var]) &&
  201. !empty($classProperties[$var]) &&
  202. is_array($this->{$var}) &&
  203. $this->{$var} != $classProperties[$var]
  204. ) {
  205. if ($normalize) {
  206. $classProperties[$var] = Set::normalize($classProperties[$var]);
  207. $this->{$var} = Set::normalize($this->{$var});
  208. }
  209. $this->{$var} = Set::merge($classProperties[$var], $this->{$var});
  210. }
  211. }
  212. }
  213. /**
  214. * You should choose a unique name for the persistent file
  215. *
  216. * There are many uses for this method, see manual for examples
  217. *
  218. * @param string $name name used for object to cache
  219. * @param object $object the object to persist
  220. * @return boolean true on save, throws error if file can not be created
  221. */
  222. protected function _savePersistent($name, &$object) {
  223. $file = 'persistent' . DS . strtolower($name) . '.php';
  224. $objectArray = array(&$object);
  225. $data = str_replace('\\', '\\\\', serialize($objectArray));
  226. $data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
  227. $duration = '+999 days';
  228. if (Configure::read('debug') >= 1) {
  229. $duration = '+10 seconds';
  230. }
  231. cache($file, $data, $duration);
  232. }
  233. /**
  234. * Open the persistent class file for reading
  235. * Used by Object::_persist()
  236. *
  237. * @param string $name Name of persisted class
  238. * @param string $type Type of persistance (e.g: registry)
  239. * @return void
  240. * @access private
  241. */
  242. private function __openPersistent($name, $type = null) {
  243. $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
  244. include($file);
  245. switch ($type) {
  246. case 'registry':
  247. $vars = unserialize(${$name});
  248. foreach ($vars['0'] as $key => $value) {
  249. if (strpos($key, '_behavior') !== false) {
  250. App::import('Behavior', Inflector::classify(substr($key, 0, -9)));
  251. } else {
  252. App::import('Model', Inflector::camelize($key));
  253. }
  254. unset ($value);
  255. }
  256. unset($vars);
  257. $vars = unserialize(${$name});
  258. foreach ($vars['0'] as $key => $value) {
  259. ClassRegistry::addObject($key, $value);
  260. unset ($value);
  261. }
  262. unset($vars);
  263. break;
  264. default:
  265. $vars = unserialize(${$name});
  266. $this->{$name} = $vars['0'];
  267. unset($vars);
  268. break;
  269. }
  270. }
  271. }