Skip to content

Instantly share code, notes, and snippets.

@fukata
Created March 19, 2011 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fukata/877879 to your computer and use it in GitHub Desktop.
Save fukata/877879 to your computer and use it in GitHub Desktop.
"CodeIgniter2" on "Cache Driver" feature "CodeIgniter1" backported patches. Override system directories.
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/Cache.php ./system/libraries/Cache/Cache.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/Cache.php 1970-01-01 08:00:00.000000000 +0800
+++ ./system/libraries/Cache/Cache.php 2011-03-20 06:22:57.267999183 +0800
@@ -0,0 +1,216 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 2.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Caching Class
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Core
+ * @author ExpressionEngine Dev Team
+ * @link
+ */
+class Cache extends CI_Driver_Library {
+
+ protected $valid_drivers = array(
+ 'cache_apc', 'cache_file', 'cache_memcached', 'cache_dummy'
+ );
+
+ protected $_cache_path = NULL; // Path of cache files (if file-based cache)
+ protected $_adapter = 'dummy';
+ protected $_backup_driver;
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Constructor
+ *
+ * @param array
+ */
+ public function __construct($config = array())
+ {
+ if ( ! empty($config))
+ {
+ $this->_initialize($config);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get
+ *
+ * Look for a value in the cache. If it exists, return the data
+ * if not, return FALSE
+ *
+ * @param string
+ * @return mixed value that is stored/FALSE on failure
+ */
+ public function get($id)
+ {
+ return $this->{$this->_adapter}->get($id);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Save
+ *
+ * @param string Unique Key
+ * @param mixed Data to store
+ * @param int Length of time (in seconds) to cache the data
+ *
+ * @return boolean true on success/false on failure
+ */
+ public function save($id, $data, $ttl = 60)
+ {
+ return $this->{$this->_adapter}->save($id, $data, $ttl);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete from Cache
+ *
+ * @param mixed unique identifier of the item in the cache
+ * @return boolean true on success/false on failure
+ */
+ public function delete($id)
+ {
+ return $this->{$this->_adapter}->delete($id);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Clean the cache
+ *
+ * @return boolean false on failure/true on success
+ */
+ public function clean()
+ {
+ return $this->{$this->_adapter}->clean();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Info
+ *
+ * @param string user/filehits
+ * @return mixed array on success, false on failure
+ */
+ public function cache_info($type = 'user')
+ {
+ return $this->{$this->_adapter}->cache_info($type);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get Cache Metadata
+ *
+ * @param mixed key to get cache metadata on
+ * @return mixed return value from child method
+ */
+ public function get_metadata($id)
+ {
+ return $this->{$this->_adapter}->get_metadata($id);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Initialize
+ *
+ * Initialize class properties based on the configuration array.
+ *
+ * @param array
+ * @return void
+ */
+ private function _initialize($config)
+ {
+ $default_config = array(
+ 'adapter',
+ 'memcached'
+ );
+
+ foreach ($default_config as $key)
+ {
+ if (isset($config[$key]))
+ {
+ $param = '_'.$key;
+
+ $this->{$param} = $config[$key];
+ }
+ }
+
+ if (isset($config['backup']))
+ {
+ if (in_array('cache_'.$config['backup'], $this->valid_drivers))
+ {
+ $this->_backup_driver = $config['backup'];
+ }
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Is the requested driver supported in this environment?
+ *
+ * @param string The driver to test.
+ * @return array
+ */
+ public function is_supported($driver)
+ {
+ static $support = array();
+
+ if ( ! isset($support[$driver]))
+ {
+ $support[$driver] = $this->{$driver}->is_supported();
+ }
+
+ return $support[$driver];
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * __get()
+ *
+ * @param child
+ * @return object
+ */
+ public function __get($child)
+ {
+ $obj = parent::__get($child);
+
+ if ( ! $this->is_supported($child))
+ {
+ $this->_adapter = $this->_backup_driver;
+ }
+
+ return $obj;
+ }
+
+ // ------------------------------------------------------------------------
+}
+// End Class
+
+/* End of file Cache.php */
+/* Location: ./system/libraries/Cache/Cache.php */
\ ファイル末尾に改行がありません
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_apc.php ./system/libraries/Cache/drivers/Cache_apc.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_apc.php 1970-01-01 08:00:00.000000000 +0800
+++ ./system/libraries/Cache/drivers/Cache_apc.php 2011-03-20 06:22:57.267999183 +0800
@@ -0,0 +1,151 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.1.6 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 2.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter APC Caching Class
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Core
+ * @author ExpressionEngine Dev Team
+ * @link
+ */
+
+class Cache_apc extends CI_Driver {
+
+ /**
+ * Get
+ *
+ * Look for a value in the cache. If it exists, return the data
+ * if not, return FALSE
+ *
+ * @param string
+ * @return mixed value that is stored/FALSE on failure
+ */
+ public function get($id)
+ {
+ $data = apc_fetch($id);
+
+ return (is_array($data)) ? $data[0] : FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Save
+ *
+ * @param string Unique Key
+ * @param mixed Data to store
+ * @param int Length of time (in seconds) to cache the data
+ *
+ * @return boolean true on success/false on failure
+ */
+ public function save($id, $data, $ttl = 60)
+ {
+ return apc_store($id, array($data, time(), $ttl), $ttl);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete from Cache
+ *
+ * @param mixed unique identifier of the item in the cache
+ * @param boolean true on success/false on failure
+ */
+ public function delete($id)
+ {
+ return apc_delete($id);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Clean the cache
+ *
+ * @return boolean false on failure/true on success
+ */
+ public function clean()
+ {
+ return apc_clear_cache('user');
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Info
+ *
+ * @param string user/filehits
+ * @return mixed array on success, false on failure
+ */
+ public function cache_info($type = NULL)
+ {
+ return apc_cache_info($type);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get Cache Metadata
+ *
+ * @param mixed key to get cache metadata on
+ * @return mixed array on success/false on failure
+ */
+ public function get_metadata($id)
+ {
+ $stored = apc_fetch($id);
+
+ if (count($stored) !== 3)
+ {
+ return FALSE;
+ }
+
+ list($data, $time, $ttl) = $stored;
+
+ return array(
+ 'expire' => $time + $ttl,
+ 'mtime' => $time,
+ 'data' => $data
+ );
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * is_supported()
+ *
+ * Check to see if APC is available on this system, bail if it isn't.
+ */
+ public function is_supported()
+ {
+ if ( ! extension_loaded('apc') OR ! function_exists('apc_store'))
+ {
+ log_message('error', 'The APC PHP extension must be loaded to use APC Cache.');
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+
+}
+// End Class
+
+/* End of file Cache_apc.php */
+/* Location: ./system/libraries/Cache/drivers/Cache_apc.php */
\ ファイル末尾に改行がありません
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_dummy.php ./system/libraries/Cache/drivers/Cache_dummy.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_dummy.php 1970-01-01 08:00:00.000000000 +0800
+++ ./system/libraries/Cache/drivers/Cache_dummy.php 2011-03-20 06:22:57.267999183 +0800
@@ -0,0 +1,129 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 2.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Dummy Caching Class
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Core
+ * @author ExpressionEngine Dev Team
+ * @link
+ */
+
+class Cache_dummy extends CI_Driver {
+
+ /**
+ * Get
+ *
+ * Since this is the dummy class, it's always going to return FALSE.
+ *
+ * @param string
+ * @return Boolean FALSE
+ */
+ public function get($id)
+ {
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Save
+ *
+ * @param string Unique Key
+ * @param mixed Data to store
+ * @param int Length of time (in seconds) to cache the data
+ *
+ * @return boolean TRUE, Simulating success
+ */
+ public function save($id, $data, $ttl = 60)
+ {
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete from Cache
+ *
+ * @param mixed unique identifier of the item in the cache
+ * @param boolean TRUE, simulating success
+ */
+ public function delete($id)
+ {
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Clean the cache
+ *
+ * @return boolean TRUE, simulating success
+ */
+ public function clean()
+ {
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Info
+ *
+ * @param string user/filehits
+ * @return boolean FALSE
+ */
+ public function cache_info($type = NULL)
+ {
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get Cache Metadata
+ *
+ * @param mixed key to get cache metadata on
+ * @return boolean FALSE
+ */
+ public function get_metadata($id)
+ {
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Is this caching driver supported on the system?
+ * Of course this one is.
+ *
+ * @return TRUE;
+ */
+ public function is_supported()
+ {
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+}
+// End Class
+
+/* End of file Cache_apc.php */
+/* Location: ./system/libraries/Cache/drivers/Cache_apc.php */
\ ファイル末尾に改行がありません
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_file.php ./system/libraries/Cache/drivers/Cache_file.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_file.php 1970-01-01 08:00:00.000000000 +0800
+++ ./system/libraries/Cache/drivers/Cache_file.php 2011-03-20 06:22:57.267999183 +0800
@@ -0,0 +1,196 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 2.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Memcached Caching Class
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Core
+ * @author ExpressionEngine Dev Team
+ * @link
+ */
+
+class Cache_file extends CI_Driver {
+
+ protected $_cache_path;
+
+ /**
+ * Constructor
+ */
+ public function __construct()
+ {
+ $CI =& get_instance();
+ $CI->load->helper('file');
+
+ $path = $CI->config->item('cache_path');
+
+ $this->_cache_path = ($path == '') ? APPPATH.'cache/' : $path;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch from cache
+ *
+ * @param mixed unique key id
+ * @return mixed data on success/false on failure
+ */
+ public function get($id)
+ {
+ if ( ! file_exists($this->_cache_path.$id))
+ {
+ return FALSE;
+ }
+
+ $data = read_file($this->_cache_path.$id);
+ $data = unserialize($data);
+
+ if (time() > $data['time'] + $data['ttl'])
+ {
+ unlink($this->_cache_path.$id);
+ return FALSE;
+ }
+
+ return $data['data'];
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Save into cache
+ *
+ * @param string unique key
+ * @param mixed data to store
+ * @param int length of time (in seconds) the cache is valid
+ * - Default is 60 seconds
+ * @return boolean true on success/false on failure
+ */
+ public function save($id, $data, $ttl = 60)
+ {
+ $contents = array(
+ 'time' => time(),
+ 'ttl' => $ttl,
+ 'data' => $data
+ );
+
+ if (write_file($this->_cache_path.$id, serialize($contents)))
+ {
+ @chmod($this->_cache_path.$id, 0777);
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete from Cache
+ *
+ * @param mixed unique identifier of item in cache
+ * @return boolean true on success/false on failure
+ */
+ public function delete($id)
+ {
+ return unlink($this->_cache_path.$id);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Clean the Cache
+ *
+ * @return boolean false on failure/true on success
+ */
+ public function clean()
+ {
+ return delete_files($this->_cache_path);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Info
+ *
+ * Not supported by file-based caching
+ *
+ * @param string user/filehits
+ * @return mixed FALSE
+ */
+ public function cache_info($type = NULL)
+ {
+ return get_dir_file_info($this->_cache_path);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get Cache Metadata
+ *
+ * @param mixed key to get cache metadata on
+ * @return mixed FALSE on failure, array on success.
+ */
+ public function get_metadata($id)
+ {
+ if ( ! file_exists($this->_cache_path.$id))
+ {
+ return FALSE;
+ }
+
+ $data = read_file($this->_cache_path.$id);
+ $data = unserialize($data);
+
+ if (is_array($data))
+ {
+ $data = $data['data'];
+ $mtime = filemtime($this->_cache_path.$id);
+
+ if ( ! isset($data['ttl']))
+ {
+ return FALSE;
+ }
+
+ return array(
+ 'expire' => $mtime + $data['ttl'],
+ 'mtime' => $mtime
+ );
+ }
+
+ return FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Is supported
+ *
+ * In the file driver, check to see that the cache directory is indeed writable
+ *
+ * @return boolean
+ */
+ public function is_supported()
+ {
+ return is_really_writable($this->_cache_path);
+ }
+
+ // ------------------------------------------------------------------------
+}
+// End Class
+
+/* End of file Cache_file.php */
+/* Location: ./system/libraries/Cache/drivers/Cache_file.php */
\ ファイル末尾に改行がありません
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_memcached.php ./system/libraries/Cache/drivers/Cache_memcached.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Cache/drivers/Cache_memcached.php 1970-01-01 08:00:00.000000000 +0800
+++ ./system/libraries/Cache/drivers/Cache_memcached.php 2011-03-20 06:22:57.267999183 +0800
@@ -0,0 +1,209 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 2.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Memcached Caching Class
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Core
+ * @author ExpressionEngine Dev Team
+ * @link
+ */
+
+class Cache_memcached extends CI_Driver {
+
+ private $_memcached; // Holds the memcached object
+
+ protected $_memcache_conf = array(
+ 'default' => array(
+ 'default_host' => '127.0.0.1',
+ 'default_port' => 11211,
+ 'default_weight' => 1
+ )
+ );
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Fetch from cache
+ *
+ * @param mixed unique key id
+ * @return mixed data on success/false on failure
+ */
+ public function get($id)
+ {
+ $data = $this->_memcached->get($id);
+
+ return (is_array($data)) ? $data[0] : FALSE;
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Save
+ *
+ * @param string unique identifier
+ * @param mixed data being cached
+ * @param int time to live
+ * @return boolean true on success, false on failure
+ */
+ public function save($id, $data, $ttl = 60)
+ {
+ return $this->_memcached->add($id, array($data, time(), $ttl), $ttl);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Delete from Cache
+ *
+ * @param mixed key to be deleted.
+ * @return boolean true on success, false on failure
+ */
+ public function delete($id)
+ {
+ return $this->_memcached->delete($id);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Clean the Cache
+ *
+ * @return boolean false on failure/true on success
+ */
+ public function clean()
+ {
+ return $this->_memcached->flush();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Cache Info
+ *
+ * @param null type not supported in memcached
+ * @return mixed array on success, false on failure
+ */
+ public function cache_info($type = NULL)
+ {
+ return $this->_memcached->getStats();
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Get Cache Metadata
+ *
+ * @param mixed key to get cache metadata on
+ * @return mixed FALSE on failure, array on success.
+ */
+ public function get_metadata($id)
+ {
+ $stored = $this->_memcached->get($id);
+
+ if (count($stored) !== 3)
+ {
+ return FALSE;
+ }
+
+ list($data, $time, $ttl) = $stored;
+
+ return array(
+ 'expire' => $time + $ttl,
+ 'mtime' => $time,
+ 'data' => $data
+ );
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Setup memcached.
+ */
+ private function _setup_memcached()
+ {
+ // Try to load memcached server info from the config file.
+ $CI =& get_instance();
+ if ($CI->config->load('memcached', TRUE, TRUE))
+ {
+ if (is_array($CI->config->config['memcached']))
+ {
+ $this->_memcache_conf = NULL;
+
+ foreach ($CI->config->config['memcached'] as $name => $conf)
+ {
+ $this->_memcache_conf[$name] = $conf;
+ }
+ }
+ }
+
+ $this->_memcached = new Memcached();
+
+ foreach ($this->_memcache_conf as $name => $cache_server)
+ {
+ if ( ! array_key_exists('hostname', $cache_server))
+ {
+ $cache_server['hostname'] = $this->_default_options['default_host'];
+ }
+
+ if ( ! array_key_exists('port', $cache_server))
+ {
+ $cache_server['port'] = $this->_default_options['default_port'];
+ }
+
+ if ( ! array_key_exists('weight', $cache_server))
+ {
+ $cache_server['weight'] = $this->_default_options['default_weight'];
+ }
+
+ $this->_memcached->addServer(
+ $cache_server['hostname'], $cache_server['port'], $cache_server['weight']
+ );
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+
+ /**
+ * Is supported
+ *
+ * Returns FALSE if memcached is not supported on the system.
+ * If it is, we setup the memcached object & return TRUE
+ */
+ public function is_supported()
+ {
+ if ( ! extension_loaded('memcached'))
+ {
+ log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
+
+ return FALSE;
+ }
+
+ $this->_setup_memcached();
+ return TRUE;
+ }
+
+ // ------------------------------------------------------------------------
+
+}
+// End Class
+
+/* End of file Cache_memcached.php */
+/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */
\ ファイル末尾に改行がありません
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Driver.php ./system/libraries/Driver.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Driver.php 1970-01-01 08:00:00.000000000 +0800
+++ ./system/libraries/Driver.php 2011-03-20 06:22:57.267999183 +0800
@@ -0,0 +1,229 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 5.1.6 or newer
+ *
+ * @package CodeIgniter
+ * @author EllisLab Dev Team
+ * @copyright Copyright (c) 2006 - 2011, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Driver Library Class
+ *
+ * This class enables you to create "Driver" libraries that add runtime ability
+ * to extend the capabilities of a class via additional driver objects
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Libraries
+ * @author EllisLab Dev Team
+ * @link
+ */
+class CI_Driver_Library {
+
+ protected $valid_drivers = array();
+ protected static $lib_name;
+
+ // The first time a child is used it won't exist, so we instantiate it
+ // subsequents calls will go straight to the proper child.
+ function __get($child)
+ {
+ if ( ! isset($this->lib_name))
+ {
+ $this->lib_name = get_class($this);
+ }
+
+ // The class will be prefixed with the parent lib
+ $child_class = $this->lib_name.'_'.$child;
+
+ if (in_array(strtolower($child_class), array_map('strtolower', $this->valid_drivers)))
+ {
+ // check and see if the driver is in a separate file
+ if ( ! class_exists($child_class))
+ {
+ // check application path first
+ foreach (array(APPPATH, BASEPATH) as $path)
+ {
+ // and check for case sensitivity of both the parent and child libs
+ foreach (array(ucfirst($this->lib_name), strtolower($this->lib_name)) as $lib)
+ {
+ // loves me some nesting!
+ foreach (array(ucfirst($child_class), strtolower($child_class)) as $class)
+ {
+ $filepath = $path.'libraries/'.$this->lib_name.'/drivers/'.$child_class.EXT;
+
+ if (file_exists($filepath))
+ {
+ include_once $filepath;
+ break;
+ }
+ }
+ }
+ }
+
+ // it's a valid driver, but the file simply can't be found
+ if ( ! class_exists($child_class))
+ {
+ log_message('error', "Unable to load the requested driver: ".$child_class);
+ show_error("Unable to load the requested driver: ".$child_class);
+ }
+ }
+
+ $obj = new $child_class;
+ $obj->decorate($this);
+ $this->$child = $obj;
+ return $this->$child;
+ }
+
+ // The requested driver isn't valid!
+ log_message('error', "Invalid driver requested: ".$child_class);
+ show_error("Invalid driver requested: ".$child_class);
+ }
+
+ // --------------------------------------------------------------------
+
+}
+// END CI_Driver_Library CLASS
+
+
+/**
+ * CodeIgniter Driver Class
+ *
+ * This class enables you to create drivers for a Library based on the Driver Library.
+ * It handles the drivers' access to the parent library
+ *
+ * @package CodeIgniter
+ * @subpackage Libraries
+ * @category Libraries
+ * @author EllisLab Dev Team
+ * @link
+ */
+class CI_Driver {
+ protected $parent;
+
+ private $methods = array();
+ private $properties = array();
+
+ private static $reflections = array();
+
+ /**
+ * Decorate
+ *
+ * Decorates the child with the parent driver lib's methods and properties
+ *
+ * @param object
+ * @return void
+ */
+ public function decorate($parent)
+ {
+ $this->parent = $parent;
+
+ // Lock down attributes to what is defined in the class
+ // and speed up references in magic methods
+
+ $class_name = get_class($parent);
+
+ if ( ! isset(self::$reflections[$class_name]))
+ {
+ $r = new ReflectionObject($parent);
+
+ foreach ($r->getMethods() as $method)
+ {
+ if ($method->isPublic())
+ {
+ $this->methods[] = $method->getName();
+ }
+ }
+
+ foreach ($r->getProperties() as $prop)
+ {
+ if ($prop->isPublic())
+ {
+ $this->properties[] = $prop->getName();
+ }
+ }
+
+ self::$reflections[$class_name] = array($this->methods, $this->properties);
+ }
+ else
+ {
+ list($this->methods, $this->properties) = self::$reflections[$class_name];
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * __call magic method
+ *
+ * Handles access to the parent driver library's methods
+ *
+ * @access public
+ * @param string
+ * @param array
+ * @return mixed
+ */
+ public function __call($method, $args = array())
+ {
+ if (in_array($method, $this->methods))
+ {
+ return call_user_func_array(array($this->parent, $method), $args);
+ }
+
+ $trace = debug_backtrace();
+ _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
+ exit;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * __get magic method
+ *
+ * Handles reading of the parent driver library's properties
+ *
+ * @param string
+ * @return mixed
+ */
+ public function __get($var)
+ {
+ if (in_array($var, $this->properties))
+ {
+ return $this->parent->$var;
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * __set magic method
+ *
+ * Handles writing to the parent driver library's properties
+ *
+ * @param string
+ * @param array
+ * @return mixed
+ */
+ public function __set($var, $val)
+ {
+ if (in_array($var, $this->properties))
+ {
+ $this->parent->$var = $val;
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+}
+// END CI_Driver CLASS
+
+/* End of file Driver.php */
+/* Location: ./system/libraries/Driver.php */
\ ファイル末尾に改行がありません
diff -Nru /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Loader.php ./system/libraries/Loader.php
--- /home/tatsuya/DOWNLOAD/php/CodeIgniter_1.7.3/system/libraries/Loader.php 2010-12-03 01:38:35.000000000 +0800
+++ ./system/libraries/Loader.php 2011-03-20 06:26:16.698059453 +0800
@@ -552,6 +552,36 @@
$CI->config->load($file, $use_sections, $fail_gracefully);
}
+ // --------------------------------------------------------------------
+
+ /**
+ * Driver
+ *
+ * Loads a driver library
+ *
+ * @param string the name of the class
+ * @param mixed the optional parameters
+ * @param string an optional object name
+ * @return void
+ */
+ function driver($library = '', $params = NULL, $object_name = NULL)
+ {
+ if ( ! class_exists('CI_Driver_Library'))
+ {
+ // we aren't instantiating an object here, that'll be done by the Library itself
+ require BASEPATH.'libraries/Driver'.EXT;
+ }
+
+ // We can save the loader some time since Drivers will *always* be in a subfolder,
+ // and typically identically named to the library
+ if ( ! strpos($library, '/'))
+ {
+ $library = ucfirst($library).'/'.$library;
+ }
+
+ return $this->library($library, $params, $object_name);
+ }
+
// --------------------------------------------------------------------
/**
@@ -1082,4 +1112,4 @@
}
/* End of file Loader.php */
-/* Location: ./system/libraries/Loader.php */
\ ファイル末尾に改行がありません
+/* Location: ./system/libraries/Loader.php */
@fukata
Copy link
Author

fukata commented Mar 19, 2011

= How to Patch =
cd [CodeIgniter1 directory]
patch -p0 < cache-driver.patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment