1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class File |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Root path |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $root; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Container |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $container = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param string $root |
27
|
|
|
*/ |
28
|
|
|
public function __construct($root) |
29
|
|
|
{ |
30
|
|
|
$this->root = $root; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Root |
35
|
|
|
* |
36
|
|
|
* @return $root |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function root() |
39
|
|
|
{ |
40
|
|
|
return $this->root; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add the given path file to the container |
45
|
|
|
* |
46
|
|
|
* @param string $file |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
private function share($key, $value) |
50
|
|
|
{ |
51
|
|
|
$this->container[$key] = $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if the given path file exists in the container |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
private function isSharing($key) |
61
|
|
|
{ |
62
|
|
|
return isset($this->container[$key]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Determine if the given file path exists |
67
|
|
|
* |
68
|
|
|
* @param string $file |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function exists($file) |
72
|
|
|
{ |
73
|
|
|
return file_exists($file); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Require the given file |
78
|
|
|
* |
79
|
|
|
* @param string $path |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function call($path) |
83
|
|
|
{ |
84
|
|
|
$path = $this->fullPath($path); |
85
|
|
|
|
86
|
|
|
if (!$this->isSharing($path . ':file')) { |
87
|
|
|
|
88
|
|
|
if ($this->exists($path)) { |
89
|
|
|
|
90
|
|
|
$this->share($path . ':file', require $path); |
91
|
|
|
|
92
|
|
|
} else { |
93
|
|
|
|
94
|
|
|
throw new Exception("$path is not found"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->container[$path . ':file']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get file content |
103
|
|
|
* |
104
|
|
|
* @param string $path |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function fileContent($path) |
108
|
|
|
{ |
109
|
|
|
$path = $this->fullPath($path); |
110
|
|
|
|
111
|
|
|
if (!$this->isSharing($path . ':content')) { |
112
|
|
|
|
113
|
|
|
if ($this->exists($path)) { |
114
|
|
|
|
115
|
|
|
$this->share($path . ':content', file_get_contents($path)); |
116
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
|
119
|
|
|
throw new Exception("$path is not found"); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->container[$path . ':content']; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Generate full path to the given path |
128
|
|
|
* |
129
|
|
|
* @param string $path |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function fullPath($path) |
133
|
|
|
{ |
134
|
|
|
return $this->root . DS . str_replace(['/', '\\'], DS, $path); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|