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
|
|
|
* Get the name of the file without an extension. |
41
|
|
|
* |
42
|
|
|
* @param null $suffix This value will be discarded and is only needed to be able to override the \SplFileInfo |
43
|
|
|
* definition. |
44
|
|
|
* |
45
|
|
|
* @since 0.2.0 |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
5 |
|
public function getBasename($suffix = null) |
50
|
|
|
{ |
51
|
5 |
|
return parent::getBasename('.' . $this->getExtension()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the name of the with the extension. |
56
|
|
|
* |
57
|
|
|
* @since 0.2.0 |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function getFilename() |
62
|
|
|
{ |
63
|
1 |
|
return parent::getBasename(); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the absolute path to this file. |
68
|
|
|
* |
69
|
|
|
* @since 0.2.0 |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
129 |
|
public function getAbsolutePath() |
74
|
|
|
{ |
75
|
129 |
|
return $this->getPathname(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the path to the parent folder of this file. |
80
|
|
|
* |
81
|
|
|
* @since 0.2.0 |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getParentFolder() |
86
|
|
|
{ |
87
|
1 |
|
return $this->getPath(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the file path to this file, relative to where it was created; likely the current working directory. |
92
|
|
|
* |
93
|
|
|
* @since 0.2.0 |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
90 |
|
public function getRelativeFilePath() |
98
|
|
|
{ |
99
|
90 |
|
$path = str_replace($this->relativePath, '', $this->getAbsolutePath()); |
100
|
|
|
|
101
|
90 |
|
return ltrim($path, "/\\"); |
|
|
|
|
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
|
1 |
|
public function getRelativeParentFolder() |
112
|
|
|
{ |
113
|
1 |
|
return dirname($this->getRelativeFilePath()); |
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
|
126 |
|
public function getContents() |
126
|
|
|
{ |
127
|
126 |
|
if (!file_exists($this->getAbsolutePath())) |
128
|
126 |
|
{ |
129
|
1 |
|
throw new FileNotFoundException(null, 0, null, $this->getAbsolutePath()); |
130
|
|
|
} |
131
|
|
|
|
132
|
126 |
|
$content = file_get_contents($this->getAbsolutePath()); |
133
|
|
|
|
134
|
126 |
|
if ($content === false) |
135
|
126 |
|
{ |
136
|
|
|
$error = error_get_last(); |
137
|
|
|
throw new \RuntimeException($error['message']); |
138
|
|
|
} |
139
|
|
|
|
140
|
126 |
|
return $content; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.