comparison cake/libs/object.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
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 versions 4 and 5
9 *
10 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11 * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
17 * @link http://cakephp.org CakePHP(tm) Project
18 * @package cake
19 * @subpackage cake.cake.libs
20 * @since CakePHP(tm) v 0.2.9
21 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22 */
23
24 /**
25 * Object class, allowing __construct and __destruct in PHP4.
26 *
27 * Also includes methods for logging and the special method RequestAction,
28 * to call other Controllers' Actions from anywhere.
29 *
30 * @package cake
31 * @subpackage cake.cake.libs
32 */
33 class Object {
34
35 /**
36 * A hack to support __construct() on PHP 4
37 * Hint: descendant classes have no PHP4 class_name() constructors,
38 * so this constructor gets called first and calls the top-layer __construct()
39 * which (if present) should call parent::__construct()
40 *
41 * @return Object
42 */
43 function Object() {
44 $args = func_get_args();
45 if (method_exists($this, '__destruct')) {
46 register_shutdown_function (array(&$this, '__destruct'));
47 }
48 call_user_func_array(array(&$this, '__construct'), $args);
49 }
50
51 /**
52 * Class constructor, overridden in descendant classes.
53 */
54 function __construct() {
55 }
56
57 /**
58 * Object-to-string conversion.
59 * Each class can override this method as necessary.
60 *
61 * @return string The name of this class
62 * @access public
63 */
64 function toString() {
65 $class = get_class($this);
66 return $class;
67 }
68
69 /**
70 * Calls a controller's method from any location. Can be used to connect controllers together
71 * or tie plugins into a main application. requestAction can be used to return rendered views
72 * or fetch the return value from controller actions.
73 *
74 * @param mixed $url String or array-based url.
75 * @param array $extra if array includes the key "return" it sets the AutoRender to true.
76 * @return mixed Boolean true or false on success/failure, or contents
77 * of rendered action if 'return' is set in $extra.
78 * @access public
79 */
80 function requestAction($url, $extra = array()) {
81 if (empty($url)) {
82 return false;
83 }
84 if (!class_exists('dispatcher')) {
85 require CAKE . 'dispatcher.php';
86 }
87 if (in_array('return', $extra, true)) {
88 $extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
89 }
90 if (is_array($url) && !isset($extra['url'])) {
91 $extra['url'] = array();
92 }
93 $params = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
94 $dispatcher = new Dispatcher;
95 return $dispatcher->dispatch($url, $params);
96 }
97
98 /**
99 * Calls a method on this object with the given parameters. Provides an OO wrapper
100 * for `call_user_func_array`
101 *
102 * @param string $method Name of the method to call
103 * @param array $params Parameter list to use when calling $method
104 * @return mixed Returns the result of the method call
105 * @access public
106 */
107 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 /**
128 * Stop execution of the current script. Wraps exit() making
129 * testing easier.
130 *
131 * @param $status see http://php.net/exit for values
132 * @return void
133 * @access public
134 */
135 function _stop($status = 0) {
136 exit($status);
137 }
138
139 /**
140 * Convience method to write a message to CakeLog. See CakeLog::write()
141 * for more information on writing to logs.
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 require LIBS . 'cake_log.php';
151 }
152 if (!is_string($msg)) {
153 $msg = print_r($msg, true);
154 }
155 return CakeLog::write($type, $msg);
156 }
157
158 /**
159 * Allows setting of multiple properties of the object in a single line of code. Will only set
160 * properties that are part of a class declaration.
161 *
162 * @param array $properties An associative array containing properties and corresponding values.
163 * @return void
164 * @access protected
165 */
166 function _set($properties = array()) {
167 if (is_array($properties) && !empty($properties)) {
168 $vars = get_object_vars($this);
169 foreach ($properties as $key => $val) {
170 if (array_key_exists($key, $vars)) {
171 $this->{$key} = $val;
172 }
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
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
198 if (class_exists('AppError')) {
199 $error = new AppError($method, $messages);
200 } else {
201 $error = new ErrorHandler($method, $messages);
202 }
203 return $error;
204 }
205
206 /**
207 * Checks for a persistent class file, if found file is opened and true returned
208 * If file is not found a file is created and false returned
209 * If used in other locations of the model you should choose a unique name for the persistent file
210 * There are many uses for this method, see manual for examples
211 *
212 * @param string $name name of the class to persist
213 * @param string $object the object to persist
214 * @return boolean Success
215 * @access protected
216 * @todo add examples to manual
217 */
218 function _persist($name, $return = null, &$object, $type = null) {
219 $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
220 if ($return === null) {
221 if (!file_exists($file)) {
222 return false;
223 } else {
224 return true;
225 }
226 }
227
228 if (!file_exists($file)) {
229 $this->_savePersistent($name, $object);
230 return false;
231 } else {
232 $this->__openPersistent($name, $type);
233 return true;
234 }
235 }
236
237 /**
238 * You should choose a unique name for the persistent file
239 *
240 * There are many uses for this method, see manual for examples
241 *
242 * @param string $name name used for object to cache
243 * @param object $object the object to persist
244 * @return boolean true on save, throws error if file can not be created
245 * @access protected
246 */
247 function _savePersistent($name, &$object) {
248 $file = 'persistent' . DS . strtolower($name) . '.php';
249 $objectArray = array(&$object);
250 $data = str_replace('\\', '\\\\', serialize($objectArray));
251 $data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
252 $duration = '+999 days';
253 if (Configure::read() >= 1) {
254 $duration = '+10 seconds';
255 }
256 cache($file, $data, $duration);
257 }
258
259 /**
260 * Open the persistent class file for reading
261 * Used by Object::_persist()
262 *
263 * @param string $name Name of persisted class
264 * @param string $type Type of persistance (e.g: registry)
265 * @return void
266 * @access private
267 */
268 function __openPersistent($name, $type = null) {
269 $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
270 include($file);
271
272 switch ($type) {
273 case 'registry':
274 $vars = unserialize(${$name});
275 foreach ($vars['0'] as $key => $value) {
276 if (strpos($key, '_behavior') !== false) {
277 App::import('Behavior', Inflector::classify(substr($key, 0, -9)));
278 } else {
279 App::import('Model', Inflector::camelize($key));
280 }
281 unset ($value);
282 }
283 unset($vars);
284 $vars = unserialize(${$name});
285 foreach ($vars['0'] as $key => $value) {
286 ClassRegistry::addObject($key, $value);
287 unset ($value);
288 }
289 unset($vars);
290 break;
291 default:
292 $vars = unserialize(${$name});
293 $this->{$name} = $vars['0'];
294 unset($vars);
295 break;
296 }
297 }
298 }