Skip to content

Commit 8f0bbaa

Browse files
committed
Add docs to README
1 parent 97caf0b commit 8f0bbaa

File tree

3 files changed

+192
-19
lines changed

3 files changed

+192
-19
lines changed

README.md

Lines changed: 189 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# file ![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)
1+
# amphp/file
22

3-
`amphp/file` allows non-blocking access to the filesystem for [Amp](https://github.com/amphp/amp).
3+
AMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind.
4+
`amphp/file` provides non-blocking file system access.
5+
6+
[![Latest Release](https://img.shields.io/github/release/amphp/file.svg?style=flat-square)](https://github.com/amphp/file/releases)
7+
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/amphp/file/blob/master/LICENSE)
48

59
## Installation
610

@@ -10,15 +14,191 @@ This package can be installed as a [Composer](https://getcomposer.org/) dependen
1014
composer require amphp/file
1115
```
1216

13-
## Optional Extension Backends
17+
`amphp/file` works out of the box without any PHP extensions.
18+
It uses multiple processes by default, but also comes with a blocking driver that just uses PHP's blocking functions in the current process.
19+
20+
Extensions allow using threading in the background instead of using multiple processes:
21+
22+
- [`ext-eio`](https://pecl.php.net/package/eio)
23+
- [`ext-uv`](https://github.com/amphp/ext-uv)
24+
- [`ext-parallel`](https://github.com/krakjoe/parallel)
25+
26+
## Usage
27+
28+
### read
29+
30+
Read the specified file's contents:
31+
32+
```php
33+
$contents = Amp\File\read('/path/to/file.txt');
34+
```
35+
36+
### write
37+
38+
Write the contents string to the specified path:
39+
40+
```php
41+
Amp\File\write('/path/to/file.txt', 'contents');
42+
```
43+
44+
### openFile
45+
46+
Open a [`File` handle](#file) for the specified path, e.g. to stream data:
47+
48+
```php
49+
$file = Amp\File\openFile('/path/to/file.txt', 'r');
50+
Amp\ByteStream\pipe($file, getStdout());
51+
```
52+
53+
### getStatus
54+
55+
Execute a file stat operation.
56+
57+
If the requested path does not exist the function will return `null`.
58+
59+
### getLinkStatus
60+
61+
Same as [`getStatus()`](#getstatus) except if the path is a link then the link's data is returned.
62+
63+
If the requested path does not exist the function will return `null`.
64+
65+
### exists
66+
67+
Checks whether the specified path exists.
68+
69+
### getSize
70+
71+
Returns the file size in bytes.
72+
73+
### isDirectory
74+
75+
Checks whether the given path exists and is a directory.
76+
77+
### isFile
78+
79+
Checks whether the given path exists and is a regular file.
80+
81+
### isSymlink
82+
83+
Checks whether the given path exists and is a symlink.
84+
85+
### getModificationTime
86+
87+
Returns the file's modification time as Unix timestamp in seconds.
88+
89+
### getAccessTime
90+
91+
Returns the file's access time as Unix timestamp in seconds.
92+
93+
### getCreationTime
94+
95+
Returns the file's creation time as Unix timestamp in seconds.
96+
97+
### createHardlink
98+
99+
Creates a new hardlink.
100+
101+
### createSymlink
102+
103+
Creates a new symlink.
104+
105+
### resolveSymlink
106+
107+
Resolves a symlink to its target path.
108+
109+
### move
110+
111+
Move / rename a file or directory.
112+
113+
### deleteFile
114+
115+
Deletes a file.
116+
117+
### createDirectory
118+
119+
Creates a directory.
120+
121+
### createDirectoriesRecursively
122+
123+
Creates a directory and its parents.
124+
125+
### deleteDirectory
126+
127+
Deletes a directory.
128+
129+
### listFiles
130+
131+
List all files and subdirectories in a directory.
132+
133+
### changePermissions
134+
135+
Change the permissions of a file or directory.
136+
137+
### changeOwner
138+
139+
Change the ownership of a file or directory.
140+
141+
### touch
142+
143+
Update the access and modification time of the specified path.
144+
145+
If the file does not exist it will be created automatically.
146+
147+
### File
148+
149+
Reference to an open file handle.
150+
151+
#### File::read
152+
153+
See also `File::isReadable`.
154+
155+
#### File::write
156+
157+
See also `File::isWritable`.
158+
159+
#### File::end
160+
161+
See `WritableStream::end()`.
162+
163+
#### File::close
164+
165+
Close the file handle.
166+
167+
- `File::isClosed()` can be used to check if the file handle has already been closed.
168+
- `File::onClose()` can be used ot register a callback that's called on file handle closure.
169+
170+
#### File::seek
171+
172+
Set the internal pointer position.
173+
174+
- `SEEK_SET`: Set position equal to offset bytes.
175+
- `SEEK_CUR`: Set position to current location plus offset.
176+
- `SEEK_END`: Set position to end-of-file plus offset.
177+
178+
Returns the new offset position.
179+
180+
See also `File::isSeekable`.
181+
182+
#### File::tell
183+
184+
Return the current internal offset position of the file handle.
185+
186+
#### File::eof
187+
188+
Test for being at the end of the stream (a.k.a. "end-of-file").
189+
190+
#### File::getPath
191+
192+
Retrieve the path used when opening the file handle.
193+
194+
#### File::getMode
195+
196+
Retrieve the mode used when opening the file handle.
14197

15-
Extensions allow using threading in the background instead of using multiple processes.
16-
17-
- [`ext-eio`](https://pecl.php.net/package/eio)
18-
- [`ext-uv`](https://github.com/amphp/ext-uv)
19-
- [`ext-parallel`](https://github.com/krakjoe/parallel)
198+
#### File::truncate
20199

21-
`amphp/file` works out of the box without any PHP extensions. It uses multi-processing by default, but also comes with a blocking driver that just uses PHP's blocking functions in the current process.
200+
Truncates the file to the given length.
201+
If `$size` is larger than the current file size, the file is extended with NUL bytes.
22202

23203
## Versioning
24204

src/File.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ interface File extends ReadableStream, WritableStream
1616

1717
/**
1818
* Read $length bytes from the open file handle.
19-
*
20-
*
2119
*/
2220
public function read(?Cancellation $cancellation = null, int $length = self::DEFAULT_READ_LENGTH): ?string;
2321

@@ -30,20 +28,17 @@ public function read(?Cancellation $cancellation = null, int $length = self::DEF
3028
* SEEK_CUR - Set position to current location plus offset.
3129
* SEEK_END - Set position to end-of-file plus offset.
3230
*
33-
*
3431
* @return int New offset position.
3532
*/
3633
public function seek(int $position, int $whence = self::SEEK_SET): int;
3734

3835
/**
3936
* Return the current internal offset position of the file handle.
40-
*
4137
*/
4238
public function tell(): int;
4339

4440
/**
4541
* Test for being at the end of the stream (a.k.a. "end-of-file").
46-
*
4742
*/
4843
public function eof(): bool;
4944

@@ -54,13 +49,11 @@ public function isSeekable(): bool;
5449

5550
/**
5651
* Retrieve the path used when opening the file handle.
57-
*
5852
*/
5953
public function getPath(): string;
6054

6155
/**
6256
* Retrieve the mode used when opening the file handle.
63-
*
6457
*/
6558
public function getMode(): string;
6659

src/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,15 @@ function listFiles(string $path): array
264264
}
265265

266266
/**
267-
* Change permissions of a file or directory.
267+
* Change the permissions of a file or directory.
268268
*/
269269
function changePermissions(string $path, int $mode): void
270270
{
271271
filesystem()->changePermissions($path, $mode);
272272
}
273273

274274
/**
275-
* Change ownership of a file or directory.
275+
* Change the ownership of a file or directory.
276276
*
277277
* @param int|null $uid null to ignore
278278
* @param int|null $gid null to ignore
@@ -296,7 +296,7 @@ function touch(string $path, ?int $modificationTime = null, ?int $accessTime = n
296296
}
297297

298298
/**
299-
* Buffer the specified file's contents.
299+
* Read the specified file's contents.
300300
*
301301
* @param string $path The file path from which to buffer contents.
302302
*/

0 commit comments

Comments
 (0)