Skip to content

Commit 0d9173f

Browse files
authored
Merge pull request #3 from sxbrsky/implement-filesystem-package
2 parents 0861efc + 56fbd09 commit 0d9173f

6 files changed

Lines changed: 525 additions & 1 deletion

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"require": {
1111
"php": "^8.2",
12+
"ext-fileinfo": "*",
1213
"psr/cache": "^3.0",
1314
"psr/clock": "^1.0",
1415
"psr/container": "^2.0.x-dev",

packages/Filesystem/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"source": "https://github.com/sxbrsky/aether"
99
},
1010
"require": {
11-
"php": "^8.2"
11+
"php": "^8.2",
12+
"ext-fileinfo": "*"
1213
},
1314
"minimum-stability": "dev",
1415
"autoload": {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
85+
public function rmdir(string $directory): void
86+
{
87+
if (! $this->isDirectory($directory)) {
88+
throw new IOException("Directory $directory does not exists.");
89+
}
90+
91+
$items = new \FilesystemIterator($directory);
92+
foreach ($items as $item) {
93+
$item->isDir() && ! $item->isLink()
94+
? $this->rmdir($item->getPathname())
95+
: $this->unlink($item->getPathname());
96+
}
97+
98+
unset($items);
99+
@rmdir($directory);
100+
}
101+
102+
public function name(string $path): string
103+
{
104+
return \pathinfo($path, \PATHINFO_FILENAME);
105+
}
106+
107+
public function basename(string $path): string
108+
{
109+
return \pathinfo($path, \PATHINFO_BASENAME);
110+
}
111+
112+
public function dirname(string $path): string
113+
{
114+
return \pathinfo($path, \PATHINFO_DIRNAME);
115+
}
116+
117+
public function extension(string $path): string
118+
{
119+
return \pathinfo($path, \PATHINFO_EXTENSION);
120+
}
121+
122+
public function filesize(string $path): int
123+
{
124+
if (! $this->exists($path)) {
125+
throw new IOException("File $path does not exists.");
126+
}
127+
128+
return \filesize($path);
129+
}
130+
131+
public function isDirectory(string $path): bool
132+
{
133+
return \is_dir($path);
134+
}
135+
136+
public function isFile(string $path): bool
137+
{
138+
return \is_file($path);
139+
}
140+
141+
public function mimeType(string $path): string
142+
{
143+
if (! $this->exists($path)) {
144+
throw new IOException("File not found $path");
145+
}
146+
147+
return \mime_content_type($path);
148+
}
149+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
interface FilesystemInterface
15+
{
16+
/**
17+
* Checks if a file exists at the given path.
18+
*
19+
* @param string $path
20+
* @return bool
21+
* @throws \Aether\Filesystem\IOException
22+
*/
23+
public function exists(string $path): bool;
24+
25+
/**
26+
* Gets the contents of a file.
27+
*
28+
* @param string $path
29+
* @return string
30+
*/
31+
public function read(string $path): string;
32+
33+
/**
34+
* Writes the contents to a file.
35+
*
36+
* @param string $path
37+
* @param string $contents
38+
* @param bool $lock
39+
* @return void
40+
* @throws \Aether\Filesystem\IOException
41+
*/
42+
public function write(string $path, string $contents, bool $lock = false): void;
43+
44+
/**
45+
* Copies a file to a new location.
46+
*
47+
* @param string $source
48+
* @param string $destination
49+
50+
* @return void
51+
*
52+
* @throws \Aether\Filesystem\IOException
53+
*/
54+
public function copy(string $source, string $destination): void;
55+
56+
/**
57+
* Moves a file to a new location.
58+
*
59+
* @param string $source
60+
* @param string $destination
61+
*
62+
* @return void
63+
*
64+
* @throws \Aether\Filesystem\IOException
65+
*/
66+
public function move(string $source, string $destination): void;
67+
68+
/**
69+
* Appends contents to a file.
70+
*
71+
* @param string $path
72+
* @param string $contents
73+
* @return bool
74+
*/
75+
public function append(string $path, string $contents): bool;
76+
77+
/**
78+
* Sets access and modification time of file.
79+
*
80+
* @param string $filename
81+
* @param int|null $mtime
82+
* @param int|null $atime
83+
*/
84+
public function touch(string $filename, ?int $mtime = null, ?int $atime = null): void;
85+
86+
/**
87+
* Deletes a file.
88+
*
89+
* @param string|array $filename
90+
* @return void
91+
*
92+
* @throws \Aether\Filesystem\IOException
93+
*/
94+
public function unlink(string|array $filename): void;
95+
96+
/**
97+
* Creates a directory.
98+
*
99+
* @param string $directory
100+
* @param int $mode
101+
* @param bool $recursive
102+
*
103+
* @return void
104+
*
105+
* @throws \Aether\Filesystem\IOException
106+
*/
107+
public function mkdir(string $directory, int $mode = 0777, bool $recursive = true): void;
108+
109+
/**
110+
* Deletes a directory.
111+
*
112+
* @param string $directory
113+
* @return void
114+
*
115+
* @throws \Aether\Filesystem\IOException
116+
*/
117+
public function rmdir(string $directory): void;
118+
119+
/**
120+
* Gets the name.
121+
*
122+
* @param string $path
123+
* @return string
124+
*/
125+
public function name(string $path): string;
126+
127+
/**
128+
* Gets the basename.
129+
*
130+
* @param string $path
131+
* @return string
132+
*/
133+
public function basename(string $path): string;
134+
135+
/**
136+
* Gets the dirname.
137+
*
138+
* @param string $path
139+
* @return string
140+
*/
141+
public function dirname(string $path): string;
142+
143+
/**
144+
* Gets the file extension.
145+
*
146+
* @param string $path
147+
* @return string
148+
*/
149+
public function extension(string $path): string;
150+
151+
/**
152+
* Gets the file size.
153+
*
154+
* @param string $path
155+
* @return int
156+
*
157+
* @throws \Aether\Filesystem\IOException
158+
*/
159+
public function filesize(string $path): int;
160+
161+
/**
162+
* Check if a path is a directory.
163+
*
164+
* @param string $path
165+
* @return bool
166+
*/
167+
public function isDirectory(string $path): bool;
168+
169+
/**
170+
* Check if a path is a file.
171+
*
172+
* @param string $path
173+
* @return bool
174+
*/
175+
public function isFile(string $path): bool;
176+
177+
/**
178+
* Get the mime type of file.
179+
*
180+
* @param string $path
181+
* @return string
182+
*
183+
* @throws \Aether\Filesystem\IOException
184+
*/
185+
public function mimeType(string $path): string;
186+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 IOException extends \RuntimeException
15+
{
16+
}

0 commit comments

Comments
 (0)