Update
This commit is contained in:
parent
a37785b391
commit
33458b2ca3
9915 changed files with 1247019 additions and 0 deletions
3
trunk/_vendor/gregwar/cache/Gregwar/Cache/.gitignore
vendored
Normal file
3
trunk/_vendor/gregwar/cache/Gregwar/Cache/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
**.swp
|
||||
vendor
|
||||
composer.lock
|
||||
14
trunk/_vendor/gregwar/cache/Gregwar/Cache/.travis.yml
vendored
Normal file
14
trunk/_vendor/gregwar/cache/Gregwar/Cache/.travis.yml
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
language: php
|
||||
|
||||
php:
|
||||
- 5.3.3
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
|
||||
script:
|
||||
- composer install
|
||||
- mkdir tests/cache
|
||||
- chmod 777 tests/cache
|
||||
- phpunit
|
||||
365
trunk/_vendor/gregwar/cache/Gregwar/Cache/Cache.php
vendored
Executable file
365
trunk/_vendor/gregwar/cache/Gregwar/Cache/Cache.php
vendored
Executable file
|
|
@ -0,0 +1,365 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\Cache;
|
||||
|
||||
/**
|
||||
* A cache system based on files
|
||||
*
|
||||
* @author Gregwar <g.passault@gmail.com>
|
||||
*/
|
||||
class Cache implements CacheInterface
|
||||
{
|
||||
/**
|
||||
* Cache directory
|
||||
*/
|
||||
protected $cacheDirectory;
|
||||
|
||||
/**
|
||||
* Use a different directory as actual cache
|
||||
* @var string
|
||||
*/
|
||||
protected $actualCacheDirectory = null;
|
||||
|
||||
/**
|
||||
* Prefix directories size
|
||||
*
|
||||
* For instance, if the file is helloworld.txt and the prefix size is
|
||||
* 5, the cache file will be: h/e/l/l/o/helloworld.txt
|
||||
*
|
||||
* This is useful to avoid reaching a too large number of files into the
|
||||
* cache system directories
|
||||
* @var int
|
||||
*/
|
||||
protected $prefixSize = 5;
|
||||
|
||||
/**
|
||||
* Directory mode
|
||||
*
|
||||
* Allows setting of the access mode for the directories created.
|
||||
* @var int
|
||||
*/
|
||||
protected $directoryMode = 0755;
|
||||
|
||||
/**
|
||||
* Constructs the cache system
|
||||
*
|
||||
* @param string $cacheDirectory the cache directory
|
||||
*/
|
||||
public function __construct($cacheDirectory = 'cache')
|
||||
{
|
||||
$this->cacheDirectory = $cacheDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the cache directory
|
||||
*
|
||||
* @param string $cacheDirectory the cache directory
|
||||
* @return self
|
||||
*/
|
||||
public function setCacheDirectory($cacheDirectory)
|
||||
{
|
||||
$this->cacheDirectory = $cacheDirectory;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the cache directory
|
||||
*
|
||||
* @return string the cache directory
|
||||
*/
|
||||
public function getCacheDirectory()
|
||||
{
|
||||
return $this->cacheDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the actual cache directory
|
||||
*
|
||||
* @param string $actualCacheDirectory the actual cache directory
|
||||
* @return self
|
||||
*/
|
||||
public function setActualCacheDirectory($actualCacheDirectory = null)
|
||||
{
|
||||
$this->actualCacheDirectory = $actualCacheDirectory;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actual cache directory
|
||||
*/
|
||||
public function getActualCacheDirectory()
|
||||
{
|
||||
return $this->actualCacheDirectory ?: $this->cacheDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the prefix size
|
||||
*
|
||||
* @param int $prefixSize the size of the prefix directories
|
||||
* @return self
|
||||
*/
|
||||
public function setPrefixSize($prefixSize)
|
||||
{
|
||||
$this->prefixSize = $prefixSize;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the directory mode
|
||||
*
|
||||
* @param int $directoryMode the directory mode to use
|
||||
* @return self
|
||||
*/
|
||||
public function setDirectoryMode($directoryMode)
|
||||
{
|
||||
if (!$directoryMode) {
|
||||
$directoryMode = 0755;
|
||||
}
|
||||
$this->directoryMode = $directoryMode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a directory
|
||||
*
|
||||
* @param string $directory the target directory
|
||||
*/
|
||||
protected function mkdir($directory)
|
||||
{
|
||||
if (!is_dir($directory)) {
|
||||
@mkdir($directory, $this->directoryMode, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the cache file name
|
||||
*
|
||||
* @param string $filename the name of the cache file
|
||||
* @param bool $actual get the actual file or the public file
|
||||
* @param bool $mkdir a boolean to enable/disable the construction of the
|
||||
* cache file directory
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheFile($filename, $actual = false, $mkdir = false)
|
||||
{
|
||||
$path = array();
|
||||
|
||||
// Getting the length of the filename before the extension
|
||||
$parts = explode('.', $filename);
|
||||
$len = strlen($parts[0]);
|
||||
|
||||
for ($i=0; $i<min($len, $this->prefixSize); $i++) {
|
||||
$path[] = $filename[$i];
|
||||
|
||||
}
|
||||
$path = implode('/', $path);
|
||||
|
||||
if ($mkdir) {
|
||||
$actualDir = $this->getActualCacheDirectory() . '/' . $path;
|
||||
$this->mkdir($actualDir);
|
||||
}
|
||||
|
||||
$path .= '/' . $filename;
|
||||
|
||||
if ($actual) {
|
||||
return $this->getActualCacheDirectory() . '/' . $path;
|
||||
} else {
|
||||
return $this->getCacheDirectory() . '/' . $path;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the cache conditions are respected
|
||||
*
|
||||
* @param string $cacheFile the cache file
|
||||
* @param array $conditions an array of conditions to check
|
||||
* @return bool
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkConditions($cacheFile, array $conditions = array())
|
||||
{
|
||||
// Implicit condition: the cache file should exist
|
||||
if (!file_exists($cacheFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($conditions as $type => $value) {
|
||||
switch ($type) {
|
||||
case 'maxage':
|
||||
case 'max-age':
|
||||
// Return false if the file is older than $value
|
||||
$age = time() - filemtime($cacheFile);
|
||||
if ($age > $value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 'younger-than':
|
||||
case 'youngerthan':
|
||||
// Return false if the file is older than the file $value, or the files $value
|
||||
$check = function($filename) use ($cacheFile) {
|
||||
return !file_exists($filename) || filemtime($cacheFile) < filemtime($filename);
|
||||
};
|
||||
|
||||
if (!is_array($value)) {
|
||||
if (!$this->isRemote($value) && $check($value)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
foreach ($value as $file) {
|
||||
if (!$this->isRemote($file) && $check($file)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('Cache condition '.$type.' not supported');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the target filename exists in the cache and if the conditions
|
||||
* are respected
|
||||
*
|
||||
* @param string $filename the filename
|
||||
* @param array $conditions the conditions to respect
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($filename, array $conditions = array())
|
||||
{
|
||||
$cacheFile = $this->getCacheFile($filename, true);
|
||||
|
||||
return $this->checkConditions($cacheFile, $conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for exists
|
||||
*
|
||||
* @param string $filename the filename
|
||||
* @param array $conditions the conditions to respect
|
||||
* @return bool
|
||||
*/
|
||||
public function check($filename, array $conditions = array())
|
||||
{
|
||||
return $this->exists($filename, $conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data in the cache
|
||||
*
|
||||
* @param string $filename the name of the cache file
|
||||
* @param string $contents the contents to store
|
||||
* @return self
|
||||
*/
|
||||
public function set($filename, $contents = '')
|
||||
{
|
||||
$cacheFile = $this->getCacheFile($filename, true, true);
|
||||
|
||||
file_put_contents($cacheFile, $contents, \LOCK_EX);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for set()
|
||||
*
|
||||
* @param string $filename the name of the cache file
|
||||
* @param string $contents the contents to store
|
||||
* @return self
|
||||
*/
|
||||
public function write($filename, $contents = '')
|
||||
{
|
||||
return $this->set($filename, $contents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data from the cache
|
||||
*
|
||||
* @param string $filename the cache file name
|
||||
* @param array $conditions
|
||||
* @return null|string
|
||||
*/
|
||||
public function get($filename, array $conditions = array())
|
||||
{
|
||||
if ($this->exists($filename, $conditions)) {
|
||||
return file_get_contents($this->getCacheFile($filename, true));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this URL remote?
|
||||
*
|
||||
* @param string $file
|
||||
* @return bool
|
||||
*/
|
||||
protected function isRemote($file)
|
||||
{
|
||||
if (preg_match('/^([a-z]+):\/\//', $file, $match)) {
|
||||
return ($match[1] != 'file');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the cache entry
|
||||
*
|
||||
* @param string $filename the cache file name
|
||||
* @param array $conditions an array of conditions about expiration
|
||||
* @param \Closure $function the closure to call if the file does not exist
|
||||
* @param bool $file returns the cache file or the file contents
|
||||
* @param bool $actual returns the actual cache file
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getOrCreate($filename, array $conditions = array(), $function, $file = false, $actual = false)
|
||||
{
|
||||
if (!is_callable($function)) {
|
||||
throw new \InvalidArgumentException('The argument $function should be callable');
|
||||
}
|
||||
|
||||
$cacheFile = $this->getCacheFile($filename, true, true);
|
||||
$data = null;
|
||||
|
||||
if (!$this->check($filename, $conditions)) {
|
||||
if(file_exists($cacheFile)) {
|
||||
unlink($cacheFile);
|
||||
}
|
||||
|
||||
$data = call_user_func($function, $cacheFile);
|
||||
|
||||
// Test if the closure wrote the file or if it returned the data
|
||||
if (!file_exists($cacheFile)) {
|
||||
$this->set($filename, $data);
|
||||
} else {
|
||||
$data = file_get_contents($cacheFile);
|
||||
}
|
||||
}
|
||||
|
||||
return $file ? $this->getCacheFile($filename, $actual) : file_get_contents($cacheFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias to getOrCreate with $file = true
|
||||
*
|
||||
* @param string $filename the cache file name
|
||||
* @param array $conditions an array of conditions about expiration
|
||||
* @param \Closure $function the closure to call if the file does not exist
|
||||
* @param bool $actual returns the actual cache file
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getOrCreateFile($filename, array $conditions = array(), $function, $actual = false)
|
||||
{
|
||||
return $this->getOrCreate($filename, $conditions, $function, true, $actual);
|
||||
}
|
||||
}
|
||||
131
trunk/_vendor/gregwar/cache/Gregwar/Cache/CacheInterface.php
vendored
Normal file
131
trunk/_vendor/gregwar/cache/Gregwar/Cache/CacheInterface.php
vendored
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
<?php namespace Gregwar\Cache;
|
||||
|
||||
interface CacheInterface {
|
||||
|
||||
/**
|
||||
* Sets the cache directory
|
||||
*
|
||||
* @param string $cacheDirectory the cache directory
|
||||
* @return self
|
||||
*/
|
||||
public function setCacheDirectory($cacheDirectory);
|
||||
|
||||
/**
|
||||
* Gets the cache directory
|
||||
*
|
||||
* @return string the cache directory
|
||||
*/
|
||||
public function getCacheDirectory();
|
||||
|
||||
/**
|
||||
* Sets the actual cache directory
|
||||
*
|
||||
* @param string $actualCacheDirectory the actual cache directory
|
||||
* @return self
|
||||
*/
|
||||
public function setActualCacheDirectory($actualCacheDirectory = null);
|
||||
|
||||
/**
|
||||
* Returns the actual cache directory
|
||||
*/
|
||||
public function getActualCacheDirectory();
|
||||
|
||||
/**
|
||||
* Change the prefix size
|
||||
*
|
||||
* @param int $prefixSize the size of the prefix directories
|
||||
* @return self
|
||||
*/
|
||||
public function setPrefixSize($prefixSize);
|
||||
|
||||
/**
|
||||
* Change the directory mode
|
||||
*
|
||||
* @param int $directoryMode the directory mode to use
|
||||
* @return self
|
||||
*/
|
||||
public function setDirectoryMode($directoryMode);
|
||||
|
||||
/**
|
||||
* Gets the cache file name
|
||||
*
|
||||
* @param string $filename the name of the cache file
|
||||
* @param bool $actual get the actual file or the public file
|
||||
* @param bool $mkdir a boolean to enable/disable the construction of the
|
||||
* cache file directory
|
||||
* @return string
|
||||
*/
|
||||
public function getCacheFile($filename, $actual = false, $mkdir = false);
|
||||
|
||||
/**
|
||||
* Checks if the target filename exists in the cache and if the conditions
|
||||
* are respected
|
||||
*
|
||||
* @param string $filename the filename
|
||||
* @param array $conditions the conditions to respect
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($filename, array $conditions = array());
|
||||
|
||||
/**
|
||||
* Alias for exists
|
||||
*
|
||||
* @param string $filename the filename
|
||||
* @param array $conditions the conditions to respect
|
||||
* @return bool
|
||||
*/
|
||||
public function check($filename, array $conditions = array());
|
||||
|
||||
/**
|
||||
* Write data in the cache
|
||||
*
|
||||
* @param string $filename the name of the cache file
|
||||
* @param string $contents the contents to store
|
||||
* @return self
|
||||
*/
|
||||
public function set($filename, $contents = '');
|
||||
|
||||
/**
|
||||
* Alias for set()
|
||||
*
|
||||
* @param string $filename the name of the cache file
|
||||
* @param string $contents the contents to store
|
||||
* @return self
|
||||
*/
|
||||
public function write($filename, $contents = '');
|
||||
|
||||
/**
|
||||
* Get data from the cache
|
||||
*
|
||||
* @param string $filename the cache file name
|
||||
* @param array $conditions
|
||||
* @return null|string
|
||||
*/
|
||||
public function get($filename, array $conditions = array());
|
||||
|
||||
/**
|
||||
* Get or create the cache entry
|
||||
*
|
||||
* @param string $filename the cache file name
|
||||
* @param array $conditions an array of conditions about expiration
|
||||
* @param \Closure $function the closure to call if the file does not exist
|
||||
* @param bool $file returns the cache file or the file contents
|
||||
* @param bool $actual returns the actual cache file
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getOrCreate($filename, array $conditions = array(), $function, $file = false, $actual = false);
|
||||
|
||||
/**
|
||||
* Alias to getOrCreate with $file = true
|
||||
*
|
||||
* @param string $filename the cache file name
|
||||
* @param array $conditions an array of conditions about expiration
|
||||
* @param \Closure $function the closure to call if the file does not exist
|
||||
* @param bool $actual returns the actual cache file
|
||||
* @return string
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function getOrCreateFile($filename, array $conditions = array(), $function, $actual = false);
|
||||
|
||||
}
|
||||
86
trunk/_vendor/gregwar/cache/Gregwar/Cache/GarbageCollect.php
vendored
Normal file
86
trunk/_vendor/gregwar/cache/Gregwar/Cache/GarbageCollect.php
vendored
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace Gregwar\Cache;
|
||||
|
||||
/**
|
||||
* Garbage collect a directory, this will crawl a directory, lookng
|
||||
* for files older than X days and destroy them
|
||||
*
|
||||
* @author Gregwar <g.passault@gmail.com>
|
||||
*/
|
||||
class GarbageCollect
|
||||
{
|
||||
/**
|
||||
* Drops old files of a directory
|
||||
*
|
||||
* @param string $directory the name of the target directory
|
||||
* @param int $days the number of days to consider a file old
|
||||
* @param bool $verbose enable verbose output
|
||||
*
|
||||
* @return bool true if all the files/directories of a directory was wiped
|
||||
*/
|
||||
public static function dropOldFiles($directory, $days = 30, $verbose = false)
|
||||
{
|
||||
$allDropped = true;
|
||||
$now = time();
|
||||
|
||||
$dir = opendir($directory);
|
||||
|
||||
if (!$dir) {
|
||||
if ($verbose) {
|
||||
echo "! Unable to open $directory\n";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
while ($file = readdir($dir)) {
|
||||
if ($file == '.' || $file == '..') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fullName = $directory.'/'.$file;
|
||||
|
||||
$old = $now-filemtime($fullName);
|
||||
|
||||
if (is_dir($fullName)) {
|
||||
// Directories are recursively crawled
|
||||
if (static::dropOldFiles($fullName, $days, $verbose)) {
|
||||
self::drop($fullName, $verbose);
|
||||
} else {
|
||||
$allDropped = false;
|
||||
}
|
||||
} else {
|
||||
if ($old > (24*60*60*$days)) {
|
||||
self::drop($fullName, $verbose);
|
||||
} else {
|
||||
$allDropped = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir($dir);
|
||||
|
||||
return $allDropped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a file or an empty directory
|
||||
*
|
||||
* @param string $file the file to be removed
|
||||
* @param bool $verbose the verbosity
|
||||
*/
|
||||
public static function drop($file, $verbose = false)
|
||||
{
|
||||
if (is_dir($file)) {
|
||||
@rmdir($file);
|
||||
} else {
|
||||
@unlink($file);
|
||||
}
|
||||
|
||||
if ($verbose) {
|
||||
echo "> Dropping $file...\n";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
19
trunk/_vendor/gregwar/cache/Gregwar/Cache/LICENSE
vendored
Normal file
19
trunk/_vendor/gregwar/cache/Gregwar/Cache/LICENSE
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) <2013> Grégoire Passault
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
165
trunk/_vendor/gregwar/cache/Gregwar/Cache/README.md
vendored
Normal file
165
trunk/_vendor/gregwar/cache/Gregwar/Cache/README.md
vendored
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
Cache
|
||||
=====
|
||||
|
||||

|
||||
|
||||
This is a lightweight cache system based on file and directories.
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
||||
Step 1: Install it
|
||||
------------------
|
||||
|
||||
Via composer:
|
||||
|
||||
```json
|
||||
{
|
||||
"require": {
|
||||
"gregwar/cache": "1.0.*"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Or with a clone of the repository:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/Gregwar/Cache.git
|
||||
```
|
||||
|
||||
Or downloading it:
|
||||
|
||||
* [Download .zip](https://github.com/Gregwar/Cache/archive/master.zip)
|
||||
* [Download .tar.gz](https://github.com/Gregwar/Cache/archive/master.tar.gz)
|
||||
|
||||
Step 2: Setup the rights
|
||||
------------------------
|
||||
|
||||
You need your PHP script to have access to the cache directory, you can for instance
|
||||
create a `cache` directory (be sure the web server can write it):
|
||||
|
||||
```
|
||||
mkdir cache
|
||||
```
|
||||
|
||||
Step 3: Access the cache
|
||||
------------------------
|
||||
|
||||
To access the cache, you can do like this:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
include('vendor/autoload.php'); // If using composer
|
||||
|
||||
use Gregwar\Cache\Cache;
|
||||
|
||||
$cache = new Cache;
|
||||
$cache->setCacheDirectory('cache'); // This is the default
|
||||
|
||||
// If the cache exists, this will return it, else, the closure will be called
|
||||
// to create this image
|
||||
$data = $cache->getOrCreate('red-square.png', array(), function($filename) {
|
||||
$i = imagecreatetruecolor(100, 100);
|
||||
imagefill($i, 0, 0, 0xff0000);
|
||||
imagepng($i, $filename);
|
||||
});
|
||||
|
||||
header('Content-type: image/png');
|
||||
echo $data;
|
||||
```
|
||||
|
||||
This will render a red square. If the cache file (which will look like `cache/r/e/d/-/s/red-square.png')
|
||||
exists, it will be read, else, the closure will be called in order to create the cache file.
|
||||
|
||||
API
|
||||
===
|
||||
|
||||
You can use the following methods:
|
||||
|
||||
* `setCacheDirectory($directory)`: sets the cache directory (see below).
|
||||
* `setActualCacheDirectory($directory)`: sets the actual cache directory (see below).
|
||||
* `exists($filename, $conditions = array())`: check that the $filename file exists in the cache, checking
|
||||
the conditions (see below).
|
||||
* `check($filename, $conditions = array())`: alias for `exists`.
|
||||
* `getCacheFile($filename, $actual = false, $mkdir = false)`: gets the cache file. If the `$actual` flag
|
||||
is true, the actual cache file name will be returned (see below), if the `$mkdir` flag is true, the
|
||||
cache file directories tree will be created.
|
||||
* `set($filename, $contents)`: write contents to `$filename` cache file.
|
||||
* `write($filename, $contents)`: alias for `set()`
|
||||
* `get($filename, $conditions = array())`: if the cache file for `$filename` exists, contents will be
|
||||
returned, else, `NULL` will be returned.
|
||||
* `setPrefixSize($prefixSize)`: sets the prefix size for directories, default is 5. For instance, the
|
||||
cache file for `helloworld.txt`, will be `'h/e/l/l/o/helloworld.txt`.
|
||||
* `setDirectoryMode($directoryMode)`: sets the directory mode when creating directories, default is `0755`.
|
||||
Does not affect any directories previously created.
|
||||
* `getOrCreate($filename, $conditions = array(), $function, $file = false)`: this will check if the `$filename`
|
||||
cache file exists and verifies `$conditions` (see below). If the cache file is OK, it will return its
|
||||
contents. Else, it will call the `$function`, passing it the target file, this function can write the
|
||||
file given in parameter or just return data. Then, cache data will be returned. If `$file` flag is set,
|
||||
the cache file name will be returned instead of file data.
|
||||
|
||||
Note: consider using an hash for the `$filename` cache file, to avoid special characters.
|
||||
|
||||
Conditions
|
||||
==========
|
||||
|
||||
You can use conditions to manage file expirations on the cache, there is two way of expiring:
|
||||
|
||||
* Using `max-age`, in seconds, to set the maximum age of the file
|
||||
* Using `younger-than`, by passing another file, this will compare the modification date
|
||||
and regenerate the cache if the given file is younger.
|
||||
|
||||
For instance, if you want to uppercase a file:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Gregwar\Cache\Cache;
|
||||
|
||||
$cache = new Cache;
|
||||
|
||||
$data = $cache->getOrCreate('uppercase.txt',
|
||||
array(
|
||||
'younger-than' => 'original.txt'
|
||||
),
|
||||
function() {
|
||||
echo "Generating file...\n";
|
||||
return strtoupper(file_get_contents('original.txt'));
|
||||
});
|
||||
|
||||
echo $data;
|
||||
```
|
||||
|
||||
This will be create the `uppercase.txt` cache file by uppercasing the `original.txt` if the cache file
|
||||
does not exists or if the `original.txt` file is more recent than the cache file.
|
||||
|
||||
For instance:
|
||||
|
||||
```
|
||||
php uppercase.php # Will generate the cache file
|
||||
php uppercase.php # Will not generate the cache file
|
||||
touch original.txt # Sets the last modification time to now
|
||||
php uppercase.php # Will re-generate the cache file
|
||||
```
|
||||
|
||||
Cache directory and actual cache directory
|
||||
==========================================
|
||||
|
||||
In some cases, you'll want to get the cache file name. For instance, if you're caching
|
||||
images, you'll want to give a string like `cache/s/o/m/e/i/someimage.png` to put it into
|
||||
an `<img>` tag. This can be done by passing the `$file` argument to the `getOrCreate` to true,
|
||||
or directly using `getCacheFile` method (see above).
|
||||
|
||||
However, the visible `cache` directory of your users is not the same as the absolute path
|
||||
you want to access. To do that, you can set both the cache directory and the actual cache directory.
|
||||
|
||||
The cache directory is the prefix visible by the users (for instance: `cache/s/o/m/e/i/someimage.png`),
|
||||
and the actual cache directory is the prefix to use to actually access to the image (for instance:
|
||||
`/var/www/somesite/cache/s/o/m/e/i/someimage.png`). This way, the file will be accessed using absolute
|
||||
path and the cache file returned will directly be usable for your user's browsers.
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
This repository is under the MIT license, have a look at the `LICENCE` file.
|
||||
16
trunk/_vendor/gregwar/cache/Gregwar/Cache/autoload.php
vendored
Normal file
16
trunk/_vendor/gregwar/cache/Gregwar/Cache/autoload.php
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Registers an autoload for all the classes in Gregwar\Cache
|
||||
*/
|
||||
spl_autoload_register(function ($className) {
|
||||
$namespace = 'Gregwar\\Cache';
|
||||
|
||||
if (strpos($className, $namespace) === 0) {
|
||||
$className = str_replace($namespace, '', $className);
|
||||
$fileName = __DIR__ . '/' . str_replace('\\', '/', $className) . '.php';
|
||||
if (file_exists($fileName)) {
|
||||
require($fileName);
|
||||
}
|
||||
}
|
||||
});
|
||||
21
trunk/_vendor/gregwar/cache/Gregwar/Cache/composer.json
vendored
Normal file
21
trunk/_vendor/gregwar/cache/Gregwar/Cache/composer.json
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "gregwar/cache",
|
||||
"description": "A lightweight file-system cache system",
|
||||
"keywords": ["cache", "caching", "system", "file-system"],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Gregwar",
|
||||
"email": "g.passault@gmail.com"
|
||||
}
|
||||
],
|
||||
"target-dir": "Gregwar/Cache",
|
||||
"require": {
|
||||
"php": ">=5.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Gregwar\\Cache": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
1
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/.gitignore
vendored
Normal file
1
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
cache/
|
||||
14
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/garbage.php
vendored
Normal file
14
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/garbage.php
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
include('../autoload.php'); // If using composer
|
||||
|
||||
use Gregwar\Cache\GarbageCollect;
|
||||
|
||||
if (!is_dir('cache')) {
|
||||
`mkdir cache`;
|
||||
}
|
||||
`touch -t 9901010101 cache/foo`;
|
||||
`touch cache/bar`;
|
||||
|
||||
GarbageCollect::dropOldFiles(__DIR__.'/cache', 30, true);
|
||||
|
||||
23
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/max-age.php
vendored
Normal file
23
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/max-age.php
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
include('../autoload.php');
|
||||
|
||||
$cache = new Gregwar\Cache\Cache;
|
||||
|
||||
$data = $cache->getOrCreate('uppercase.txt', array('max-age' => 2), function() {
|
||||
echo "First call: generating file...\n";
|
||||
return strtoupper(file_get_contents('original.txt'));
|
||||
});
|
||||
|
||||
$data = $cache->getOrCreate('uppercase.txt', array('max-age' => 2), function() {
|
||||
echo "Second call: generating file, this should not happen!...\n";
|
||||
return strtoupper(file_get_contents('original.txt'));
|
||||
});
|
||||
|
||||
echo "Waiting 4s...\n";
|
||||
sleep(4);
|
||||
|
||||
$data = $cache->getOrCreate('uppercase.txt', array('max-age' => 2), function() {
|
||||
echo "Third call: generating cache file, because it expired...\n";
|
||||
return strtoupper(file_get_contents('original.txt'));
|
||||
});
|
||||
4
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/original.txt
vendored
Normal file
4
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/original.txt
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
There he goes.
|
||||
One of God's own prototypes.
|
||||
A high-powered mutant of some kind never even considered for mass production.
|
||||
Too weird to live, and too rare to die.
|
||||
20
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/red-file.php
vendored
Normal file
20
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/red-file.php
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
include('../autoload.php'); // If using composer
|
||||
|
||||
use Gregwar\Cache\Cache;
|
||||
|
||||
$cache = new Cache;
|
||||
$cache->setCacheDirectory('cache'); // This is the default
|
||||
|
||||
// If the cache exists, this will return it, else, the closure will be called
|
||||
// to create this image
|
||||
$file = $cache->getOrCreateFile('red-square.png', array(), function($filename) {
|
||||
$i = imagecreatetruecolor(100, 100);
|
||||
imagefill($i, 0, 0, 0xff0000);
|
||||
file_put_contents($filename, 'abc');
|
||||
imagepng($i, 'a.png');
|
||||
imagepng($i, $filename);
|
||||
});
|
||||
|
||||
echo $file, "\n";
|
||||
20
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/red.php
vendored
Normal file
20
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/red.php
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
include('../autoload.php'); // If using composer
|
||||
|
||||
use Gregwar\Cache\Cache;
|
||||
|
||||
$cache = new Cache;
|
||||
$cache->setCacheDirectory('cache'); // This is the default
|
||||
|
||||
// If the cache exists, this will return it, else, the closure will be called
|
||||
// to create this image
|
||||
$data = $cache->getOrCreate('red-square.png', array(), function($filename) {
|
||||
$i = imagecreatetruecolor(100, 100);
|
||||
imagefill($i, 0, 0, 0xff0000);
|
||||
file_put_contents($filename, 'abc');
|
||||
imagepng($i, $filename);
|
||||
});
|
||||
|
||||
header('Content-type: image/png');
|
||||
echo $data;
|
||||
14
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/uppercase.php
vendored
Normal file
14
trunk/_vendor/gregwar/cache/Gregwar/Cache/demo/uppercase.php
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
include('../autoload.php');
|
||||
|
||||
use Gregwar\Cache\Cache;
|
||||
|
||||
$cache = new Cache;
|
||||
|
||||
$data = $cache->getOrCreate('uppercase.txt', array('younger-than' => 'original.txt'), function() {
|
||||
echo "Generating file...\n";
|
||||
return strtoupper(file_get_contents('original.txt'));
|
||||
});
|
||||
|
||||
echo $data;
|
||||
20
trunk/_vendor/gregwar/cache/Gregwar/Cache/phpunit.xml
vendored
Normal file
20
trunk/_vendor/gregwar/cache/Gregwar/Cache/phpunit.xml
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
bootstrap="tests/bootstrap.php"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Cache testing">
|
||||
<file>./tests/CacheTests.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
</phpunit>
|
||||
208
trunk/_vendor/gregwar/cache/Gregwar/Cache/tests/CacheTests.php
vendored
Normal file
208
trunk/_vendor/gregwar/cache/Gregwar/Cache/tests/CacheTests.php
vendored
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
<?php
|
||||
|
||||
use Gregwar\Cache\Cache;
|
||||
|
||||
/**
|
||||
* Unit testing for Cache
|
||||
*/
|
||||
class CacheTests extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testContract()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
$this->assertInstanceOf('Gregwar\Cache\CacheInterface', $cache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing that file names are good
|
||||
*/
|
||||
public function testFileName()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
|
||||
$cacheDir = $this->getCacheDirectory();
|
||||
$actualCacheDir = $this->getActualCacheDirectory();
|
||||
$cacheFile = $cache->getCacheFile('helloworld.txt');
|
||||
$actualCacheFile = $cache->getCacheFile('helloworld.txt', true);
|
||||
$this->assertEquals($cacheDir . '/h/e/l/l/o/helloworld.txt', $cacheFile);
|
||||
$this->assertEquals($actualCacheDir . '/h/e/l/l/o/helloworld.txt', $actualCacheFile);
|
||||
|
||||
$cacheFile = $cache->getCacheFile('xy.txt');
|
||||
$actualCacheFile = $cache->getCacheFile('xy.txt', true);
|
||||
$this->assertEquals($cacheDir . '/x/y/xy.txt', $cacheFile);
|
||||
$this->assertEquals($actualCacheDir . '/x/y/xy.txt', $actualCacheFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing caching a file
|
||||
*/
|
||||
public function testCaching()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
|
||||
$this->assertFalse($cache->exists('testing.txt'));
|
||||
$cache->set('testing.txt', 'toto');
|
||||
$this->assertTrue($cache->exists('testing.txt'));
|
||||
|
||||
$this->assertFalse($cache->exists('testing2.txt'));
|
||||
$cache->write('testing2.txt', 'toto');
|
||||
$this->assertTrue($cache->exists('testing2.txt'));
|
||||
|
||||
$this->assertFalse($cache->exists('testing.txt', array(
|
||||
'max-age' => -1
|
||||
)));
|
||||
$this->assertTrue($cache->exists('testing.txt', array(
|
||||
'max-age' => 2
|
||||
)));
|
||||
sleep(3);
|
||||
$this->assertFalse($cache->exists('testing.txt', array(
|
||||
'max-age' => 2
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the getOrCreate function
|
||||
*/
|
||||
public function testGetOrCreate()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
|
||||
$this->assertFalse($cache->exists('testing.txt'));
|
||||
|
||||
$data = $cache->getOrCreate('testing.txt', array(), function() {
|
||||
return 'zebra';
|
||||
});
|
||||
|
||||
$this->assertTrue($cache->exists('testing.txt'));
|
||||
$this->assertEquals('zebra', $data);
|
||||
|
||||
$data = $cache->getOrCreate('testing.txt', array(), function() {
|
||||
return 'elephant';
|
||||
});
|
||||
$this->assertEquals('zebra', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the getOrCreate function with a callable
|
||||
*/
|
||||
public function testGetOrCreateWithCallable()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
|
||||
$this->assertFalse($cache->exists('testing.txt'));
|
||||
|
||||
$data = $cache->getOrCreate('testing.txt', array(), array($this, 'getAnimal'));
|
||||
|
||||
$this->assertTrue($cache->exists('testing.txt'));
|
||||
$this->assertEquals('orangutan', $data);
|
||||
}
|
||||
|
||||
public function getAnimal()
|
||||
{
|
||||
return 'orangutan';
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the getOrCreate function with $file=true
|
||||
*/
|
||||
public function testGetOrCreateFile()
|
||||
{
|
||||
$dir = __DIR__;
|
||||
$cache = $this->getCache();
|
||||
|
||||
$file = $dir.'/'.$cache->getOrCreateFile('file.txt', array(), function() {
|
||||
return 'xyz';
|
||||
});
|
||||
$file2 = $dir.'/'.$cache->getOrCreate('file.txt', array(), function(){}, true);
|
||||
|
||||
$this->assertEquals($file, $file2);
|
||||
$this->assertTrue(file_exists($file));
|
||||
$this->assertEquals('xyz', file_get_contents($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing that the not existing younger file works
|
||||
*/
|
||||
public function testNotExistingYounger()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
|
||||
$data = $cache->getOrCreate('testing.txt', array('younger-than'=> 'i-dont-exist'), function() {
|
||||
return 'some-data';
|
||||
});
|
||||
|
||||
$this->assertEquals('some-data', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing that directory mode works
|
||||
*/
|
||||
public function testDirectoryMode()
|
||||
{
|
||||
$dir = __DIR__;
|
||||
$cache = $this->getCache();
|
||||
$cacheDir = $this->getCacheDirectory();
|
||||
|
||||
// default permissions are 0755
|
||||
$data = $cache->getOrCreate('aaa.txt', array(), function () {
|
||||
return 'abc';
|
||||
});
|
||||
$this->assertTrue((fileperms("$dir/$cacheDir/a") & 0777) == 0755);
|
||||
$this->assertTrue((fileperms("$dir/$cacheDir/a/a") & 0777) == 0755);
|
||||
$this->assertTrue((fileperms("$dir/$cacheDir/a/a/a") & 0777) == 0755);
|
||||
|
||||
// Change permissions to be more restrictive
|
||||
$cache->setDirectoryMode(0700);
|
||||
$data = $cache->getOrCreate('bbb.txt', array(), function () {
|
||||
return 'abc';
|
||||
});
|
||||
$this->assertTrue((fileperms("$dir/$cacheDir/b") & 0777) == 0700);
|
||||
$this->assertTrue((fileperms("$dir/$cacheDir/b/b") & 0777) == 0700);
|
||||
$this->assertTrue((fileperms("$dir/$cacheDir/b/b/b") & 0777) == 0700);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing that remotes does not cause cache regeneration
|
||||
*/
|
||||
public function testRemote()
|
||||
{
|
||||
$cache = $this->getCache();
|
||||
$cache->set('remote', 'original');
|
||||
|
||||
$data = $cache->getOrCreate('remote', array('younger-than' => 'http://google.com'), function() {
|
||||
return 'modified';
|
||||
});
|
||||
$data = $cache->getOrCreate('remote', array('younger-than' => 'ftps://google.com'), function() {
|
||||
return 'modified';
|
||||
});
|
||||
$this->assertEquals('original', $data);
|
||||
}
|
||||
|
||||
protected function getCache()
|
||||
{
|
||||
$cache = new Cache;
|
||||
|
||||
return $cache
|
||||
->setPrefixSize(5)
|
||||
->setCacheDirectory($this->getCacheDirectory())
|
||||
->setActualCacheDirectory($this->getActualCacheDirectory())
|
||||
;
|
||||
}
|
||||
|
||||
protected function getActualCacheDirectory()
|
||||
{
|
||||
return __DIR__.'/'.$this->getCacheDirectory();
|
||||
}
|
||||
|
||||
protected function getCacheDirectory()
|
||||
{
|
||||
return 'cache';
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$cacheDirectory = $this->getActualCacheDirectory();
|
||||
`rm -rf $cacheDirectory`;
|
||||
}
|
||||
}
|
||||
3
trunk/_vendor/gregwar/cache/Gregwar/Cache/tests/bootstrap.php
vendored
Normal file
3
trunk/_vendor/gregwar/cache/Gregwar/Cache/tests/bootstrap.php
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
include(__DIR__ . '/../autoload.php');
|
||||
Loading…
Add table
Add a link
Reference in a new issue