Merge branch 'develop' of https://github.com/Orange-Management/phpOMS into develop

This commit is contained in:
Dennis Eichhorn 2016-11-06 14:42:36 +01:00
commit dc19c6eec5
6 changed files with 50 additions and 29 deletions

View File

@ -13,7 +13,8 @@
* @version 1.0.0 * @version 1.0.0
* @link http://orange-management.com * @link http://orange-management.com
*/ */
namespace phpOMS\DataStorage\Database\Connection; namespace phpOMS\DataStorage\Cache;
use phpOMS\DataStorage\Cache\CacheInterface; use phpOMS\DataStorage\Cache\CacheInterface;
use phpOMS\DataStorage\Cache\FileCache; use phpOMS\DataStorage\Cache\FileCache;

View File

@ -17,7 +17,7 @@ namespace phpOMS\DataStorage\Cache;
use phpOMS\Config\OptionsInterface; use phpOMS\Config\OptionsInterface;
use phpOMS\Config\OptionsTrait; use phpOMS\Config\OptionsTrait;
use phpOMS\DataStorage\Database\Connection\CacheFactory; use phpOMS\DataStorage\Cache\CacheFactory;
/** /**

View File

@ -142,9 +142,9 @@ class FileCache implements CacheInterface
return false; return false;
} }
$path = File::sanitize($key); $path = File::sanitize($key, '~');
file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); file_put_contents($this->cachePath . '/' . $path . '.cache', $this->build($value, $expire));
return false; return false;
} }
@ -158,10 +158,11 @@ class FileCache implements CacheInterface
return false; return false;
} }
$path = File::sanitize($key); $name = File::sanitize($key, '~');
$path = $this->cachePath . '/' . $path . '.cache';
if (!file_exists($this->cachePath . '/' . $path)) { if (!file_exists($path)) {
file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); file_put_contents($path, $this->build($value, $expire));
return true; return true;
} }
@ -260,9 +261,9 @@ class FileCache implements CacheInterface
private function getExpire(string $raw) : int private function getExpire(string $raw) : int
{ {
$expireStart = strpos($raw, self::DELIM); $expireStart = strpos($raw, self::DELIM);
$expireEnd = strpos($raw, self::DELIM, $expireStart); $expireEnd = strpos($raw, self::DELIM, $expireStart+1);
return (int) substr($raw, $expireStart, $expireEnd); return (int) substr($raw, $expireStart+1, $expireEnd - ($expireStart+1));
} }
/** /**
@ -274,11 +275,17 @@ class FileCache implements CacheInterface
return null; return null;
} }
$name = Directory::sanitize($key); $name = File::sanitize($key, '~');
$created = Directory::created($path = $this->cachePath . '/' . $name)->getTimestamp(); $path = $this->cachePath . '/' . $name . '.cache';
if(!file_exists($path)) {
return null;
}
$created = Directory::created($path)->getTimestamp();
$now = time(); $now = time();
if ($expire >= 0 && $created + $expire > $now) { if ($expire >= 0 && $created + $expire < $now) {
return null; return null;
} }
@ -286,10 +293,10 @@ class FileCache implements CacheInterface
$type = $raw[0]; $type = $raw[0];
$expireStart = strpos($raw, self::DELIM); $expireStart = strpos($raw, self::DELIM);
$expireEnd = strpos($raw, self::DELIM, $expireStart); $expireEnd = strpos($raw, self::DELIM, $expireStart+1);
$cacheExpire = substr($raw, $expireStart, $expireEnd); $cacheExpire = substr($raw, $expireStart+1, $expireEnd - ($expireStart+1));
if ($cacheExpire >= 0 && $created + $cacheExpire > $now) { if ($cacheExpire >= 0 && $created + $cacheExpire < $now) {
$this->delete($key); $this->delete($key);
return null; return null;
@ -316,7 +323,7 @@ class FileCache implements CacheInterface
case CacheType::_SERIALIZABLE: case CacheType::_SERIALIZABLE:
case CacheType::_JSONSERIALIZABLE: case CacheType::_JSONSERIALIZABLE:
$namespaceStart = strpos($raw, self::DELIM, $expireEnd); $namespaceStart = strpos($raw, self::DELIM, $expireEnd);
$namespaceEnd = strpos($raw, self::DELIM, $namespaceStart); $namespaceEnd = strpos($raw, self::DELIM, $namespaceStart+1);
$namespace = substr($raw, $namespaceStart, $namespaceEnd); $namespace = substr($raw, $namespaceStart, $namespaceEnd);
$value = $namespace::unserialize(substr($raw, $namespaceEnd + 1)); $value = $namespace::unserialize(substr($raw, $namespaceEnd + 1));
@ -335,8 +342,8 @@ class FileCache implements CacheInterface
return false; return false;
} }
$name = File::sanitize($key); $name = File::sanitize($key, '~');
$path = $this->cachePath . '/' . $name; $path = $this->cachePath . '/' . $name . '.cache';
if ($expire < 0 && file_exists($path)) { if ($expire < 0 && file_exists($path)) {
unlink($path); unlink($path);
@ -349,8 +356,8 @@ class FileCache implements CacheInterface
$now = time(); $now = time();
$raw = file_get_contents($path); $raw = file_get_contents($path);
$expireStart = strpos($raw, self::DELIM); $expireStart = strpos($raw, self::DELIM);
$expireEnd = strpos($raw, self::DELIM, $expireStart); $expireEnd = strpos($raw, self::DELIM, $expireStart+1);
$cacheExpire = substr($raw, $expireStart, $expireEnd); $cacheExpire = substr($raw, $expireStart+1, $expireEnd - ($expireStart+1));
if ($cacheExpire >= 0 && $created + $cacheExpire > $now) { if ($cacheExpire >= 0 && $created + $cacheExpire > $now) {
unlink($path); unlink($path);
@ -398,10 +405,11 @@ class FileCache implements CacheInterface
return false; return false;
} }
$path = File::sanitize($key); $name = File::sanitize($key, '~');
$path = $this->cachePath . '/' . $path . '.cache';
if (file_exists($this->cachePath . '/' . $path)) { if (file_exists($path)) {
file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); file_put_contents($path, $this->build($value, $expire));
return true; return true;
} }

View File

@ -197,13 +197,14 @@ interface ContainerInterface
* Make name/path operating system safe. * Make name/path operating system safe.
* *
* @param string $path Path of the resource * @param string $path Path of the resource
* @param string $replace Replace invalid chars with
* *
* @return string * @return string
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function sanitize(string $path) : string; public static function sanitize(string $path, string $replace = '') : string;
/** /**
* Get amount of sub-resources. * Get amount of sub-resources.

View File

@ -192,10 +192,21 @@ class Directory extends FileAbstract implements DirectoryInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
* todo: move to fileAbastract since it should be the same for file and directory?
*/ */
public static function created(string $path) : \DateTime public static function created(string $path) : \DateTime
{ {
// TODO: Implement created() method. if(!file_exists($path)) {
$created = new \DateTime();
$created->setTimestamp(0);
return $created;
}
$created = new \DateTime('now');
$created->setTimestamp(filemtime($path));
return $created;
} }
/** /**
@ -249,9 +260,9 @@ class Directory extends FileAbstract implements DirectoryInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function sanitize(string $path) : string public static function sanitize(string $path, string $replace = '') : string
{ {
return preg_replace('[^\w\s\d\.\-_~,;:\[\]\(\]\/]', '', $path); return preg_replace('[^\w\s\d\.\-_~,;:\[\]\(\]\/]', $replace, $path);
} }
/** /**

View File

@ -143,9 +143,9 @@ class File extends FileAbstract implements FileInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function sanitize(string $path) : string public static function sanitize(string $path, string $replace = '') : string
{ {
return preg_replace('[^\w\s\d\.\-_~,;:\[\]\(\]]', '', $path); return preg_replace('/[^\w\s\d\.\-_~,;\[\]\(\]]/', $replace, $path);
} }
/** /**