1: <?php
2: namespace Contrib\Component\File;
3:
4: /**
5: * Path utils.
6: *
7: * @author Kitamura Satoshi <with.no.parachute@gmail.com>
8: */
9: class Path
10: {
11: /**
12: * Return whether the path is relative path.
13: *
14: * @param string $path Path.
15: * @return boolean true if the path is relative path, false otherwise.
16: */
17: public function isRelativePath($path)
18: {
19: return strlen($path) === 0 || strpos($path, DIRECTORY_SEPARATOR) !== 0;
20: }
21:
22: /**
23: * Cat file path.
24: *
25: * @param string $path File path.
26: * @param string $rootDir Absolute path to project root directory.
27: * @return string|false Absolute path.
28: */
29: public function toAbsolutePath($path, $rootDir)
30: {
31: if (!is_string($path)) {
32: return false;
33: }
34:
35: if ($this->isRelativePath($path)) {
36: return $rootDir . DIRECTORY_SEPARATOR . $path;
37: }
38:
39: return $path;
40: }
41:
42: /**
43: * Return real file path.
44: *
45: * @param string $path File path.
46: * @param string $rootDir Absolute path to project root directory.
47: * @return string|false Real path string if the path string is passed and real path exists, false otherwise.
48: */
49: public function getRealPath($path, $rootDir)
50: {
51: if (!is_string($path)) {
52: return false;
53: }
54:
55: if ($this->isRelativePath($path)) {
56: return realpath($rootDir . DIRECTORY_SEPARATOR . $path);
57: }
58:
59: return realpath($path);
60: }
61:
62: /**
63: * Return real directory path.
64: *
65: * @param string $path Path.
66: * @param string $rootDir Absolute path to project root directory.
67: * @return string|false Real directory path string if the path string is passed and real directory exists, false otherwise.
68: */
69: public function getRealDir($path, $rootDir)
70: {
71: if (!is_string($path)) {
72: return false;
73: }
74:
75: if ($this->isRelativePath($path)) {
76: return realpath($rootDir . DIRECTORY_SEPARATOR . dirname($path));
77: }
78:
79: return realpath(dirname($path));
80: }
81:
82: /**
83: * Return real file path to write.
84: *
85: * @param string $path File path.
86: * @param string $rootDir Absolute path to project root directory.
87: * @return string|false Real file path string if the parent directory exists, false otherwise.
88: */
89: public function getRealWritingFilePath($path, $rootDir)
90: {
91: $realDir = $this->getRealDir($path, $rootDir);
92:
93: if (!is_string($realDir)) {
94: return false;
95: }
96:
97: return $realDir . DIRECTORY_SEPARATOR . basename($path);
98: }
99:
100: /**
101: * Return whether the real path exists.
102: *
103: * @param string|boolean $realpath Real path.
104: * @return boolean true if the real path exists, false otherwise.
105: */
106: public function isRealPathExist($realpath)
107: {
108: return $realpath !== false && file_exists($realpath);
109: }
110:
111: /**
112: * Return whether the real file path exists.
113: *
114: * @param string|boolean $realpath Real file path.
115: * @return boolean true if the real file path exists, false otherwise.
116: */
117: public function isRealFileExist($realpath)
118: {
119: return $this->isRealPathExist($realpath) && is_file($realpath);
120: }
121:
122: /**
123: * Return whether the real file path is readable.
124: *
125: * @param string|boolean $realpath Real file path.
126: * @return boolean true if the real file path is readable, false otherwise.
127: */
128: public function isRealFileReadable($realpath)
129: {
130: return $this->isRealFileExist($realpath) && is_readable($realpath);
131: }
132:
133: /**
134: * Return whether the real file path is writable.
135: *
136: * @param string|boolean $realpath Real file path.
137: * @return boolean true if the real file path is writable, false otherwise.
138: */
139: public function isRealFileWritable($realpath)
140: {
141: return $this->isRealFileExist($realpath) && is_writable($realpath);
142: }
143:
144: /**
145: * Return whether the real directory exists.
146: *
147: * @param string|boolean $realpath Real directory path.
148: * @return boolean true if the real directory exists, false otherwise.
149: */
150: public function isRealDirExist($realpath)
151: {
152: return $this->isRealPathExist($realpath) && is_dir($realpath);
153: }
154:
155: /**
156: * Return whether the real directory is writable.
157: *
158: * @param string|boolean $realpath Real directory path.
159: * @return boolean true if the real directory is writable, false otherwise.
160: */
161: public function isRealDirWritable($realpath)
162: {
163: return $this->isRealDirExist($realpath) && is_writable($realpath);
164: }
165: }
166: