1
|
<?php
|
2
|
/* SVN FILE: $Id: object.php 4409 2007-02-02 13:20:59Z phpnut $ */
|
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-2007, Cake Software Foundation, Inc.
|
13
|
* 1785 E. Sahara Avenue, Suite 490-204
|
14
|
* Las Vegas, Nevada 89104
|
15
|
*
|
16
|
* Licensed under The MIT License
|
17
|
* Redistributions of files must retain the above copyright notice.
|
18
|
*
|
19
|
* @filesource
|
20
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
21
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
22
|
* @package cake
|
23
|
* @subpackage cake.cake.libs
|
24
|
* @since CakePHP(tm) v 0.2.9
|
25
|
* @version $Revision: 4409 $
|
26
|
* @modifiedby $LastChangedBy: phpnut $
|
27
|
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
|
28
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
29
|
*/
|
30
|
/**
|
31
|
* Object class, allowing __construct and __destruct in PHP4.
|
32
|
*
|
33
|
* Also includes methods for logging and the special method RequestAction,
|
34
|
* to call other Controllers' Actions from anywhere.
|
35
|
*
|
36
|
* @package cake
|
37
|
* @subpackage cake.cake.libs
|
38
|
*/
|
39
|
class Object{
|
40
|
/**
|
41
|
* Log object
|
42
|
*
|
43
|
* @var object
|
44
|
* @access protected
|
45
|
*/
|
46
|
var $_log = null;
|
47
|
/**
|
48
|
* A hack to support __construct() on PHP 4
|
49
|
* Hint: descendant classes have no PHP4 class_name() constructors,
|
50
|
* so this constructor gets called first and calls the top-layer __construct()
|
51
|
* which (if present) should call parent::__construct()
|
52
|
*
|
53
|
* @return Object
|
54
|
* @access public
|
55
|
*/
|
56
|
function Object() {
|
57
|
$args = func_get_args();
|
58
|
if (method_exists($this, '__destruct')) {
|
59
|
register_shutdown_function (array(&$this, '__destruct'));
|
60
|
}
|
61
|
call_user_func_array(array(&$this, '__construct'), $args);
|
62
|
}
|
63
|
/**
|
64
|
* Class constructor, overridden in descendant classes.
|
65
|
*
|
66
|
* @abstract
|
67
|
* @access public
|
68
|
*/
|
69
|
function __construct() {
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Object-to-string conversion.
|
74
|
* Each class can override this method as necessary.
|
75
|
*
|
76
|
* @return string The name of this class
|
77
|
* @access public
|
78
|
*/
|
79
|
function toString() {
|
80
|
$class = get_class($this);
|
81
|
return $class;
|
82
|
}
|
83
|
/**
|
84
|
* Calls a controller's method from any location. Allows for
|
85
|
* controllers to communicate with each other.
|
86
|
*
|
87
|
* @param string $url URL in the form of Cake URL ("/controller/method/parameter")
|
88
|
* @param array $extra If array includes the key "return" it sets the AutoRender to true.
|
89
|
* @return boolean Success
|
90
|
* @access public
|
91
|
*/
|
92
|
function requestAction($url, $extra = array()) {
|
93
|
if (!empty($url)) {
|
94
|
$dispatcher =& new Dispatcher();
|
95
|
if(isset($this->plugin)){
|
96
|
$extra['plugin'] = $this->plugin;
|
97
|
}
|
98
|
if (in_array('return', $extra)) {
|
99
|
$extra['return'] = 0;
|
100
|
$extra['bare'] = 1;
|
101
|
$extra['requested'] = 1;
|
102
|
ob_start();
|
103
|
$out = $dispatcher->dispatch($url, $extra);
|
104
|
$out = ob_get_clean();
|
105
|
return $out;
|
106
|
} else {
|
107
|
$extra['return'] = 1;
|
108
|
$extra['bare'] = 1;
|
109
|
$extra['requested'] = 1;
|
110
|
return $dispatcher->dispatch($url, $extra);
|
111
|
}
|
112
|
} else {
|
113
|
return false;
|
114
|
}
|
115
|
}
|
116
|
/**
|
117
|
* API for logging events.
|
118
|
*
|
119
|
* @param string $msg Log message
|
120
|
* @param int $type Error type constant. Defined in app/config/core.php.
|
121
|
* @access
|
122
|
*/
|
123
|
function log($msg, $type = LOG_ERROR) {
|
124
|
if (!class_exists('CakeLog')) {
|
125
|
uses('cake_log');
|
126
|
}
|
127
|
|
128
|
if (is_null($this->_log)) {
|
129
|
$this->_log = new CakeLog();
|
130
|
}
|
131
|
|
132
|
if (!is_string($msg)) {
|
133
|
ob_start();
|
134
|
print_r ($msg);
|
135
|
$msg=ob_get_contents();
|
136
|
ob_end_clean();
|
137
|
}
|
138
|
|
139
|
switch($type) {
|
140
|
case LOG_DEBUG:
|
141
|
return $this->_log->write('debug', $msg);
|
142
|
break;
|
143
|
default:
|
144
|
return $this->_log->write('error', $msg);
|
145
|
break;
|
146
|
}
|
147
|
}
|
148
|
/**
|
149
|
* Used to report user friendly errors.
|
150
|
* If there is a file app/error.php this file will be loaded
|
151
|
* error.php is the AppError class it should extend ErrorHandler class.
|
152
|
*
|
153
|
* @param string $method Method to be called in the error class (AppError or ErrorHandler classes)
|
154
|
* @param array $messages Message that is to be displayed by the error class
|
155
|
* @return error message
|
156
|
* @access public
|
157
|
*/
|
158
|
function cakeError($method, $messages) {
|
159
|
if (!class_exists('ErrorHandler')) {
|
160
|
uses('error');
|
161
|
if (file_exists(APP . 'error.php')) {
|
162
|
include_once (APP . 'error.php');
|
163
|
}
|
164
|
}
|
165
|
|
166
|
if (class_exists('AppError')) {
|
167
|
$error = new AppError($method, $messages);
|
168
|
} else {
|
169
|
$error = new ErrorHandler($method, $messages);
|
170
|
}
|
171
|
return $error;
|
172
|
}
|
173
|
/**
|
174
|
* Checks for a persistent class file, if found file is opened and true returned
|
175
|
* If file is not found a file is created and false returned
|
176
|
*
|
177
|
* There are many uses for this method, see manual for examples also art of
|
178
|
* the cache system
|
179
|
*
|
180
|
* @param string $name name of class to persist
|
181
|
* @param boolean $return
|
182
|
* @param object $object
|
183
|
* @param string $type
|
184
|
* @return boolean
|
185
|
* @todo add examples to manual
|
186
|
* @access protected
|
187
|
*/
|
188
|
function _persist($name, $return = null, &$object, $type = null) {
|
189
|
$file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
|
190
|
if ($return === null) {
|
191
|
if (!file_exists($file)) {
|
192
|
return false;
|
193
|
} else {
|
194
|
return true;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
if (!file_exists($file)) {
|
199
|
$this->_savePersistent($name, $object);
|
200
|
return false;
|
201
|
} else {
|
202
|
$this->__openPersistent($name, $type);
|
203
|
return true;
|
204
|
}
|
205
|
}
|
206
|
/**
|
207
|
* You should choose a unique name for the persistent file
|
208
|
*
|
209
|
* There are many uses for this method, see manual for examples also part of
|
210
|
* the cache system
|
211
|
*
|
212
|
* @param string $name name used for object to cache
|
213
|
* @param object $object the object to persist
|
214
|
* @return true on save, throws error if file can not be created
|
215
|
* @access protected
|
216
|
*/
|
217
|
function _savePersistent($name, &$object) {
|
218
|
$file = 'persistent' . DS . strtolower($name) . '.php';
|
219
|
$objectArray = array(&$object);
|
220
|
$data = str_replace('\\', '\\\\', serialize($objectArray));
|
221
|
$data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
|
222
|
cache($file, $data, '+1 day');
|
223
|
}
|
224
|
/**
|
225
|
* Open the persistent class file for reading
|
226
|
* Used by Object::_persist(), part of the cache
|
227
|
* system
|
228
|
*
|
229
|
* @param string $name Name of the persistant file
|
230
|
* @param string $type
|
231
|
* @access private
|
232
|
*/
|
233
|
function __openPersistent($name, $type = null) {
|
234
|
$file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
|
235
|
include($file);
|
236
|
|
237
|
switch($type) {
|
238
|
case 'registry':
|
239
|
$vars = unserialize(${$name});
|
240
|
foreach($vars['0'] as $key => $value) {
|
241
|
loadModel(Inflector::classify($key));
|
242
|
}
|
243
|
unset($vars);
|
244
|
$vars = unserialize(${$name});
|
245
|
foreach($vars['0'] as $key => $value) {
|
246
|
ClassRegistry::addObject($key, $value);
|
247
|
unset ($value);
|
248
|
}
|
249
|
unset($vars);
|
250
|
break;
|
251
|
default:
|
252
|
$vars = unserialize(${$name});
|
253
|
$this->{$name} = $vars['0'];
|
254
|
unset($vars);
|
255
|
break;
|
256
|
}
|
257
|
}
|
258
|
}
|
259
|
?>
|