Skip to content

Commit 54fbda3

Browse files
committed
Implement filesystem interface
1 parent 38669ce commit 54fbda3

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the aether/aether.
5+
*
6+
* Copyright (C) 2024 Dominik Szamburski
7+
*
8+
* This software may be modified and distributed under the terms
9+
* of the MIT license. See the LICENSE file for details.
10+
*/
11+
12+
namespace Aether\Filesystem;
13+
14+
class Filesystem implements FilesystemInterface {
15+
16+
public function exists(string $path): bool
17+
{
18+
if (\strlen($path) > \PHP_MAXPATHLEN - 2) {
19+
throw new IOException("Could not check if file exists because path exceeds " . (\PHP_MAXPATHLEN - 2) . " characters.");
20+
}
21+
22+
return \file_exists($path);
23+
}
24+
25+
public function read(string $path): string
26+
{
27+
if (!$this->exists($path) && !$this->isFile($path)) {
28+
throw new IOException("File $path does not exists.");
29+
}
30+
31+
return \file_get_contents($path);
32+
}
33+
34+
public function write(string $path, string $contents, bool $lock = false): void
35+
{
36+
if (\file_put_contents($path, $contents, $lock ? \LOCK_EX : 0) === false) {
37+
throw new IOException("Failed to save file $path.");
38+
}
39+
}
40+
41+
public function copy(string $source, string $destination): void
42+
{
43+
if (\copy($source, $destination) === false) {
44+
throw new IOException("Filed to copy '".$source."' to '".$destination."'.");
45+
}
46+
}
47+
48+
public function move(string $source, string $destination): void
49+
{
50+
if (\rename($source, $destination) === false) {
51+
throw new IOException("Failed to rename '".$source."' to '".$destination."'.");
52+
}
53+
}
54+
55+
public function append(string $path, string $contents): bool
56+
{
57+
return \file_put_contents($path, $contents, \FILE_APPEND) !== false;
58+
}
59+
60+
public function touch(string $filename, ?int $mtime = null, ?int $atime = null): void
61+
{
62+
if (\touch($filename, $mtime, $atime) === false) {
63+
throw new IOException("Failed to touch '$filename'.");
64+
}
65+
}
66+
67+
public function unlink(string|array $filename): void
68+
{
69+
$paths = \is_array($filename) ? $filename : [$filename];
70+
71+
foreach ($paths as $path) {
72+
if (\unlink($path) === false) {
73+
throw new IOException("Failed to unlink '". $path ."'.");
74+
}
75+
}
76+
}
77+
78+
public function mkdir(string $directory, int $mode = 0777, bool $recursive = false): void
79+
{
80+
if (\mkdir($directory, $mode, $recursive) === false) {
81+
throw new IOException("Failed to create a directory '".$directory."'.");
82+
}
83+
}
84+
public function rmdir(string $directory): void {
85+
if (!$this->isDirectory($directory)) {
86+
throw new IOException("Directory $directory does not exists.");
87+
}
88+
89+
$items = new \FilesystemIterator($directory);
90+
foreach ($items as $item) {
91+
$item->isDir() && !$item->isLink()
92+
? $this->rmdir($item->getPathname())
93+
: $this->unlink($item->getPathname());
94+
}
95+
96+
unset($items);
97+
@rmdir($directory);
98+
}
99+
100+
public function name(string $path): string
101+
{
102+
return \pathinfo($path, \PATHINFO_FILENAME);
103+
}
104+
105+
public function basename(string $path): string
106+
{
107+
return \pathinfo($path, \PATHINFO_BASENAME);
108+
}
109+
110+
public function dirname(string $path): string
111+
{
112+
return \pathinfo($path, \PATHINFO_DIRNAME);
113+
}
114+
115+
public function extension(string $path): string
116+
{
117+
return \pathinfo($path, \PATHINFO_EXTENSION);
118+
}
119+
120+
public function filesize(string $path): int
121+
{
122+
if (!$this->exists($path)) {
123+
throw new IOException("File $path does not exists.");
124+
}
125+
126+
return \filesize($path);
127+
}
128+
129+
130+
public function isDirectory(string $path): bool
131+
{
132+
return \is_dir($path);
133+
}
134+
135+
public function isFile(string $path): bool
136+
{
137+
return \is_file($path);
138+
}
139+
140+
public function mimeType(string $path): string
141+
{
142+
if (!$this->exists($path)) {
143+
throw new IOException("File not found $path");
144+
}
145+
146+
return \mime_content_type($path);
147+
}
148+
}

0 commit comments

Comments
 (0)