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