object.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: object_8php-source.html 675 2008-12-26 00:27:14Z 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: 675 $
00026  * @modifiedby      $LastChangedBy: gwoo $
00027  * @lastmodified    $Date: 2008-12-25 18:27:14 -0600 (Thu, 25 Dec 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  * @access public
00055  */
00056     function Object() {
00057         $args = func_get_args();
00058         if (method_exists($this, '__destruct')) {
00059             register_shutdown_function (array(&$this, '__destruct'));
00060         }
00061         call_user_func_array(array(&$this, '__construct'), $args);
00062     }
00063 /**
00064  * Class constructor, overridden in descendant classes.
00065  *
00066  * @abstract
00067  * @access public
00068  */
00069     function __construct() {
00070     }
00071 
00072 /**
00073  * Object-to-string conversion.
00074  * Each class can override this method as necessary.
00075  *
00076  * @return string The name of this class
00077  * @access public
00078  */
00079     function toString() {
00080         $class = get_class($this);
00081         return $class;
00082     }
00083 /**
00084  * Calls a controller's method from any location. Allows for
00085  * controllers to communicate with each other.
00086  *
00087  * @param string $url  URL in the form of Cake URL ("/controller/method/parameter")
00088  * @param array $extra If array includes the key "return" it sets the AutoRender to true.
00089  * @return boolean  Success
00090  * @access public
00091  */
00092     function requestAction($url, $extra = array()) {
00093         if (!empty($url)) {
00094             $dispatcher =& new Dispatcher();
00095             if (isset($this->plugin)) {
00096                 $extra['plugin'] = $this->plugin;
00097             }
00098             if (in_array('return', $extra, true)) {
00099                 $extra['return'] = 0;
00100                 $extra['bare'] = 1;
00101                 $extra['requested'] = 1;
00102                 ob_start();
00103                 $out = $dispatcher->dispatch($url, $extra);
00104                 $out = ob_get_clean();
00105                 return $out;
00106             } else {
00107                 $extra['return'] = 1;
00108                 $extra['bare'] = 1;
00109                 $extra['requested'] = 1;
00110                 return $dispatcher->dispatch($url, $extra);
00111             }
00112         } else {
00113             return false;
00114         }
00115     }
00116 /**
00117  * API for logging events.
00118  *
00119  * @param string $msg Log message
00120  * @param int $type Error type constant. Defined in app/config/core.php.
00121  * @access
00122  */
00123     function log($msg, $type = LOG_ERROR) {
00124         if (!class_exists('CakeLog')) {
00125             uses('cake_log');
00126         }
00127 
00128         if (is_null($this->_log)) {
00129             $this->_log = new CakeLog();
00130         }
00131 
00132         if (!is_string($msg)) {
00133             ob_start();
00134             print_r ($msg);
00135             $msg=ob_get_contents();
00136             ob_end_clean();
00137         }
00138 
00139         switch($type) {
00140             case LOG_DEBUG:
00141                 return $this->_log->write('debug', $msg);
00142             break;
00143             default:
00144                 return $this->_log->write('error', $msg);
00145             break;
00146         }
00147     }
00148 /**
00149  * Used to report user friendly errors.
00150  * If there is a file app/error.php this file will be loaded
00151  * error.php is the AppError class it should extend ErrorHandler class.
00152  *
00153  * @param string $method Method to be called in the error class (AppError or ErrorHandler classes)
00154  * @param array $messages Message that is to be displayed by the error class
00155  * @return error message
00156  * @access public
00157  */
00158     function cakeError($method, $messages) {
00159         if (!class_exists('ErrorHandler')) {
00160             uses('error');
00161             if (file_exists(APP . 'error.php')) {
00162                 include_once (APP . 'error.php');
00163             }
00164         }
00165 
00166         if (class_exists('AppError')) {
00167             $error = new AppError($method, $messages);
00168         } else {
00169             $error = new ErrorHandler($method, $messages);
00170         }
00171         return $error;
00172     }
00173 /**
00174  * Checks for a persistent class file, if found file is opened and true returned
00175  * If file is not found a file is created and false returned
00176  *
00177  * There are many uses for this method, see manual for examples also art of
00178  * the cache system
00179  *
00180  * @param string $name name of class to persist
00181  * @param boolean $return
00182  * @param object $object
00183  * @param string $type
00184  * @return boolean
00185  * @todo add examples to manual
00186  * @access protected
00187  */
00188     function _persist($name, $return = null, &$object, $type = null) {
00189         $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
00190         if ($return === null) {
00191             if (!file_exists($file)) {
00192                 return false;
00193             } else {
00194                 return true;
00195             }
00196         }
00197 
00198         if (!file_exists($file)) {
00199             $this->_savePersistent($name, $object);
00200             return false;
00201         } else {
00202             $this->__openPersistent($name, $type);
00203             return true;
00204         }
00205     }
00206 /**
00207  * You should choose a unique name for the persistent file
00208  *
00209  * There are many uses for this method, see manual for examples also part of
00210  * the cache system
00211  *
00212  * @param string $name name used for object to cache
00213  * @param object $object the object to persist
00214  * @return true on save, throws error if file can not be created
00215  * @access protected
00216  */
00217     function _savePersistent($name, &$object) {
00218         $file = 'persistent' . DS . strtolower($name) . '.php';
00219         $objectArray = array(&$object);
00220         $data = str_replace('\\', '\\\\', serialize($objectArray));
00221         $data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
00222         cache($file, $data, '+1 day');
00223     }
00224 /**
00225  * Open the persistent class file for reading
00226  * Used by Object::_persist(), part of the cache
00227  * system
00228  *
00229  * @param string $name Name of the persistant file
00230  * @param string $type
00231  * @access private
00232  */
00233     function __openPersistent($name, $type = null) {
00234         $file = CACHE . 'persistent' . DS . strtolower($name) . '.php';
00235         include($file);
00236 
00237         switch($type) {
00238             case 'registry':
00239                 $vars = unserialize(${$name});
00240                 foreach ($vars['0'] as $key => $value) {
00241                     loadModel(Inflector::classify($key));
00242                 }
00243                 unset($vars);
00244                 $vars = unserialize(${$name});
00245                 foreach ($vars['0'] as $key => $value) {
00246                     ClassRegistry::addObject($key, $value);
00247                     unset ($value);
00248                 }
00249                 unset($vars);
00250             break;
00251             default:
00252                 $vars = unserialize(${$name});
00253                 $this->{$name} = $vars['0'];
00254                 unset($vars);
00255             break;
00256         }
00257     }
00258 }
00259 ?>