Advertisement
Sverri

Kohana Cache "Permission denied" fix (Windows 7)

Sep 9th, 2012
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class Cache_File extends Kohana_Cache_File {
  4.  
  5.     /**
  6.      * Retrieve a cached value entry by id.
  7.      *
  8.      *     // Retrieve cache entry from file group
  9.      *     $data = Cache::instance('file')->get('foo');
  10.      *
  11.      *     // Retrieve cache entry from file group and return 'bar' if miss
  12.      *     $data = Cache::instance('file')->get('foo', 'bar');
  13.      *
  14.      * @param   string   id of cache to entry
  15.      * @param   string   default value to return if cache miss
  16.      * @return  mixed
  17.      * @throws  Cache_Exception
  18.      */
  19.     public function get($id, $default = NULL)
  20.     {
  21.         $filename = Cache_File::filename($this->_sanitize_id($id));
  22.         $directory = $this->_resolve_directory($filename);
  23.  
  24.         // Wrap operations in try/catch to handle notices
  25.         try
  26.         {
  27.             // Open file
  28.             $file = new SplFileInfo($directory.$filename);
  29.  
  30.             // If file does not exist
  31.             if ( ! $file->isFile())
  32.             {
  33.                 // Return default value
  34.                 return $default;
  35.             }
  36.             else
  37.             {
  38.                 // Open the file and parse data
  39.                 $created  = $file->getMTime();
  40.                 $data     = $file->openFile();
  41.                 $lifetime = $data->fgets();
  42.  
  43.                 // If we're at the EOF at this point, corrupted!
  44.                 if ($data->eof())
  45.                 {
  46.                     throw new Cache_Exception(__METHOD__.' corrupted cache file!');
  47.                 }
  48.  
  49.                 $cache = '';
  50.  
  51.                 while ($data->eof() === FALSE)
  52.                 {
  53.                     $cache .= $data->fgets();
  54.                 }
  55.  
  56.                 // Test the expiry
  57.                 if (($created + (int) $lifetime) < time())
  58.                 {
  59.                     if (Kohana::$is_windows === TRUE)
  60.                     {
  61.                         // $data is a SplFileObject with a lock on the file
  62.                         // Unsetting it appears to release the lock, allowing
  63.                         // the file to be deleted
  64.                         unset($data);
  65.                     }
  66.                     // Delete the file
  67.                     $this->_delete_file($file, NULL, TRUE);
  68.                     return $default;
  69.                 }
  70.                 else
  71.                 {
  72.                     return unserialize($cache);
  73.                 }
  74.             }
  75.  
  76.         }
  77.         catch (ErrorException $e)
  78.         {
  79.             // Handle ErrorException caused by failed unserialization
  80.             if ($e->getCode() === E_NOTICE)
  81.             {
  82.                 throw new Cache_Exception(__METHOD__.' failed to unserialize cached object with message : '.$e->getMessage());
  83.             }
  84.  
  85.             // Otherwise throw the exception
  86.             throw $e;
  87.         }
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement