1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Filesystem; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A representation of a file on a given filesystem. |
14
|
|
|
* |
15
|
|
|
* This class extends \SplFileInfo and adds new methods along with overriding some methods solely because I feel that |
16
|
|
|
* some of the naming can be misleading. |
17
|
|
|
* |
18
|
|
|
* @since 0.2.0 |
19
|
|
|
*/ |
20
|
|
|
final class File extends \SplFileInfo |
21
|
|
|
{ |
22
|
|
|
private $relativePath; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* File Constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $absoluteFilePath The absolute file path |
28
|
|
|
* @param string|null $relativePath The relative path to its parent folder with respect to the CWD |
29
|
|
|
* |
30
|
|
|
* @since 0.2.0 |
31
|
|
|
*/ |
32
|
156 |
|
public function __construct($absoluteFilePath, $relativePath = null) |
33
|
|
|
{ |
34
|
156 |
|
$this->relativePath = ($relativePath === null) ? getcwd() : $relativePath; |
35
|
|
|
|
36
|
156 |
|
parent::__construct($absoluteFilePath); |
37
|
156 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Whether or not this file exists on the filesystem. |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
126 |
|
public function exists() |
45
|
|
|
{ |
46
|
126 |
|
return file_exists($this->getAbsolutePath()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the name of the file without an extension. |
51
|
|
|
* |
52
|
|
|
* @param null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo |
53
|
|
|
* definition. |
54
|
|
|
* |
55
|
|
|
* @since 0.2.0 |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
5 |
|
public function getBasename($suffix = null) |
60
|
|
|
{ |
61
|
5 |
|
return parent::getBasename('.' . $this->getExtension()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the name of the with the extension. |
66
|
|
|
* |
67
|
|
|
* @since 0.2.0 |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
public function getFilename() |
72
|
|
|
{ |
73
|
1 |
|
return parent::getBasename(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the absolute path to this file. |
78
|
|
|
* |
79
|
|
|
* @since 0.2.0 |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
130 |
|
public function getAbsolutePath() |
84
|
|
|
{ |
85
|
130 |
|
return $this->getPathname(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the path to the parent folder of this file. |
90
|
|
|
* |
91
|
|
|
* @since 0.2.0 |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
1 |
|
public function getParentFolder() |
96
|
|
|
{ |
97
|
1 |
|
return $this->getPath(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the file path to this file, relative to where it was created; likely the current working directory. |
102
|
|
|
* |
103
|
|
|
* @since 0.2.0 |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
107 |
|
public function getRelativeFilePath() |
108
|
|
|
{ |
109
|
107 |
|
$path = str_replace($this->relativePath, '', $this->getAbsolutePath()); |
110
|
|
|
|
111
|
107 |
|
return ltrim($path, "/\\"); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the path to the parent folder this file, relative to where it was created; likely the current working directory. |
116
|
|
|
* |
117
|
|
|
* @since 0.2.0 |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
1 |
|
public function getRelativeParentFolder() |
122
|
|
|
{ |
123
|
1 |
|
return dirname($this->getRelativeFilePath()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the contents of this file. |
128
|
|
|
* |
129
|
|
|
* @since 0.2.0 |
130
|
|
|
* |
131
|
|
|
* @throws \RuntimeException When the file could not be read. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
126 |
|
public function getContents() |
136
|
|
|
{ |
137
|
126 |
|
if (!$this->exists()) |
138
|
|
|
{ |
139
|
|
|
throw new FileNotFoundException(null, 0, null, $this->getAbsolutePath()); |
140
|
|
|
} |
141
|
|
|
|
142
|
126 |
|
$content = file_get_contents($this->getAbsolutePath()); |
143
|
|
|
|
144
|
126 |
|
if ($content === false) |
145
|
|
|
{ |
146
|
|
|
$error = error_get_last(); |
147
|
|
|
throw new \RuntimeException($error['message']); |
148
|
|
|
} |
149
|
|
|
|
150
|
126 |
|
return $content; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.