object.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: object.php 8273 2009-08-02 19:12:02Z jperras $ */
00003 /**
00004  * Object class, allowing __construct and __destruct in PHP4.
00005  *
00006  * Also includes methods for logging and the special method RequestAction,
00007  * to call other Controllers' Actions from anywhere.
00008  *
00009  * PHP versions 4 and 5
00010  *
00011  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00012  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00013  *
00014  * Licensed under The MIT License
00015  * Redistributions of files must retain the above copyright notice.
00016  *
00017  * @filesource
00018  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00019  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00020  * @package       cake
00021  * @subpackage    cake.cake.libs
00022  * @since         CakePHP(tm) v 0.2.9
00023  * @version       $Revision: 8273 $
00024  * @modifiedby    $LastChangedBy: jperras $
00025  * @lastmodified  $Date: 2009-08-02 15:12:02 -0400 (Sun, 02 Aug 2009) $
00026  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00027  */
00028 /**
00029  * Object class, allowing __construct and __destruct in PHP4.
00030  *
00031  * Also includes methods for logging and the special method RequestAction,
00032  * to call other Controllers' Actions from anywhere.
00033  *
00034  * @package       cake
00035  * @subpackage    cake.cake.libs
00036  */
00037 class Object {
00038 /**
00039  * Log object
00040  *
00041  * @var CakeLog
00042  * @access protected
00043  */
00044     var $_log = null;
00045 /**
00046  * A hack to support __construct() on PHP 4
00047  * Hint: descendant classes have no PHP4 class_name() constructors,
00048  * so this constructor gets called first and calls the top-layer __construct()
00049  * which (if present) should call parent::__construct()
00050  *
00051  * @return Object
00052  */
00053     function Object() {
00054         $args = func_get_args();
00055         if (method_exists($this, '__destruct')) {
00056             register_shutdown_function (array(&$this, '__destruct'));
00057         }
00058         call_user_func_array(array(&$this, '__construct'), $args);
00059     }
00060 /**
00061  * Class constructor, overridden in descendant classes.
00062  */
00063     function __construct() {
00064     }
00065 
00066 /**
00067  * Object-to-string conversion.
00068  * Each class can override this method as necessary.
00069  *
00070  * @return string The name of this class
00071  * @access public
00072  */
00073     function toString() {
00074         $class = get_class($this);
00075         return $class;
00076     }
00077 /**
00078  * Calls a controller's method from any location.
00079  *
00080  * @param mixed $url String or array-based url.
00081  * @param array $extra if array includes the key "return" it sets the AutoRender to true.
00082  * @return mixed Boolean true or false on success/failure, or contents
00083  *               of rendered action if 'return' is set in $extra.
00084  * @access public
00085  */
00086     function requestAction($url, $extra = array()) {
00087         if (empty($url)) {
00088             return false;
00089         }
00090         if (!class_exists('dispatcher')) {
00091             require CAKE . 'dispatcher.php';
00092         }
00093         if (in_array('return', $extra, true)) {
00094             $extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
00095         }
00096         if (is_array($url) && !isset($extra['url'])) {
00097             $extra['url'] = array();
00098         }
00099         $params = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
00100         $dispatcher = new Dispatcher;
00101         return $dispatcher->dispatch($url, $params);
00102     }
00103 /**
00104  * Calls a method on this object with the given parameters. Provides an OO wrapper
00105  * for call_user_func_array, and improves performance by using straight method calls
00106  * in most cases.
00107  *
00108  * @param string $method  Name of the method to call
00109  * @param array $params  Parameter list to use when calling $method
00110  * @return mixed  Returns the result of the method call
00111  * @access public
00112  */
00113     function dispatchMethod($method, $params = array()) {
00114         switch (count($params)) {
00115             case 0:
00116                 return $this->{$method}();
00117             case 1:
00118                 return $this->{$method}($params[0]);
00119             case 2:
00120                 return $this->{$method}($params[0], $params[1]);
00121             case 3:
00122                 return $this->{$method}($params[0], $params[1], $params[2]);
00123             case 4:
00124                 return $this->{$method}($params[0], $params[1], $params[2], $params[3]);
00125             case 5:
00126                 return $this->{$method}($params[0], $params[1], $params[2], $params[3], $params[4]);
00127             default:
00128                 return call_user_func_array(array(&$this, $method), $params);
00129             break;
00130         }
00131     }
00132 /**
00133  * Stop execution of the current script
00134  *
00135  * @param $status see http://php.net/exit for values
00136  * @return void
00137  * @access public
00138  */
00139     function _stop($status = 0) {
00140         exit($status);
00141     }
00142 /**
00143  * API for logging events.
00144  *
00145  * @param string $msg Log message
00146  * @param integer $type Error type constant. Defined in app/config/core.php.
00147  * @return boolean Success of log write
00148  * @access public
00149  */
00150     function log($msg, $type = LOG_ERROR) {
00151         if (!class_exists('CakeLog')) {
00152             uses('cake_log');
00153         }
00154         if (is_null($this->_log)) {
00155             $this->_log = new CakeLog();
00156         }
00157         if (!is_string($msg)) {
00158             $msg = print_r($msg, true);
00159         }
00160         return $this->_log->write($type, $msg);
00161     }
00162 /**
00163  * Allows setting of multiple properties of the object in a single line of code.
00164  *
00165  * @param array $properties An associative array containing properties and corresponding values.
00166  * @return void
00167  * @access protected
00168  */
00169     function _set($properties = array()) {
00170         if (is_array($properties) && !empty($properties)) {
00171             $vars = get_object_vars($this);
00172             foreach ($properties as $key => $val) {
00173                 if (array_key_exists($key, $vars)) {
00174                     $this->{$key} = $val;
00175                 }
00176             }
00177         }
00178     }
00179 /**
00180  * Used to report user friendly errors.
00181  * If there is a file app/error.php or app/app_error.php this file will be loaded
00182  * error.php is the AppError class it should extend ErrorHandler class.
00183  *
00184  * @param string $method Method to be called in the error class (AppError or ErrorHandler classes)
00185  * @param array $messages Message that is to be displayed by the error class
00186  * @return error message
00187  * @access public
00188  */
00189     function cakeError($method, $messages = array()) {
00190         if (!class_exists('ErrorHandler')) {
00191             App::import('Core', 'Error');
00192 
00193             if (file_exists(APP . 'error.php')) {
00194                 include_once (APP . 'error.php');
00195             } elseif (file_exists(APP . 'app_error.php')) {
00196                 include_once (APP . 'app_error.php');
00197             }
00198         }
00199 
00200         if (class_exists('AppError')) {
00201             $error = new AppError($method, $messages);
00202         } else {
00203             $error = new ErrorHandler($method, $messages);
00204         }
00205         return $error;
00206     }
00207 /**
00208  * Checks for a persistent class file, if found file is opened and true returned
00209  * If file is not found a file is created and false returned
00210  * If used in other locations of the model you should choose a unique name for the persistent file
00211  * There are many uses for this method, see manual for examples
00212  *
00213  * @param string $name name of the class to persist
00214  * @param string $object the object to persist
00215  * @return boolean Success
00216  * @access protected
00217  * @todo add examples to manual
00218  */
00219     function _persist($name, $return = null, &$object, $type = null) {
00220         $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
00221         if ($return === null) {
00222             if (!file_exists($file)) {
00223                 return false;
00224             } else {
00225                 return true;
00226             }
00227         }
00228 
00229         if (!file_exists($file)) {
00230             $this->_savePersistent($name, $object);
00231             return false;
00232         } else {
00233             $this->__openPersistent($name, $type);
00234             return true;
00235         }
00236     }
00237 /**
00238  * You should choose a unique name for the persistent file
00239  *
00240  * There are many uses for this method, see manual for examples
00241  *
00242  * @param string $name name used for object to cache
00243  * @param object $object the object to persist
00244  * @return boolean true on save, throws error if file can not be created
00245  * @access protected
00246  */
00247     function _savePersistent($name, &$object) {
00248         $file = 'persistent' . DS . strtolower($name) . '.php';
00249         $objectArray = array(&$object);
00250         $data = str_replace('\\', '\\\\', serialize($objectArray));
00251         $data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
00252         $duration = '+999 days';
00253         if (Configure::read() >= 1) {
00254             $duration = '+10 seconds';
00255         }
00256         cache($file, $data, $duration);
00257     }
00258 /**
00259  * Open the persistent class file for reading
00260  * Used by Object::_persist()
00261  *
00262  * @param string $name Name of persisted class
00263  * @param string $type Type of persistance (e.g: registry)
00264  * @return void
00265  * @access private
00266  */
00267     function __openPersistent($name, $type = null) {
00268         $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
00269         include($file);
00270 
00271         switch ($type) {
00272             case 'registry':
00273                 $vars = unserialize(${$name});
00274                 foreach ($vars['0'] as $key => $value) {
00275                     if (strpos($key, '_behavior') !== false) {
00276                         App::import('Behavior', Inflector::classify(substr($key, 0, -9)));
00277                     } else {
00278                         App::import('Model', Inflector::classify($key));
00279                     }
00280                     unset ($value);
00281                 }
00282                 unset($vars);
00283                 $vars = unserialize(${$name});
00284                 foreach ($vars['0'] as $key => $value) {
00285                     ClassRegistry::addObject($key, $value);
00286                     unset ($value);
00287                 }
00288                 unset($vars);
00289             break;
00290             default:
00291                 $vars = unserialize(${$name});
00292                 $this->{$name} = $vars['0'];
00293                 unset($vars);
00294             break;
00295         }
00296     }
00297 }
00298 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7