|
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
|
7 |
|
public function __construct(string $path = null) |
|
49
|
|
|
{ |
|
50
|
7 |
|
if (static::PATH_TYPE === self::PATH_TYPE) { |
|
|
|
|
|
|
51
|
|
|
throw new \RuntimeException('You must override the PATH_TYPE in your concrete class'); |
|
52
|
|
|
} |
|
53
|
7 |
|
if (null !== $path) { |
|
54
|
4 |
|
$this->setPath($path); |
|
55
|
|
|
} |
|
56
|
7 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create the filesystem item, asserting that it does not already exist |
|
60
|
|
|
* |
|
61
|
|
|
* @return static |
|
62
|
|
|
* @throws DoctrineStaticMetaException |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function create() |
|
65
|
|
|
{ |
|
66
|
3 |
|
$this->assertPathIsSet(); |
|
67
|
1 |
|
$this->assertNotExists(); |
|
68
|
1 |
|
$this->doCreate(); |
|
69
|
|
|
$this->setPermissions(); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @throws DoctrineStaticMetaException |
|
76
|
|
|
*/ |
|
77
|
17 |
|
protected function assertPathIsSet(): void |
|
78
|
|
|
{ |
|
79
|
17 |
|
if (null === $this->path) { |
|
80
|
9 |
|
throw new DoctrineStaticMetaException('$this->path is not set'); |
|
81
|
|
|
} |
|
82
|
8 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Throw an Exception if there is already something that exists at the path |
|
86
|
|
|
* |
|
87
|
|
|
* @throws DoctrineStaticMetaException |
|
88
|
|
|
*/ |
|
89
|
1 |
|
protected function assertNotExists(): void |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (true === $this->exists()) { |
|
92
|
|
|
throw new DoctrineStaticMetaException(static::PATH_TYPE . ' already exists at path ' . $this->path); |
|
93
|
|
|
} |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Check if something exists at the real path |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
11 |
|
public function exists(): bool |
|
102
|
|
|
{ |
|
103
|
11 |
|
$realPath = \realpath($this->path); |
|
104
|
11 |
|
if (false === $realPath) { |
|
105
|
10 |
|
return false; |
|
106
|
|
|
} |
|
107
|
1 |
|
$this->path = $realPath; |
|
108
|
1 |
|
$this->assertCorrectType(); |
|
109
|
|
|
|
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check that the path is actually a file/directory etc |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
1 |
|
protected function assertCorrectType(): void |
|
119
|
|
|
{ |
|
120
|
1 |
|
if (false === $this->isCorrectType()) { |
|
121
|
1 |
|
throw new \RuntimeException('path is not the correct type: ' . $this->path); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
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
|
|
|
private function setPermissions(): void |
|
133
|
|
|
{ |
|
134
|
|
|
chmod($this->path, $this->createModeOctal); |
|
135
|
|
|
} |
|
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
|
6 |
|
public function getPath(): ?string |
|
143
|
|
|
{ |
|
144
|
6 |
|
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
|
5 |
|
public function setPath(string $path) |
|
155
|
|
|
{ |
|
156
|
5 |
|
$this->path = $path; |
|
157
|
|
|
|
|
158
|
5 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Provide an SplFileInfo object, asserting that the path exists |
|
163
|
|
|
* |
|
164
|
|
|
* @return \SplFileInfo |
|
165
|
|
|
* @throws DoctrineStaticMetaException |
|
166
|
|
|
*/ |
|
167
|
8 |
|
public function getSplFileInfo(): \SplFileInfo |
|
168
|
|
|
{ |
|
169
|
8 |
|
if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) { |
|
170
|
|
|
return $this->splFileInfo; |
|
171
|
|
|
} |
|
172
|
8 |
|
$this->assertExists(); |
|
173
|
|
|
|
|
174
|
|
|
return $this->createSplFileInfo(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
15 |
|
protected function assertExists(): void |
|
178
|
|
|
{ |
|
179
|
15 |
|
$this->assertPathIsSet(); |
|
180
|
8 |
|
if (false === $this->exists()) { |
|
181
|
8 |
|
throw new DoctrineStaticMetaException(static::PATH_TYPE . ' does not exist at path ' . $this->path); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Create an SplFileInfo, assuming the path already exists |
|
187
|
|
|
* |
|
188
|
|
|
* @return \SplFileInfo |
|
189
|
|
|
*/ |
|
190
|
1 |
|
protected function createSplFileInfo(): \SplFileInfo |
|
191
|
|
|
{ |
|
192
|
1 |
|
if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) { |
|
193
|
|
|
return $this->splFileInfo; |
|
194
|
|
|
} |
|
195
|
1 |
|
$this->splFileInfo = new \SplFileInfo($this->path); |
|
196
|
|
|
|
|
197
|
1 |
|
return $this->splFileInfo; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param int $createMode |
|
202
|
|
|
* |
|
203
|
|
|
* @return static |
|
204
|
|
|
*/ |
|
205
|
|
|
public function setCreateMode(int $createMode): self |
|
206
|
|
|
{ |
|
207
|
|
|
$this->createModeOctal = $createMode; |
|
208
|
|
|
$this->createModeOctalString = decoct($createMode); |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|