1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
6
|
|
|
|
7
|
|
|
abstract class AbstractFilesystemItem |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* This is the name of the path type. It should be overridden in child classes. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected const PATH_TYPE = 'override me'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is the path as configured for the filesystem item. It in no way means the item exists there, it is simply |
18
|
|
|
* where we expect it to exist if/when it has been created |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $path; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* What permissions should be set to created filesystem items? |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $createModeOctal = 0777; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* A string representation of the octal number |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $createModeOctalString = '0777'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Stores an instance fo the SplFileInfo object for the path item. |
40
|
|
|
* |
41
|
|
|
* @var \SplFileInfo |
42
|
|
|
*/ |
43
|
|
|
protected $splFileInfo; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $path - the path where we would expect the item to exist if/when it has been created |
47
|
|
|
*/ |
48
|
25 |
|
public function __construct(string $path = null) |
49
|
|
|
{ |
50
|
25 |
|
if (static::PATH_TYPE === self::PATH_TYPE) { |
|
|
|
|
51
|
|
|
throw new \RuntimeException('You must override the PATH_TYPE in your concrete class'); |
52
|
|
|
} |
53
|
25 |
|
if (null !== $path) { |
54
|
19 |
|
$this->setPath($path); |
55
|
|
|
} |
56
|
25 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create the filesystem item, asserting that it does not already exist |
60
|
|
|
* |
61
|
|
|
* @return static |
62
|
|
|
* @throws DoctrineStaticMetaException |
63
|
|
|
*/ |
64
|
15 |
|
public function create() |
65
|
|
|
{ |
66
|
15 |
|
$this->assertPathIsSet(); |
67
|
11 |
|
$this->assertNotExists(); |
68
|
9 |
|
$this->doCreate(); |
69
|
6 |
|
$this->setPermissions(); |
70
|
|
|
|
71
|
6 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws DoctrineStaticMetaException |
76
|
|
|
*/ |
77
|
45 |
|
protected function assertPathIsSet(): void |
78
|
|
|
{ |
79
|
45 |
|
if (null === $this->path) { |
80
|
18 |
|
throw new DoctrineStaticMetaException('$this->path is not set'); |
81
|
|
|
} |
82
|
27 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Throw an Exception if there is already something that exists at the path |
86
|
|
|
* |
87
|
|
|
* @throws DoctrineStaticMetaException |
88
|
|
|
*/ |
89
|
11 |
|
protected function assertNotExists(): void |
90
|
|
|
{ |
91
|
11 |
|
if (true === $this->exists()) { |
92
|
2 |
|
throw new DoctrineStaticMetaException(static::PATH_TYPE . ' already exists at path ' . $this->path); |
93
|
|
|
} |
94
|
9 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check if something exists at the real path |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
34 |
|
public function exists(): bool |
102
|
|
|
{ |
103
|
34 |
|
$realPath = \realpath($this->path); |
104
|
34 |
|
if (false === $realPath) { |
105
|
27 |
|
return false; |
106
|
|
|
} |
107
|
12 |
|
$this->path = $realPath; |
108
|
12 |
|
$this->assertCorrectType(); |
109
|
|
|
|
110
|
10 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check that the path is actually a file/directory etc |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
12 |
|
protected function assertCorrectType(): void |
119
|
|
|
{ |
120
|
12 |
|
if (false === $this->isCorrectType()) { |
121
|
2 |
|
throw new \RuntimeException('path is not the correct type: ' . $this->path); |
122
|
|
|
} |
123
|
10 |
|
} |
124
|
|
|
|
125
|
|
|
abstract protected function isCorrectType(): bool; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* This is the specific creation logic for the concrete filesystem type. |
129
|
|
|
*/ |
130
|
|
|
abstract protected function doCreate(): void; |
131
|
|
|
|
132
|
6 |
|
private function setPermissions(): void |
133
|
|
|
{ |
134
|
6 |
|
chmod($this->path, $this->createModeOctal); |
135
|
6 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the path to the filesystem item as it is currently set in this object |
139
|
|
|
* |
140
|
|
|
* @return null|string |
141
|
|
|
*/ |
142
|
15 |
|
public function getPath(): ?string |
143
|
|
|
{ |
144
|
15 |
|
return $this->path; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the string path to the filesystem item |
149
|
|
|
* |
150
|
|
|
* @param string $path |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
21 |
|
public function setPath(string $path) |
155
|
|
|
{ |
156
|
21 |
|
$this->path = $path; |
157
|
|
|
|
158
|
21 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Provide an SplFileInfo object, asserting that the path exists |
163
|
|
|
* |
164
|
|
|
* @return \SplFileInfo |
165
|
|
|
* @throws DoctrineStaticMetaException |
166
|
|
|
*/ |
167
|
20 |
|
public function getSplFileInfo(): \SplFileInfo |
168
|
|
|
{ |
169
|
20 |
|
if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) { |
170
|
3 |
|
return $this->splFileInfo; |
171
|
|
|
} |
172
|
17 |
|
$this->assertExists(); |
173
|
|
|
|
174
|
1 |
|
return $this->createSplFileInfo(); |
175
|
|
|
} |
176
|
|
|
|
177
|
38 |
|
protected function assertExists(): void |
178
|
|
|
{ |
179
|
38 |
|
$this->assertPathIsSet(); |
180
|
24 |
|
if (false === $this->exists()) { |
181
|
17 |
|
throw new DoctrineStaticMetaException(static::PATH_TYPE . ' does not exist at path ' . $this->path); |
182
|
|
|
} |
183
|
7 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Create an SplFileInfo, assuming the path already exists |
187
|
|
|
* |
188
|
|
|
* @return \SplFileInfo |
189
|
|
|
*/ |
190
|
12 |
|
protected function createSplFileInfo(): \SplFileInfo |
191
|
|
|
{ |
192
|
12 |
|
if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) { |
193
|
2 |
|
return $this->splFileInfo; |
194
|
|
|
} |
195
|
11 |
|
$this->splFileInfo = new \SplFileInfo($this->path); |
196
|
|
|
|
197
|
11 |
|
return $this->splFileInfo; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param int $createMode |
202
|
|
|
* |
203
|
|
|
* @return static |
204
|
|
|
*/ |
205
|
2 |
|
public function setCreateMode(int $createMode): self |
206
|
|
|
{ |
207
|
2 |
|
$this->createModeOctal = $createMode; |
208
|
2 |
|
$this->createModeOctalString = decoct($createMode); |
209
|
|
|
|
210
|
2 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|