2007-04-30 06:01:54 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* File Storage engine for cache
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-04-30 06:01:54 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-04-30 06:01:54 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.cache
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4933
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2007-05-13 21:54:48 +00:00
|
|
|
/**
|
|
|
|
* Included libraries.
|
|
|
|
*
|
|
|
|
*/
|
Closes #2119 Only define clone() in PHP4 when it hasn't been already defined.
Closes #2213, Support multiple plugin paths.
Closes #2234, filepaths to behavior classes should be cached in class.paths.php also
Closes #2345, ability to group components into subfolders
Closes #2645, Improvement to basic.php for class loading.
Fixes #3526, Cache::write, when using just the config name, it fails.
Fixes #3559, loading plugin model as assoc don't work.
Closes #3567 Controller Folders (Note this does not need routing to work, but controller names can not conflict with others in the same application so naming must still be unique)
Fixes #3579, email.php component: Parse error with php 4.
Adding new class and file importer.
Updated most of the core to use the importer.
Added ClassRegsitry::init() that will create and instance of an object and store it in the registry.
Deprecated most of the load functions in basics.php
Plugin model loading now forces using the dot notation, to use models within a plugin, all the model associations must be in the PluginName.Model syntax, if this is not used, the plugin will look for the models in the main app/models directory first, if not found then it will search the plugin directories recursively until it finds a model.
var $belongsTo = array('SomeModel'); will look for some_model.php in the app/models
var $belongsTo = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $belongsTo = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
The controllers of the plugin will still look for the default models inside the plugin if var $uses is not set:
var $uses = array('SomeModel'); will look for some_model.php in the app/models
var $uses = array('MyPlugin.SomeModel'); will look for some_model.php in my_plugin/models
var $uses = array('MyPlugin.MyPlugin', 'SomeModel'); will used my_plugin/models/my_plugin.php and app/models/some_model.php
All of the above will work between plugins and main app
These changes also allow placing model and controllers is sub directories
Removed old class.paths.php file generation
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6001 3807eeeb-6ff5-0310-8944-8be069107fe0
2007-11-16 09:35:19 +00:00
|
|
|
if (!class_exists('File')) {
|
|
|
|
uses ('File');
|
2007-08-28 23:34:14 +00:00
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
|
|
|
* File Storage engine for cache
|
|
|
|
*
|
|
|
|
* @todo use the File and Folder classes (if it's not a too big performance hit)
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.cache
|
|
|
|
*/
|
|
|
|
class FileEngine extends CacheEngine {
|
|
|
|
/**
|
2007-10-22 06:23:57 +00:00
|
|
|
* Instance of File class
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-10-22 06:23:57 +00:00
|
|
|
* @var object
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access private
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
var $__File = null;
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* settings
|
|
|
|
* path = absolute path to cache directory, default => CACHE
|
|
|
|
* prefix = string prefix for filename, default => cake_
|
|
|
|
* lock = enable file locking on write, default => false
|
|
|
|
* serialize = serialize the data, default => true
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @var array
|
2007-10-22 06:23:57 +00:00
|
|
|
* @see CacheEngine::__defaults
|
2007-09-30 07:45:34 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
var $settings = array();
|
2007-11-28 09:20:48 +00:00
|
|
|
/**
|
|
|
|
* Set to true if FileEngine::init(); and FileEngine::__active(); do not fail.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__active = false;
|
|
|
|
/**
|
|
|
|
* True unless FileEngine::__active(); fails
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $__init = true;
|
2007-04-30 06:01:54 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Initialize the Cache Engine
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* Called automatically by the cache frontend
|
2007-09-30 07:45:34 +00:00
|
|
|
* To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param array $setting array of setting for the engine
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the engine has been successfully initialized, false if not
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function init($settings = array()) {
|
|
|
|
parent::init($settings);
|
|
|
|
$defaults = array('path' => CACHE, 'prefix'=> 'cake_', 'lock'=> false, 'serialize'=> true);
|
2007-12-08 06:08:03 +00:00
|
|
|
$this->settings = array_merge($defaults, $this->settings, $settings);
|
2007-10-17 07:34:16 +00:00
|
|
|
if(!isset($this->__File)) {
|
|
|
|
$this->__File =& new File($this->settings['path'] . DS . 'cake');
|
2007-10-14 01:09:21 +00:00
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
$this->settings['path'] = $this->__File->Folder->cd($this->settings['path']);
|
2007-10-27 20:00:29 +00:00
|
|
|
if(empty($this->settings['path'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-28 09:20:48 +00:00
|
|
|
return $this->__active();
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-10-22 06:23:57 +00:00
|
|
|
* Garbage collection. Permanently remove all expired and deleted data
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if garbage collection was succesful, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function gc() {
|
|
|
|
return $this->clear(true);
|
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Write data for key into cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param mixed $data Data to be cached
|
2007-04-30 06:01:54 +00:00
|
|
|
* @param mixed $duration How long to cache the data, in seconds
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the data was succesfully cached, false on failure
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function write($key, &$data, $duration) {
|
2007-11-28 09:20:48 +00:00
|
|
|
if (empty($data) || !$this->__init) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
|
|
|
|
if($this->__setKey($key) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($duration == null) {
|
|
|
|
$duration = $this->settings['duration'];
|
2007-06-23 04:48:29 +00:00
|
|
|
}
|
2007-12-23 16:40:56 +00:00
|
|
|
$windows = false;
|
|
|
|
$lineBreak = "\n";
|
2007-12-14 07:18:59 +00:00
|
|
|
|
2007-12-23 16:40:56 +00:00
|
|
|
if (substr(PHP_OS, 0, 3) == "WIN") {
|
|
|
|
$lineBreak = "\r\n";
|
|
|
|
$windows = true;
|
|
|
|
}
|
2007-10-14 01:09:21 +00:00
|
|
|
if (!empty($this->settings['serialize'])) {
|
2007-12-23 16:40:56 +00:00
|
|
|
if ($windows) {
|
2007-12-14 07:18:59 +00:00
|
|
|
$data = str_replace('\\', '\\\\\\\\', serialize($data));
|
|
|
|
} else {
|
|
|
|
$data = serialize($data);
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
}
|
2007-10-14 01:09:21 +00:00
|
|
|
|
2007-10-17 07:34:16 +00:00
|
|
|
if ($this->settings['lock']) {
|
|
|
|
$this->__File->lock = true;
|
2007-06-23 04:48:29 +00:00
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$expires = time() + $duration;
|
2007-12-23 16:40:56 +00:00
|
|
|
$contents = $expires . $lineBreak . $data . $lineBreak;
|
2007-10-17 07:34:16 +00:00
|
|
|
$success = $this->__File->write($contents);
|
|
|
|
$this->__File->close();
|
|
|
|
return $success;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Read a key from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
|
|
|
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function read($key) {
|
2007-11-28 09:20:48 +00:00
|
|
|
if($this->__setKey($key) === false || !$this->__init) {
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
if ($this->settings['lock']) {
|
|
|
|
$this->__File->lock = true;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
$cachetime = $this->__File->read(11);
|
|
|
|
|
|
|
|
if ($cachetime !== false && intval($cachetime) < time()) {
|
|
|
|
$this->__File->close();
|
|
|
|
$this->__File->delete();
|
2007-04-30 06:01:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
$data = $this->__File->read(true);
|
2007-12-14 00:25:03 +00:00
|
|
|
|
2007-10-17 07:34:16 +00:00
|
|
|
if (!empty($data) && !empty($this->settings['serialize'])) {
|
2007-12-14 00:25:03 +00:00
|
|
|
$data = stripslashes($data);
|
|
|
|
$data = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $data);
|
|
|
|
$data = unserialize($data);
|
|
|
|
|
|
|
|
if (is_array($data)) {
|
|
|
|
$data = array_map('stripslashes_deep', $data);
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
$this->__File->close();
|
2007-09-30 07:45:34 +00:00
|
|
|
return $data;
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Delete a key from the cache
|
2007-04-30 06:01:54 +00:00
|
|
|
*
|
|
|
|
* @param string $key Identifier for the data
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
|
|
|
function delete($key) {
|
2007-11-28 09:20:48 +00:00
|
|
|
if($this->__setKey($key) === false || !$this->__init) {
|
2007-06-23 04:48:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
return $this->__File->delete();
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Delete all values from the cache
|
|
|
|
*
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $check Optional - only delete expired cache items
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the cache was succesfully cleared, false otherwise
|
2007-05-20 05:35:13 +00:00
|
|
|
* @access public
|
2007-04-30 06:01:54 +00:00
|
|
|
*/
|
2007-09-30 07:45:34 +00:00
|
|
|
function clear($check) {
|
2007-11-28 09:20:48 +00:00
|
|
|
if (!$this->__init) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
$dir = dir($this->settings['path']);
|
|
|
|
if ($check) {
|
2007-04-30 06:01:54 +00:00
|
|
|
$now = time();
|
2007-10-17 07:34:16 +00:00
|
|
|
$threshold = $now - $this->settings['duration'];
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
2007-06-20 06:15:35 +00:00
|
|
|
while (($entry = $dir->read()) !== false) {
|
2008-01-05 03:24:25 +00:00
|
|
|
if($this->__setKey($entry) === false) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-09-30 07:45:34 +00:00
|
|
|
if ($check) {
|
2007-10-17 07:34:16 +00:00
|
|
|
$mtime = $this->__File->lastChange();
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($mtime === false || $mtime > $threshold) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
|
|
|
|
$expires = $this->__File->read(11);
|
|
|
|
$this->__File->close();
|
2007-04-30 06:01:54 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
if ($expires > $now) {
|
2007-04-30 06:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2007-10-17 07:34:16 +00:00
|
|
|
$this->__File->delete();
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
/**
|
2007-09-30 07:45:34 +00:00
|
|
|
* Get absolute file for a given key
|
2007-04-30 07:38:47 +00:00
|
|
|
*
|
2007-09-30 07:45:34 +00:00
|
|
|
* @param string $key The key
|
|
|
|
* @return mixed Absolute cache file for the given key or false if erroneous
|
|
|
|
* @access private
|
|
|
|
*/
|
2007-10-17 07:34:16 +00:00
|
|
|
function __setKey($key) {
|
|
|
|
$this->__File->Folder->cd($this->settings['path']);
|
2008-01-05 03:24:25 +00:00
|
|
|
$this->__File->name = $key;
|
2007-10-17 07:34:16 +00:00
|
|
|
if (!$this->__File->Folder->inPath($this->__File->pwd(), true)) {
|
2007-09-30 07:45:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-04-30 07:38:47 +00:00
|
|
|
}
|
2007-11-28 09:20:48 +00:00
|
|
|
/**
|
|
|
|
* Determine is cache directory is writable
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function __active() {
|
|
|
|
if (!$this->__active && $this->__init && !is_writable($this->settings['path'])) {
|
|
|
|
$this->__init = false;
|
|
|
|
trigger_error(sprintf(__('%s is not writable', true), $this->settings['path']), E_USER_WARNING);
|
|
|
|
} else {
|
|
|
|
$this->__active = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 06:01:54 +00:00
|
|
|
}
|
|
|
|
?>
|