Completed
Push — master ( 2106b4...7c92dc )
by Jean-Christophe
01:31
created

ArrayCache::_getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace micro\cache;
3
4
/**
5
 * This class is responsible for storing Arrays in PHP files.
6
 */
7
class ArrayCache extends AbstractDataCache{
8
	/**
9
	 *
10
	 * @var int The file mode used when creating new cache files
11
	 */
12
	private $_fileMode;
13
14
	/**
15
	 * Initializes the file cache-provider
16
	 * @param string $root absolute path to the root-folder where cache-files will be stored
17
	 * @param string Termination of file names
18
	 * @param int $fileMode file creation mode; defaults to 0777
19
	 */
20
	public function __construct($root, $postfix="", $fileMode=0777) {
21
		parent::__construct($root,$postfix);
22
		$this->_fileMode=$fileMode;
23
		if (!is_dir($root))
24
			\mkdir($root, $fileMode, true);
25
	}
26
27
	/**
28
	 * Check if annotation-data for the key has been stored.
29
	 * @param string $key cache key
30
	 * @return bool true if data with the given key has been stored; otherwise false
31
	 */
32
	public function exists($key) {
33
		return file_exists($this->_getPath($key));
34
	}
35
36
	/**
37
	 * Caches the given data with the given key.
38
	 * @param string $key cache key
39
	 * @param string $code the source-code to be cached
0 ignored issues
show
Bug introduced by
There is no parameter named $code. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
	 * @throws AnnotationException if file could not be written
41
	 */
42
	protected function storeContent($key,$content) {
43
		$path=$this->_getPath($key);
44
		if (@\file_put_contents($path, $content, LOCK_EX) === false) {
45
			throw new \Exception("Unable to write cache file: {$path}");
46
		}
47
		if (@\chmod($path, $this->_fileMode) === false) {
48
			throw new \Exception("Unable to set permissions of cache file: {$path}");
49
		}
50
	}
51
52
	/**
53
	 * Fetches data stored for the given key.
54
	 * @param string $key cache key
55
	 * @return mixed the cached data
56
	 */
57
	public function fetch($key) {
58
		return include ($this->_getPath($key));
59
	}
60
61
	/**
62
	 * return data stored for the given key.
63
	 * @param string $key cache key
64
	 * @return mixed the cached data
65
	 */
66
	public function file_get_contents($key) {
67
		return \file_get_contents($this->_getPath($key));
68
	}
69
70
	/**
71
	 * Returns the timestamp of the last cache update for the given key.
72
	 *
73
	 * @param string $key cache key
74
	 * @return int unix timestamp
75
	 */
76
	public function getTimestamp($key) {
77
		return \filemtime($this->_getPath($key));
78
	}
79
80
	/**
81
	 * Maps a cache-key to the absolute path of a PHP file
82
	 *
83
	 * @param string $key cache key
84
	 * @return string absolute path of the PHP file
85
	 */
86
	private function _getPath($key) {
87
		return $this->_root . DIRECTORY_SEPARATOR . $key . $this->postfix . '.php';
88
	}
89
90
	public function remove($key) {
91
		$file=$this->_getPath($key);
92
		if (\file_exists($file))
93
			\unlink($file);
94
	}
95
96
	public function clear() {
97
		$files=glob($this->_root . '/*');
98
		foreach ( $files as $file ) {
99
			if (\is_file($file))
100
				\unlink($file);
101
		}
102
	}
103
}
104