Skip to content

Commit c5d6211

Browse files
committed
file content in dir handler
1 parent 9a1df49 commit c5d6211

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

src/FileSystem/DirHandler.php

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,48 +42,6 @@ public function __construct(string $path) {
4242
$this->setPath($path);
4343
}
4444

45-
/**
46-
* @param string $fileName
47-
* @return null|FileHandler
48-
*/
49-
public function findFile(string $fileName): ?FileHandler {
50-
return $this->_findFile($this->path, $fileName);
51-
}
52-
53-
/**
54-
* finds a file in the given dir
55-
*
56-
* @param $dirName
57-
* @param $fileName
58-
* @return string
59-
*/
60-
private function _findFile(string $dirName, string $fileName): ?FileHandler {
61-
$dirs = glob($dirName . '*');
62-
$file = null;
63-
foreach ($dirs as $d) {
64-
if (is_file($d)) {
65-
$pathInfo = \pathinfo($d);
66-
$pathInfo2 = \pathinfo($fileName);
67-
68-
if (isset($pathInfo2["extension"])) {
69-
$condition = $pathInfo["basename"] === $pathInfo2["basename"];
70-
} else {
71-
$condition = $pathInfo["filename"] === $pathInfo2["filename"];
72-
}
73-
74-
if ($condition) {
75-
return new FileHandler($dirName . "/" . $pathInfo["basename"]);
76-
}
77-
} else if (is_dir($d)) {
78-
$tmp = $this->_findFile($d . "/", $fileName);
79-
if (null !== $tmp) {
80-
$file = $tmp;
81-
}
82-
}
83-
}
84-
return $file;
85-
}
86-
8745
/**
8846
* whether the dir is readable
8947
*
@@ -156,12 +114,30 @@ private function _list(string $path): array {
156114
}
157115

158116
/**
159-
* @return string|null
117+
* @param string $name
118+
* @param bool $override
119+
* @param string $content
120+
* @return bool
160121
*/
161-
public function toRealPath(): ?string {
162-
$realpath = \realpath($this->path);
163-
if (false === $realpath) return null;
164-
return $realpath;
122+
public function createFile(string $name, bool $override = false, string $content = null): bool {
123+
if (!$this->exists()) return false;
124+
if (!$override && $this->hasFile($name)) return true;
125+
$path = $this->toRealPath();
126+
$filePath = $path . $name;
127+
$touched = \touch($filePath, \time(), \time());
128+
if (null === $content) return $touched;
129+
if (false === $touched) return false;
130+
$handle = fopen($filePath, "w+");
131+
if (false === $handle) {
132+
$this->deleteFile($filePath);
133+
return false;
134+
}
135+
$written = \fwrite($handle,$content);
136+
if (false === $written){
137+
$this->deleteFile($written);
138+
return false;
139+
}
140+
return true;
165141
}
166142

167143
/**
@@ -174,15 +150,12 @@ public function exists(): bool {
174150
}
175151

176152
/**
177-
* @param string $name
178-
* @param bool $override
179-
* @return bool
153+
* @return string|null
180154
*/
181-
public function createFile(string $name, bool $override = false): bool {
182-
if (!$this->exists()) return false;
183-
if (!$override && $this->hasFile($name)) return true;
184-
$path = $this->toRealPath();
185-
return \touch($path . "/" . $name, \time(), \time());
155+
public function toRealPath(): ?string {
156+
$realpath = \realpath($this->path);
157+
if (false === $realpath) return null;
158+
return $realpath;
186159
}
187160

188161
/**
@@ -193,6 +166,48 @@ public function hasFile(string $name): bool {
193166
return null !== $this->findFile($name);
194167
}
195168

169+
/**
170+
* @param string $fileName
171+
* @return null|FileHandler
172+
*/
173+
public function findFile(string $fileName): ?FileHandler {
174+
return $this->_findFile($this->path, $fileName);
175+
}
176+
177+
/**
178+
* finds a file in the given dir
179+
*
180+
* @param $dirName
181+
* @param $fileName
182+
* @return string
183+
*/
184+
private function _findFile(string $dirName, string $fileName): ?FileHandler {
185+
$dirs = glob($dirName . '*');
186+
$file = null;
187+
foreach ($dirs as $d) {
188+
if (is_file($d)) {
189+
$pathInfo = \pathinfo($d);
190+
$pathInfo2 = \pathinfo($fileName);
191+
192+
if (isset($pathInfo2["extension"])) {
193+
$condition = $pathInfo["basename"] === $pathInfo2["basename"];
194+
} else {
195+
$condition = $pathInfo["filename"] === $pathInfo2["filename"];
196+
}
197+
198+
if ($condition) {
199+
return new FileHandler($dirName . "/" . $pathInfo["basename"]);
200+
}
201+
} else if (is_dir($d)) {
202+
$tmp = $this->_findFile($d . "/", $fileName);
203+
if (null !== $tmp) {
204+
$file = $tmp;
205+
}
206+
}
207+
}
208+
return $file;
209+
}
210+
196211
/**
197212
* @param string $name
198213
* @return bool

0 commit comments

Comments
 (0)