Skip to content

Commit 7829188

Browse files
committed
DirHandler, FileHandler improvements
1 parent 97ff916 commit 7829188

File tree

2 files changed

+84
-26
lines changed

2 files changed

+84
-26
lines changed

src/FileSystem/DirHandler.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @package doganoo\PHPUtil\FileSystem
3232
*/
3333
class DirHandler {
34+
public const DEFAULT_PERMISSION_MODE = 0770;
3435
private $path = null;
3536

3637
/**
@@ -41,20 +42,6 @@ public function __construct(string $path) {
4142
$this->setPath($path);
4243
}
4344

44-
/**
45-
* @return string
46-
*/
47-
public function getPath(): string {
48-
return $this->path;
49-
}
50-
51-
/**
52-
* @param string $path
53-
*/
54-
public function setPath(string $path) {
55-
$this->path = $path;
56-
}
57-
5845
/**
5946
* @param string $fileName
6047
* @return null|FileHandler
@@ -106,6 +93,29 @@ public function isReadable(): bool {
10693
return \is_dir($this->path) && \is_readable($this->path);
10794
}
10895

96+
/**
97+
* @param int $mode
98+
* @param bool $recursive
99+
* @return bool
100+
*/
101+
public function mkdir(int $mode = DirHandler::DEFAULT_PERMISSION_MODE, bool $recursive = true): bool {
102+
return \mkdir($this->getPath(), $mode, $recursive);
103+
}
104+
105+
/**
106+
* @return string
107+
*/
108+
public function getPath(): string {
109+
return $this->path;
110+
}
111+
112+
/**
113+
* @param string $path
114+
*/
115+
public function setPath(string $path) {
116+
$this->path = $path;
117+
}
118+
109119
/**
110120
* whether the dir is writable
111121
*

src/FileSystem/FileHandler.php

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,45 @@ class FileHandler {
3434
/** @var string $path */
3535
private $path = null;
3636

37+
/** @var null|string $content */
38+
private $content = null;
39+
3740
/**
3841
* FileHandler constructor.
3942
*
4043
* @param $path
4144
*/
42-
public function __construct($path) {
45+
public function __construct(string $path) {
46+
$this->setPath($path);
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getPath(): string {
53+
return $this->path;
54+
}
55+
56+
/**
57+
* @param string $path
58+
*/
59+
public function setPath(string $path): void {
4360
$this->path = $path;
61+
$this->content = null;
4462
}
4563

4664
/**
4765
* creates a file
4866
*
67+
* @param bool $override whether the file should be overriden
4968
* @return bool
5069
*/
51-
public function create(): bool {
52-
if ($this->isFile()) {
53-
return \touch($this->path);
54-
}
55-
return false;
70+
public function create(bool $override): bool {
71+
if ($this->isFile() && !$override) return false;
72+
$handle = @fopen($this->path, 'w');
73+
if (false === $handle) return false;
74+
fclose($handle);
75+
return true;
5676
}
5777

5878
/**
@@ -64,10 +84,29 @@ public function isFile(): bool {
6484
return \is_file($this->path);
6585
}
6686

87+
/**
88+
* checks whether the file is readable or not
89+
*
90+
* @return bool
91+
*/
92+
public function isReadable(): bool {
93+
return \is_readable($this->path);
94+
}
95+
96+
/**
97+
* alias method of isFile()
98+
*
99+
* @return bool
100+
*/
101+
public function exists(): bool {
102+
return $this->isFile();
103+
}
104+
67105
/**
68106
* forces a file creation
69107
*
70108
* @return bool
109+
* @deprecated Use create() instead
71110
*/
72111
public function forceCreate(): bool {
73112
if (!$this->isFile()) {
@@ -95,21 +134,30 @@ public function isWritable(): bool {
95134
* @return null|string
96135
*/
97136
public function getContent(): ?string {
98-
if ($this->isFile()) {
99-
return \file_get_contents($this->path);
100-
}
101-
return null;
137+
if (null !== $this->content) return $this->content;
138+
if (!$this->isFile()) return null;
139+
$content = \file_get_contents($this->path);
140+
if (false === $content) return null;
141+
return $content;
102142
}
103143

104144
/**
105145
* sets the content
106146
*
107147
* @param string $content
148+
* @return void
149+
*/
150+
public function setContent(string $content): void {
151+
$this->content = $content;
152+
}
153+
154+
/**
108155
* @return bool
109156
*/
110-
public function setContent(string $content): bool {
157+
public function save(): bool {
111158
if (!$this->isFile()) return false;
112-
return false !== \file_put_contents($this->path, $content);
159+
if (!$this->isWritable()) return false;
160+
return false !== \file_put_contents($this->path, $this->content);
113161
}
114162

115163
}

0 commit comments

Comments
 (0)