1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem; |
6
|
|
|
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
8
|
|
|
use SplFileObject; |
9
|
|
|
|
10
|
|
|
use function dirname; |
11
|
|
|
|
12
|
|
|
class File extends AbstractFilesystemItem |
13
|
|
|
{ |
14
|
|
|
protected const PATH_TYPE = 'file'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The permissions to be set on newly created files |
18
|
|
|
* |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $createMode = 0644; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
protected $contents; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Stores an instance fo the SplFileObject object for the file. |
30
|
|
|
* |
31
|
|
|
* @var SplFileObject |
32
|
|
|
*/ |
33
|
|
|
protected $splFileObject; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Directory |
37
|
|
|
*/ |
38
|
|
|
private $directory; |
39
|
|
|
|
40
|
|
|
public function removeIfExists(): self |
41
|
|
|
{ |
42
|
|
|
if ($this->exists()) { |
43
|
|
|
unlink($this->path); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $path |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
11 |
|
public function setPath(string $path): self |
55
|
|
|
{ |
56
|
11 |
|
parent::setPath($path); |
57
|
11 |
|
$this->directory = new Directory(dirname($path)); |
58
|
|
|
|
59
|
11 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
7 |
|
public function getContents(): ?string |
66
|
|
|
{ |
67
|
7 |
|
return $this->contents; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $contents |
72
|
|
|
* |
73
|
|
|
* @return File |
74
|
|
|
*/ |
75
|
1 |
|
public function setContents(string $contents): File |
76
|
|
|
{ |
77
|
1 |
|
$this->contents = $contents; |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return File |
84
|
|
|
* @throws DoctrineStaticMetaException |
85
|
|
|
*/ |
86
|
5 |
|
public function putContents(): self |
87
|
|
|
{ |
88
|
5 |
|
$this->assertExists(); |
89
|
1 |
|
\ts\file_put_contents($this->path, $this->contents); |
|
|
|
|
90
|
|
|
|
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return File |
96
|
|
|
* @throws DoctrineStaticMetaException |
97
|
|
|
*/ |
98
|
5 |
|
public function loadContents(): self |
99
|
|
|
{ |
100
|
5 |
|
$this->assertExists(); |
101
|
1 |
|
$this->contents = \ts\file_get_contents($this->path); |
102
|
|
|
|
103
|
1 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Provide an SplFileObject object, asserting that the path exists |
108
|
|
|
* |
109
|
|
|
* @return SplFileObject |
110
|
|
|
* @throws DoctrineStaticMetaException |
111
|
|
|
*/ |
112
|
6 |
|
public function getSplFileObject(): SplFileObject |
113
|
|
|
{ |
114
|
6 |
|
$this->assertExists(); |
115
|
2 |
|
if (null !== $this->splFileObject && $this->path === $this->splFileObject->getRealPath()) { |
116
|
1 |
|
return $this->splFileObject; |
117
|
|
|
} |
118
|
1 |
|
$this->splFileObject = new SplFileObject($this->path); |
119
|
|
|
|
120
|
1 |
|
return $this->splFileObject; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return Directory |
125
|
|
|
*/ |
126
|
1 |
|
public function getDirectory(): Directory |
127
|
|
|
{ |
128
|
1 |
|
return $this->directory; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* This is the specific creation logic for the concrete filesystem type. |
133
|
|
|
*/ |
134
|
7 |
|
protected function doCreate(): void |
135
|
|
|
{ |
136
|
7 |
|
$this->directory->assertExists(); |
137
|
4 |
|
\ts\file_put_contents($this->path, ''); |
138
|
4 |
|
} |
139
|
|
|
|
140
|
5 |
|
protected function isCorrectType(): bool |
141
|
|
|
{ |
142
|
5 |
|
return $this->createSplFileInfo()->isFile(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|