Skip to content

Commit

Permalink
Merge pull request #4 from mjrider/fixes
Browse files Browse the repository at this point in the history
logic deduplication and coding style fixes
  • Loading branch information
mjrider committed Jul 2, 2014
2 parents 5e81bcc + d9311d4 commit 87a1c2f
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 398 deletions.
13 changes: 6 additions & 7 deletions src/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ class cache
*/
public static function create($prefix = null, $timeout = 7200, $context = null)
{
if ( !defined("ARC_CACHE_DIR") ) {
if (!defined("ARC_CACHE_DIR")) {
define( "ARC_CACHE_DIR", sys_get_temp_dir().'/arc/cache' );
}
if ( !file_exists( ARC_CACHE_DIR ) ) {
if (!file_exists( ARC_CACHE_DIR )) {
@mkdir( ARC_CACHE_DIR, 0770, true );
}
if ( !file_exists( ARC_CACHE_DIR ) ) {
if (!file_exists( ARC_CACHE_DIR )) {
throw new \arc\ExceptionConfigError("Cache Directory does not exist ( ".ARC_CACHE_DIR." )", \arc\exceptions::CONFIGURATION_ERROR);
}
if ( !is_dir( ARC_CACHE_DIR ) ) {
if (!is_dir( ARC_CACHE_DIR )) {
throw new \arc\ExceptionConfigError("Cache Directory is not a directory ( ".ARC_CACHE_DIR." )", \arc\exceptions::CONFIGURATION_ERROR);
}
if ( !is_writable( ARC_CACHE_DIR ) ) {
if (!is_writable( ARC_CACHE_DIR )) {
throw new \arc\ExceptionConfigError("Cache Directory is not writable ( ".ARC_CACHE_DIR." )", \arc\exceptions::CONFIGURATION_ERROR);
}
if (!$prefix) { // make sure you have a default prefix, so you won't clear other prefixes unintended
Expand All @@ -65,7 +65,7 @@ public static function getCacheStore()
public static function __callStatic($name, $args)
{
$store = self::getCacheStore();
if ( method_exists( $store, $name ) ) {
if (method_exists( $store, $name )) {
return call_user_func_array( array( $store, $name), $args);
} else {
throw new \arc\ExceptionMethodNotFound("Method $name not found in Cache Store", \arc\exceptions::OBJECT_NOT_FOUND);
Expand All @@ -76,5 +76,4 @@ public static function proxy($object, $timeout = 7200)
{
return new cache\Proxy( $object, self::getCacheStore(), $timeout );
}

}
271 changes: 135 additions & 136 deletions src/cache/FileStore.php
Original file line number Diff line number Diff line change
@@ -1,175 +1,174 @@
<?php

/*
* This file is part of the Ariadne Component Library.
*
* (c) Muze <info@muze.nl>0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// FIXME: not specific to \arc\cache really
/*
* This file is part of the Ariadne Component Library.
*
* (c) Muze <info@muze.nl>0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// FIXME: not specific to \arc\cache really
namespace arc\cache;

class FileStore
class FileStore
{
protected $root = null;
protected $currentPath = null;
protected $basePath = null;
protected $mode = null;

public function __construct($root, $currentPath = '/', $mode = 0770)
{
protected $root = null;
protected $currentPath = null;
protected $basePath = null;
protected $mode = null;

public function __construct($root, $currentPath = '/', $mode = 0770)
{
$this->root = $root;
$this->currentPath = $currentPath;
$this->basePath = $root . \arc\path::collapse( $currentPath );
$this->mode = $mode;
}
$this->root = $root;
$this->currentPath = $currentPath;
$this->basePath = $root . \arc\path::collapse( $currentPath );
$this->mode = $mode;
}

protected function getPath($name)
{
return $this->basePath . base64_encode( $name );
}
protected function getPath($name)
{
return $this->basePath . base64_encode( $name );
}

public function getVar($name)
{
$filePath = $this->getPath( $name );
if ( file_exists( $filePath ) ) {
return file_get_contents( $filePath );
}
public function getVar($name)
{
$filePath = $this->getPath( $name );
if (file_exists( $filePath )) {
return file_get_contents( $filePath );
}
}

public function putVar($name, $value)
{
$filePath = $this->getPath( $name );
$dir = dirname( $filePath );
if ( !file_exists( $dir ) ) {
mkdir( $dir, $this->mode, true ); //recursive
}

return file_put_contents( $filePath, $value, LOCK_EX );
public function putVar($name, $value)
{
$filePath = $this->getPath( $name );
$dir = dirname( $filePath );
if (!file_exists( $dir )) {
mkdir( $dir, $this->mode, true ); //recursive
}

public function getInfo($name)
{
$filePath = $this->getPath( $name );
if ( file_exists( $filePath ) && is_readable( $filePath ) ) {
return array(
'size' => filesize($filePath),
'ctime' => filectime( $filePath ),
'mtime' => filemtime( $filePath )
);
} else {
return null;
}
return file_put_contents( $filePath, $value, LOCK_EX );
}

public function getInfo($name)
{
$filePath = $this->getPath( $name );
if (file_exists( $filePath ) && is_readable( $filePath )) {
return array(
'size' => filesize($filePath),
'ctime' => filectime( $filePath ),
'mtime' => filemtime( $filePath )
);
} else {
return null;
}
}

public function setInfo($name, $info)
{
$filePath = $this->getPath( $name );
if ( file_exists( $filePath ) && is_readable( $filePath ) ) {
foreach ($info as $key => $value) {
switch ($key) {
case 'mtime':
touch( $filePath, $value );
public function setInfo($name, $info)
{
$filePath = $this->getPath( $name );
if (file_exists( $filePath ) && is_readable( $filePath )) {
foreach ($info as $key => $value) {
switch ($key) {
case 'mtime':
touch( $filePath, $value );
break;
case 'size':
case 'ctime':
// FIXME: ignore silently? other storage mechanisms might need this set explicitly?
case 'size':
case 'ctime':
// FIXME: ignore silently? other storage mechanisms might need this set explicitly?
break;
}
}

return true;
} else {
return false;
}
}

public function cd($path)
{
return new FileStore( $this->root, \arc\path::collapse( $path, $this->currentPath ), $this->mode );
return true;
} else {
return false;
}
}

public function ls()
{
$dir = dir( $this->basePath );
$result = array();
if ($dir) {
while ( $name = $dir->read() ) {
if ( !is_dir($this->basePath . $name ) ) {
$name = base64_decode($name);
}
$result[] = $name;
public function cd($path)
{
return new FileStore( $this->root, \arc\path::collapse( $path, $this->currentPath ), $this->mode );
}

public function ls()
{
$dir = dir( $this->basePath );
$result = array();
if ($dir) {
while ($name = $dir->read()) {
if (!is_dir($this->basePath . $name )) {
$name = base64_decode($name);
}
$dir->close();
$result[] = $name;
}

return $result;
$dir->close();
}

public function remove($name)
{
$filePath = $this->getPath( $name );
return $result;
}

return unlink( $filePath );
}
public function remove($name)
{
$filePath = $this->getPath( $name );

protected function cleanup($dir)
{
foreach ( glob( $dir . '/*' ) as $file ) {
if ( is_dir( $file ) ) {
$this->cleanup( $file );
} else {
unlink( $file );
}
}
rmdir( $dir );
}
return unlink( $filePath );
}

protected function rmdir($path, $cleanup = null)
{
if ( !isset( $cleanup ) ) {
$cleanup = array( $this, 'cleanup' );
protected function cleanup($dir)
{
foreach (glob( $dir . '/*' ) as $file) {
if (is_dir( $file )) {
$this->cleanup( $file );
} else {
unlink( $file );
}
call_user_func( $cleanup, $path );
}
rmdir( $dir );
}

public function purge($name = null)
{
if ($name) {
$this->clear( $name );
}
$dirPath = $this->basePath . \arc\path::collapse( \arc\path::clean( $name ) );
if ( file_exists( $dirPath ) && is_dir( $dirPath ) ) {
$this->rmdir( $dirPath );
}
protected function rmdir($path, $cleanup = null)
{
if (!isset( $cleanup )) {
$cleanup = array( $this, 'cleanup' );
}
call_user_func( $cleanup, $path );
}

return true;
public function purge($name = null)
{
if ($name) {
$this->clear( $name );
}
$dirPath = $this->basePath . \arc\path::collapse( \arc\path::clean( $name ) );
if (file_exists( $dirPath ) && is_dir( $dirPath )) {
$this->rmdir( $dirPath );
}

public function lock($name, $blocking = false)
{
$filePath = $this->getPath( $name );
$dir = dirname( $filePath );
if ( !file_exists( $dir ) ) {
mkdir( $dir, $this->mode, true ); //recursive
}
$lockFile = fopen( $filePath, 'c' );
$lockMode = LOCK_EX;
if (!$blocking) {
$lockMode = $lockMode|LOCK_NB;
}
return true;
}

return flock( $lockFile, $lockMode );
public function lock($name, $blocking = false)
{
$filePath = $this->getPath( $name );
$dir = dirname( $filePath );
if (!file_exists( $dir )) {
mkdir( $dir, $this->mode, true ); //recursive
}
$lockFile = fopen( $filePath, 'c' );
$lockMode = LOCK_EX;
if (!$blocking) {
$lockMode = $lockMode | LOCK_NB;
}

public function unlock($name)
{
$filePath = $this->getPath( $name );
$lockFile = fopen( $filePath, 'c' );
return flock( $lockFile, $lockMode );
}

return flock( $lockFile, LOCK_UN);
}
public function unlock($name)
{
$filePath = $this->getPath( $name );
$lockFile = fopen( $filePath, 'c' );

return flock( $lockFile, LOCK_UN);
}
}
1 change: 0 additions & 1 deletion src/cache/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,4 @@ public function __get($name)

return $result;
}

}
Loading

0 comments on commit 87a1c2f

Please sign in to comment.