object.php

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