<?php
/**
* Copyright (C) 2017 Karmabunny Pty Ltd.
*
* This file is a part of SproutCMS.
*
* SproutCMS is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation, either
* version 2 of the License, or (at your option) any later version.
*
* For more information, visit <http://getsproutcms.com>.
*
* This class was originally from Kohana 2.3.4
* Copyright 2007-2008 Kohana Team
*/
namespace Sprout\Helpers\Drivers\Cache;
use Kohana;
use Kohana_Exception;
use Sprout\Helpers\Drivers\CacheDriver;
/**
* File-based Cache driver.
*/
class File implements CacheDriver
{
protected $directory = '';
/**
* Tests that the storage location is a directory and is writable.
*/
public function __construct($directory)
{
// Find the real path to the directory
// Make sure the cache directory is writable
throw new Kohana_Exception('cache.unwritable', $directory);
// Directory is valid
$this->directory = $directory;
}
/**
* Finds an array of files matching the given id or tag.
*
* @param string cache id or tag
* @param bool search for tags
* @return array of filenames matching the id or tag
*/
public function exists($id, $tag = FALSE)
{
if ($id === TRUE)
{
// Find all the files
return glob($this->directory.'*~*~*'); }
elseif ($tag === TRUE)
{
// Find all the files that have the tag name
$paths = glob($this->directory.'*~*'.$id.'*~*');
// Find all tags matching the given tag
foreach ($paths as $path)
{
// Split the files
// Find valid tags
continue;
// Split the tags by plus signs, used to separate tags
{
// Add the file to the array, it has the requested tag
$files[] = $path;
}
}
return $files;
}
else
{
// Find the file matching the given id
return glob($this->directory.$id.'~*'); }
}
/**
* Sets a cache item to the given data, tags, and lifetime.
*
* @param string cache id to set
* @param string data in the cache
* @param array cache tags
* @param integer lifetime
* @return bool
*/
public function set
($id, $data, array $tags = NULL, $lifetime) {
// Remove old cache files
$this->delete($id);
// Cache File driver expects unix timestamp
if ($lifetime !== 0)
{
}
{
// Convert the tags into a string list
}
// Write out a serialized cache
}
/**
* Finds an array of ids for a given tag.
*
* @param string tag name
* @return array of ids that match the tag
*/
public function find($tag)
{
// An array will always be returned
if ($paths = $this->exists($tag, TRUE))
{
// Length of directory name
$offset = strlen($this->directory);
// Find all the files with the given tag
foreach ($paths as $path)
{
// Get the id from the filename
if (($data = $this->get($id)) !== FALSE)
{
// Add the result to the array
$result[$id] = $data;
}
}
}
return $result;
}
/**
* Fetches a cache item. This will delete the item if it is expired or if
* the hash does not match the stored hash.
*
* @param string cache id
* @return mixed|NULL
*/
public function get($id)
{
if ($file = $this->exists($id))
{
// Use the first file
// Validate that the cache has not expired
if ($this->expired($file))
{
// Remove this cache, it has expired
$this->delete($id);
}
else
{
// Turn off errors while reading the file
{
// Unserialize the data
}
else
{
// Delete the data
}
// Turn errors back on
}
}
// Return NULL if there is no data
return isset($data) ?
$data : NULL; }
/**
* Deletes a cache item by id or tag
*
* @param string cache id or tag, or TRUE for "all items"
* @param boolean use tags
* @return boolean
*/
public function delete($id, $tag = FALSE)
{
$files = $this->exists($id, $tag);
return FALSE;
// Disable all error reporting while deleting
foreach ($files as $file)
{
// Remove the cache file
Kohana
::log('error', 'Cache: Unable to delete cache file: '.$file); }
// Turn on error reporting again
return TRUE;
}
/**
* Deletes all cache files that are older than the current time.
*
* @return void
*/
public function deleteExpired()
{
if ($files = $this->exists(TRUE))
{
// Disable all error reporting while deleting
foreach ($files as $file)
{
if ($this->expired($file))
{
// The cache file has already expired, delete it
Kohana
::log('error', 'Cache: Unable to delete cache file: '.$file); }
}
// Turn on error reporting again
}
}
/**
* Check if a cache file has expired by filename.
*
* @param string filename
* @return bool
*/
protected function expired($file)
{
// Get the expiration time
// Expirations of 0 are "never expire"
return ($expires !== 0 AND
$expires <= time()); }
} // End Cache File Driver