1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package domain |
4
|
|
|
* @subpackage access |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 12.08.07 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\domain\access; |
9
|
|
|
|
10
|
|
|
use vsc\infrastructure\Object; |
11
|
|
|
use vsc\ExceptionError; |
12
|
|
|
|
13
|
|
|
class FileAccess extends Object { |
14
|
|
|
private $sUri; |
15
|
|
|
|
16
|
|
|
private $sCachePath; |
17
|
|
|
protected $saveToCache = true; |
18
|
|
|
|
19
|
1 |
|
public function __construct($sUri) { |
20
|
1 |
|
$this->sUri = $sUri; |
21
|
1 |
|
} |
22
|
|
|
|
23
|
1 |
|
public function getUri() { |
|
|
|
|
24
|
1 |
|
return $this->sUri; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function getCachePath() { |
|
|
|
|
28
|
1 |
|
return $this->sCachePath; |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function setCachePath($sPath) { |
32
|
1 |
|
if (is_dir($sPath)) { |
33
|
1 |
|
$this->sCachePath = $sPath; |
34
|
|
|
} else { |
35
|
|
|
throw new ExceptionAccess('Path [' . $sPath . '] is invalid for cache'); |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $sFile |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
1 |
|
public function getLocalPath($sFile) { |
44
|
1 |
|
return $this->sCachePath . DIRECTORY_SEPARATOR . $sFile; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function getSignature($sUri) { |
48
|
1 |
|
return md5($sUri . date('Ymd')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $sUri |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
1 |
|
public function inCache($sUri) { |
56
|
1 |
|
return (is_file($this->getLocalPath($this->getSignature($sUri)))); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function loadFromCache($sUri) { |
60
|
1 |
|
return file_get_contents($this->getLocalPath($this->getSignature($sUri))); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $sUri |
65
|
|
|
* @param string $sContent |
66
|
|
|
* @throws ExceptionError |
67
|
|
|
*/ |
68
|
1 |
|
public function cacheFile($sUri, $sContent) { |
69
|
1 |
|
$sFileName = $this->getLocalPath($this->getSignature($sUri)); |
70
|
|
|
// creating the file |
71
|
1 |
|
if (!touch($sFileName)) { |
72
|
|
|
throw new ExceptionError('Path [' . $sFileName . '] is not accessible.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if (is_writable($sFileName)) { |
76
|
1 |
|
file_put_contents($sFileName, $sContent); |
77
|
|
|
} else { |
78
|
|
|
throw new ExceptionError('Path [' . $sFileName . '] is not writable.'); |
79
|
|
|
} |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
public function isLocalFile() { |
83
|
1 |
|
return is_file($this->sUri); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public static function getFile($sPath) { |
87
|
1 |
|
return file_get_contents($sPath); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function load() { |
91
|
2 |
|
if ($this->isLocalFile($this->sUri) || !$this->inCache($this->sUri)) { |
|
|
|
|
92
|
|
|
// @todo: use curl when file_get_contents doesn't work with urls |
93
|
2 |
|
$sContent = $this->getFile($this->sUri); |
94
|
|
|
|
95
|
|
|
try { |
96
|
2 |
|
if (!$this->isLocalFile($this->sUri) && $this->saveToCache) { |
|
|
|
|
97
|
2 |
|
$this->cacheFile($this->sUri, $sContent); |
98
|
|
|
} |
99
|
|
|
} catch (ExceptionAccess $e) { |
100
|
|
|
// no cache dir |
101
|
|
|
} catch (ExceptionError $e) { |
102
|
|
|
//_e ($e->getTraceAsString()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return $sContent; |
106
|
|
|
} else { |
107
|
|
|
return $this->loadFromCache($this->sUri); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.