3
* Object class, allowing __construct and __destruct in PHP4.
5
* Also includes methods for logging and the special method RequestAction,
6
* to call other Controllers' Actions from anywhere.
10
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
13
* Licensed under The MIT License
14
* Redistributions of files must retain the above copyright notice.
16
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
17
* @link http://cakephp.org CakePHP(tm) Project
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)
25
* Object class, allowing __construct and __destruct in PHP4.
27
* Also includes methods for logging and the special method RequestAction,
28
* to call other Controllers' Actions from anywhere.
31
* @subpackage cake.cake.libs
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()
44
$args = func_get_args();
45
if (method_exists($this, '__destruct')) {
46
register_shutdown_function (array(&$this, '__destruct'));
48
call_user_func_array(array(&$this, '__construct'), $args);
52
* Class constructor, overridden in descendant classes.
54
function __construct() {
58
* Object-to-string conversion.
59
* Each class can override this method as necessary.
61
* @return string The name of this class
65
$class = get_class($this);
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.
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.
80
function requestAction($url, $extra = array()) {
84
if (!class_exists('dispatcher')) {
85
require CAKE . 'dispatcher.php';
87
if (in_array('return', $extra, true)) {
88
$extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
90
if (is_array($url) && !isset($extra['url'])) {
91
$extra['url'] = array();
93
$params = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
94
$dispatcher = new Dispatcher;
95
return $dispatcher->dispatch($url, $params);
99
* Calls a method on this object with the given parameters. Provides an OO wrapper
100
* for `call_user_func_array`
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
107
function dispatchMethod($method, $params = array()) {
108
switch (count($params)) {
110
return $this->{$method}();
112
return $this->{$method}($params[0]);
114
return $this->{$method}($params[0], $params[1]);
116
return $this->{$method}($params[0], $params[1], $params[2]);
118
return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
120
return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
122
return call_user_func_array(array(&$this, $method), $params);
128
* Stop execution of the current script. Wraps exit() making
131
* @param $status see http://php.net/exit for values
135
function _stop($status = 0) {
140
* Convience method to write a message to CakeLog. See CakeLog::write()
141
* for more information on writing to logs.
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
148
function log($msg, $type = LOG_ERROR) {
149
if (!class_exists('CakeLog')) {
150
require LIBS . 'cake_log.php';
152
if (!is_string($msg)) {
153
$msg = print_r($msg, true);
155
return CakeLog::write($type, $msg);
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.
162
* @param array $properties An associative array containing properties and corresponding values.
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;
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.
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
187
function cakeError($method, $messages = array()) {
188
if (!class_exists('ErrorHandler')) {
189
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');
198
if (class_exists('AppError')) {
199
$error = new AppError($method, $messages);
201
$error = new ErrorHandler($method, $messages);
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
212
* @param string $name name of the class to persist
213
* @param string $object the object to persist
214
* @return boolean Success
216
* @todo add examples to manual
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)) {
228
if (!file_exists($file)) {
229
$this->_savePersistent($name, $object);
232
$this->__openPersistent($name, $type);
238
* You should choose a unique name for the persistent file
240
* There are many uses for this method, see manual for examples
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
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';
256
cache($file, $data, $duration);
260
* Open the persistent class file for reading
261
* Used by Object::_persist()
263
* @param string $name Name of persisted class
264
* @param string $type Type of persistance (e.g: registry)
268
function __openPersistent($name, $type = null) {
269
$file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
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)));
279
App::import('Model', Inflector::camelize($key));
284
$vars = unserialize(${$name});
285
foreach ($vars['0'] as $key => $value) {
286
ClassRegistry::addObject($key, $value);
292
$vars = unserialize(${$name});
293
$this->{$name} = $vars['0'];