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