PageRenderTime 1371ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/object.php

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