PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/object.php

http://noserub.googlecode.com/
PHP | 312 lines | 152 code | 19 blank | 141 comment | 26 complexity | dd177a4c433aa228f6bb3a4f5256ce68 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Object class, allowing __construct and __destruct in PHP4.
  5. *
  6. * Also includes methods for logging and the special method RequestAction,
  7. * to call other Controllers' Actions from anywhere.
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  12. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  13. *
  14. * Licensed under The MIT License
  15. * Redistributions of files must retain the above copyright notice.
  16. *
  17. * @filesource
  18. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  19. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  20. * @package cake
  21. * @subpackage cake.cake.libs
  22. * @since CakePHP(tm) v 0.2.9
  23. * @version $Revision: 1802 $
  24. * @modifiedby $LastChangedBy$
  25. * @lastmodified $Date: 2009-08-06 09:28:59 +0200 (Thu, 06 Aug 2009) $
  26. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  27. */
  28. /**
  29. * Object class, allowing __construct and __destruct in PHP4.
  30. *
  31. * Also includes methods for logging and the special method RequestAction,
  32. * to call other Controllers' Actions from anywhere.
  33. *
  34. * @package cake
  35. * @subpackage cake.cake.libs
  36. */
  37. class Object {
  38. /**
  39. * Log object
  40. *
  41. * @var CakeLog
  42. * @access protected
  43. */
  44. var $_log = null;
  45. /**
  46. * A hack to support __construct() on PHP 4
  47. * Hint: descendant classes have no PHP4 class_name() constructors,
  48. * so this constructor gets called first and calls the top-layer __construct()
  49. * which (if present) should call parent::__construct()
  50. *
  51. * @return Object
  52. */
  53. function Object() {
  54. $args = func_get_args();
  55. if (method_exists($this, '__destruct')) {
  56. register_shutdown_function (array(&$this, '__destruct'));
  57. }
  58. call_user_func_array(array(&$this, '__construct'), $args);
  59. }
  60. /**
  61. * Class constructor, overridden in descendant classes.
  62. */
  63. function __construct() {
  64. }
  65. /**
  66. * Object-to-string conversion.
  67. * Each class can override this method as necessary.
  68. *
  69. * @return string The name of this class
  70. * @access public
  71. */
  72. function toString() {
  73. $class = get_class($this);
  74. return $class;
  75. }
  76. /**
  77. * Calls a controller's method from any location.
  78. *
  79. * @param mixed $url String or array-based url.
  80. * @param array $extra if array includes the key "return" it sets the AutoRender to true.
  81. * @return mixed Boolean true or false on success/failure, or contents
  82. * of rendered action if 'return' is set in $extra.
  83. * @access public
  84. */
  85. function requestAction($url, $extra = array()) {
  86. if (empty($url)) {
  87. return false;
  88. }
  89. if (!class_exists('dispatcher')) {
  90. require CAKE . 'dispatcher.php';
  91. }
  92. if (in_array('return', $extra, true)) {
  93. $extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
  94. }
  95. if (is_array($url) && !isset($extra['url'])) {
  96. $extra['url'] = array();
  97. }
  98. $params = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
  99. $dispatcher = new Dispatcher;
  100. return $dispatcher->dispatch($url, $params);
  101. }
  102. /**
  103. * Calls a method on this object with the given parameters. Provides an OO wrapper
  104. * for call_user_func_array, and improves performance by using straight method calls
  105. * in most cases.
  106. *
  107. * @param string $method Name of the method to call
  108. * @param array $params Parameter list to use when calling $method
  109. * @return mixed Returns the result of the method call
  110. * @access public
  111. */
  112. function dispatchMethod($method, $params = array()) {
  113. switch (count($params)) {
  114. case 0:
  115. return $this->{$method}();
  116. case 1:
  117. return $this->{$method}($params[0]);
  118. case 2:
  119. return $this->{$method}($params[0], $params[1]);
  120. case 3:
  121. return $this->{$method}($params[0], $params[1], $params[2]);
  122. case 4:
  123. return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
  124. case 5:
  125. return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
  126. default:
  127. return call_user_func_array(array(&$this, $method), $params);
  128. break;
  129. }
  130. }
  131. /**
  132. * Stop execution of the current script
  133. *
  134. * @param $status see http://php.net/exit for values
  135. * @return void
  136. * @access public
  137. */
  138. function _stop($status = 0) {
  139. exit($status);
  140. }
  141. /**
  142. * API for logging events.
  143. *
  144. * @param string $msg Log message
  145. * @param integer $type Error type constant. Defined in app/config/core.php.
  146. * @return boolean Success of log write
  147. * @access public
  148. */
  149. function log($msg, $type = LOG_ERROR) {
  150. if (!class_exists('CakeLog')) {
  151. require LIBS . 'cake_log.php';
  152. }
  153. if (is_null($this->_log)) {
  154. $this->_log = new CakeLog();
  155. }
  156. if (!is_string($msg)) {
  157. $msg = print_r($msg, true);
  158. }
  159. return $this->_log->write($type, $msg);
  160. }
  161. /**
  162. * Allows setting of multiple properties of the object in a single line of code.
  163. *
  164. * @param array $properties An associative array containing properties and corresponding values.
  165. * @return void
  166. * @access protected
  167. */
  168. function _set($properties = array()) {
  169. if (is_array($properties) && !empty($properties)) {
  170. $vars = get_object_vars($this);
  171. foreach ($properties as $key => $val) {
  172. if (array_key_exists($key, $vars)) {
  173. $this->{$key} = $val;
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * Used to report user friendly errors.
  180. * If there is a file app/error.php or app/app_error.php this file will be loaded
  181. * error.php is the AppError class it should extend ErrorHandler class.
  182. *
  183. * @param string $method Method to be called in the error class (AppError or ErrorHandler classes)
  184. * @param array $messages Message that is to be displayed by the error class
  185. * @return error message
  186. * @access public
  187. */
  188. function cakeError($method, $messages = array()) {
  189. if (!class_exists('ErrorHandler')) {
  190. App::import('Core', 'Error');
  191. if (file_exists(APP . 'error.php')) {
  192. include_once (APP . 'error.php');
  193. } elseif (file_exists(APP . 'app_error.php')) {
  194. include_once (APP . 'app_error.php');
  195. }
  196. }
  197. if (class_exists('AppError')) {
  198. $error = new AppError($method, $messages);
  199. } else {
  200. $error = new ErrorHandler($method, $messages);
  201. }
  202. return $error;
  203. }
  204. /**
  205. * Checks for a persistent class file, if found file is opened and true returned
  206. * If file is not found a file is created and false returned
  207. * If used in other locations of the model you should choose a unique name for the persistent file
  208. * There are many uses for this method, see manual for examples
  209. *
  210. * @param string $name name of the class to persist
  211. * @param string $object the object to persist
  212. * @return boolean Success
  213. * @access protected
  214. * @todo add examples to manual
  215. */
  216. function _persist($name, $return = null, &$object, $type = null) {
  217. $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
  218. if ($return === null) {
  219. if (!file_exists($file)) {
  220. return false;
  221. } else {
  222. return true;
  223. }
  224. }
  225. if (!file_exists($file)) {
  226. $this->_savePersistent($name, $object);
  227. return false;
  228. } else {
  229. $this->__openPersistent($name, $type);
  230. return true;
  231. }
  232. }
  233. /**
  234. * You should choose a unique name for the persistent file
  235. *
  236. * There are many uses for this method, see manual for examples
  237. *
  238. * @param string $name name used for object to cache
  239. * @param object $object the object to persist
  240. * @return boolean true on save, throws error if file can not be created
  241. * @access protected
  242. */
  243. function _savePersistent($name, &$object) {
  244. $file = 'persistent' . DS . strtolower($name) . '.php';
  245. $objectArray = array(&$object);
  246. $data = str_replace('\\', '\\\\', serialize($objectArray));
  247. $data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
  248. $duration = '+999 days';
  249. if (Configure::read() >= 1) {
  250. $duration = '+10 seconds';
  251. }
  252. cache($file, $data, $duration);
  253. }
  254. /**
  255. * Open the persistent class file for reading
  256. * Used by Object::_persist()
  257. *
  258. * @param string $name Name of persisted class
  259. * @param string $type Type of persistance (e.g: registry)
  260. * @return void
  261. * @access private
  262. */
  263. function __openPersistent($name, $type = null) {
  264. $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
  265. include($file);
  266. switch ($type) {
  267. case 'registry':
  268. $vars = unserialize(${$name});
  269. foreach ($vars['0'] as $key => $value) {
  270. if (strpos($key, '_behavior') !== false) {
  271. App::import('Behavior', Inflector::classify(substr($key, 0, -9)));
  272. } else {
  273. App::import('Model', Inflector::classify($key));
  274. }
  275. unset ($value);
  276. }
  277. unset($vars);
  278. $vars = unserialize(${$name});
  279. foreach ($vars['0'] as $key => $value) {
  280. ClassRegistry::addObject($key, $value);
  281. unset ($value);
  282. }
  283. unset($vars);
  284. break;
  285. default:
  286. $vars = unserialize(${$name});
  287. $this->{$name} = $vars['0'];
  288. unset($vars);
  289. break;
  290. }
  291. }
  292. }
  293. ?>