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
|
|
|
/** |
11
|
|
|
* A representation of a file on a given filesystem. |
12
|
|
|
* |
13
|
|
|
* This class extends \SplFileInfo and adds new methods along with overriding some methods solely because I feel that |
14
|
|
|
* some of the naming can be misleading. |
15
|
|
|
* |
16
|
|
|
* @since 0.2.0 |
17
|
|
|
*/ |
18
|
|
|
final class File extends \SplFileInfo |
19
|
|
|
{ |
20
|
|
|
private $relativeParentFolder; |
21
|
|
|
private $relativeFilePath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $filePath The file name or absolute file path. If just a file name is given, then it |
27
|
|
|
* will look for the file in the current working directory. |
28
|
|
|
* @param string $relativeParentFolder The relative path |
29
|
|
|
* @param string $relativeFilePath The relative path name |
30
|
|
|
* |
31
|
|
|
* @since 0.2.0 |
32
|
|
|
*/ |
33
|
170 |
|
public function __construct($filePath, $relativeParentFolder, $relativeFilePath) |
34
|
|
|
{ |
35
|
170 |
|
parent::__construct($filePath); |
36
|
|
|
|
37
|
170 |
|
$this->relativeParentFolder = $relativeParentFolder; |
38
|
170 |
|
$this->relativeFilePath = $relativeFilePath; |
39
|
170 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the name of the file without an extension. |
43
|
|
|
* |
44
|
|
|
* @param null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo |
45
|
|
|
* definition. |
46
|
|
|
* |
47
|
|
|
* @since 0.2.0 |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getBasename($suffix = null) |
52
|
|
|
{ |
53
|
|
|
return parent::getBasename('.' . $this->getExtension()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the name of the with the extension. |
58
|
|
|
* |
59
|
|
|
* @since 0.2.0 |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getFilename() |
64
|
|
|
{ |
65
|
|
|
return parent::getBasename(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the absolute path to this file. |
70
|
|
|
* |
71
|
|
|
* @since 0.2.0 |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getFilePath() |
76
|
|
|
{ |
77
|
|
|
return $this->getPathname(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the path to the parent folder of this file. |
82
|
|
|
* |
83
|
|
|
* @since 0.2.0 |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getParentFolder() |
88
|
|
|
{ |
89
|
|
|
return $this->getPath(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the file path to this file, relative to where it was created; likely the current working directory. |
94
|
|
|
* |
95
|
|
|
* @since 0.2.0 |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
41 |
|
public function getRelativeFilePath() |
100
|
|
|
{ |
101
|
41 |
|
return $this->relativeFilePath; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the path to the parent folder this file, relative to where it was created; likely the current working directory. |
106
|
|
|
* |
107
|
|
|
* @since 0.2.0 |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getRelativeParentFolder() |
112
|
|
|
{ |
113
|
|
|
return $this->relativeParentFolder; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the contents of this file. |
118
|
|
|
* |
119
|
|
|
* @since 0.2.0 |
120
|
|
|
* |
121
|
|
|
* @throws \RuntimeException When the file could not be read. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getContents() |
126
|
|
|
{ |
127
|
|
|
$content = file_get_contents($this->getFilePath()); |
128
|
|
|
|
129
|
|
|
if ($content === false) |
130
|
|
|
{ |
131
|
|
|
$error = error_get_last(); |
132
|
|
|
throw new \RuntimeException($error['message']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $content; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.