1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00
file/lib/functions.php

318 lines
7.7 KiB
PHP
Raw Normal View History

<?php
2015-07-11 03:59:39 +02:00
2015-08-05 16:55:56 +02:00
namespace Amp\File;
2017-06-17 23:41:57 +02:00
use Amp\Loop;
use Amp\Promise;
2016-07-21 01:33:03 +02:00
const LOOP_STATE_IDENTIFIER = Driver::class;
2015-07-11 03:59:39 +02:00
/**
2017-06-17 23:41:57 +02:00
* Retrieve the application-wide filesystem instance.
2015-07-11 03:59:39 +02:00
*
2016-07-21 01:33:03 +02:00
* @param \Amp\File\Driver $driver Use the specified object as the application-wide filesystem instance
* @return \Amp\File\Driver
2015-07-11 03:59:39 +02:00
*/
function filesystem(Driver $driver = null): Driver {
2016-07-21 01:33:03 +02:00
if ($driver === null) {
2016-08-12 17:47:55 +02:00
$driver = Loop::getState(LOOP_STATE_IDENTIFIER);
2016-07-21 01:33:03 +02:00
if ($driver) {
return $driver;
}
2017-01-11 14:22:06 +01:00
2016-07-21 01:33:03 +02:00
$driver = driver();
}
2016-08-12 17:47:55 +02:00
Loop::setState(LOOP_STATE_IDENTIFIER, $driver);
2016-07-21 01:33:03 +02:00
return $driver;
}
/**
2017-06-17 23:41:57 +02:00
* Create a new filesystem driver best-suited for the current environment.
*
* @return \Amp\File\Driver
*/
function driver(): Driver {
2016-07-21 01:33:03 +02:00
$driver = Loop::get();
2017-06-17 23:41:57 +02:00
if ($driver instanceof Loop\UvDriver) {
2016-07-21 01:33:03 +02:00
return new UvDriver($driver);
2017-06-17 23:41:57 +02:00
}
if (\extension_loaded("eio")) {
2015-07-30 16:10:53 +02:00
return new EioDriver;
2015-07-11 03:59:39 +02:00
}
2017-06-17 23:41:57 +02:00
return new ParallelDriver;
2015-07-11 03:59:39 +02:00
}
2015-08-13 01:02:41 +02:00
/**
2017-06-17 23:41:57 +02:00
* Open a handle for the specified path.
2015-08-13 01:02:41 +02:00
*
* @param string $path
* @param string $mode
* @return \Amp\Promise<\Amp\File\Handle>
2015-08-13 01:02:41 +02:00
*/
2016-11-15 06:17:19 +01:00
function open(string $path, string $mode): Promise {
2015-08-13 01:02:41 +02:00
return filesystem()->open($path, $mode);
}
/**
2017-06-17 23:41:57 +02:00
* Execute a file stat operation.
*
* If the requested path does not exist the resulting Promise will resolve to NULL.
2015-08-08 16:09:07 +02:00
* The returned Promise whould never resolve as a failure.
*
2015-08-08 16:09:07 +02:00
* @param string $path An absolute file system path
* @return \Amp\Promise<array|null>
*/
2016-11-15 06:17:19 +01:00
function stat(string $path): Promise {
return filesystem()->stat($path);
}
2015-08-08 16:09:07 +02:00
/**
* Does the specified path exist?
*
* This function should never resolve as a failure -- only a successfull bool value
* indicating the existence of the specified path.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<bool>
2015-08-08 16:09:07 +02:00
*/
2016-11-15 06:17:19 +01:00
function exists(string $path): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->exists($path);
}
/**
* Retrieve the size in bytes of the file at the specified path.
*
* If the path does not exist or is not a regular file this
* function's returned Promise WILL resolve as a failure.
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist or is not a file
* @return \Amp\Promise<int>
2015-08-08 16:09:07 +02:00
*/
2016-11-15 06:17:19 +01:00
function size(string $path): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->size($path);
}
/**
* Does the specified path exist and is it a directory?
*
* If the path does not exist the returned Promise will resolve
* to FALSE and will not reject with an error.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<bool>
2015-08-08 16:09:07 +02:00
*/
2016-11-15 06:17:19 +01:00
function isdir(string $path): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->isdir($path);
}
/**
* Does the specified path exist and is it a file?
*
* If the path does not exist the returned Promise will resolve
* to FALSE and will not reject with an error.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<bool>
2015-08-08 16:09:07 +02:00
*/
2016-11-15 06:17:19 +01:00
function isfile(string $path): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->isfile($path);
}
/**
2017-06-17 23:41:57 +02:00
* Retrieve the path's last modification time as a unix timestamp.
2015-08-08 16:09:07 +02:00
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist
* @return \Amp\Promise<int>
2015-08-08 16:09:07 +02:00
*/
2016-11-15 06:17:19 +01:00
function mtime(string $path): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->mtime($path);
}
/**
2017-06-17 23:41:57 +02:00
* Retrieve the path's last access time as a unix timestamp.
2015-08-08 16:09:07 +02:00
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist
* @return \Amp\Promise<int>
2015-08-08 16:09:07 +02:00
*/
function atime($path) {
return filesystem()->atime($path);
}
/**
2017-06-17 23:41:57 +02:00
* Retrieve the path's creation time as a unix timestamp.
2015-08-08 16:09:07 +02:00
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist
* @return \Amp\Promise<int>
2015-08-08 16:09:07 +02:00
*/
2016-11-15 06:17:19 +01:00
function ctime(string $path): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->ctime($path);
}
/**
2017-06-17 23:41:57 +02:00
* Same as stat() except if the path is a link then the link's data is returned.
*
2015-08-08 16:09:07 +02:00
* If the requested path does not exist the resulting Promise will resolve to NULL.
* The returned Promise whould never resolve as a failure.
*
* @param string $path An absolute file system path
* @return \Amp\Promise<array|null>
*/
2016-11-15 06:17:19 +01:00
function lstat(string $path): Promise {
return filesystem()->lstat($path);
}
/**
2017-06-17 23:41:57 +02:00
* Create a symlink $link pointing to the file/directory located at $original.
*
2015-08-08 16:09:07 +02:00
* @param string $original
* @param string $link
2015-08-08 16:09:07 +02:00
* @fails \Amp\Files\FilesystemException If the operation fails
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function symlink(string $original, string $link): Promise {
return filesystem()->symlink($original, $link);
}
/**
2017-06-17 23:41:57 +02:00
* Create a hard link $link pointing to the file/directory located at $original.
*
* @param string $original
* @param string $link
* @fails \Amp\Files\FilesystemException If the operation fails
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function link(string $original, string $link): Promise {
2015-08-08 16:09:07 +02:00
return filesystem()->symlink($original, $link);
}
/**
2017-06-17 23:41:57 +02:00
* Read the symlink at $path.
*
* @param string $original
* @param string $link
* @fails \Amp\Files\FilesystemException If the operation fails
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function readlink(string $path): Promise {
return filesystem()->readlink($path);
}
/**
2017-06-17 23:41:57 +02:00
* Rename a file or directory.
*
* @param string $from
* @param string $to
2015-08-08 16:09:07 +02:00
* @fails \Amp\Files\FilesystemException If the operation fails
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function rename(string $from, string $to): Promise {
return filesystem()->rename($from, $to);
}
/**
2017-06-17 23:41:57 +02:00
* Delete a file.
*
* @param string $path
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function unlink(string $path): Promise {
return filesystem()->unlink($path);
}
/**
2017-06-17 23:41:57 +02:00
* Create a director.
*
* @param string $path
* @param int $mode
2016-09-28 12:39:24 +02:00
* @param bool $recursive
* @return \Amp\Promise<null>
*/
2017-01-11 07:05:18 +01:00
function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise {
2016-09-28 12:39:24 +02:00
return filesystem()->mkdir($path, $mode, $recursive);
}
/**
2017-06-17 23:41:57 +02:00
* Delete a directory.
*
2016-07-21 01:33:03 +02:00
* @param string $path
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function rmdir(string $path): Promise {
return filesystem()->rmdir($path);
}
/**
2017-06-17 23:41:57 +02:00
* Retrieve an array of files and directories inside the specified path.
*
* Dot entries are not included in the resulting array (i.e. "." and "..").
*
* @param string $path
* @return \Amp\Promise<array>
*/
2016-11-15 06:17:19 +01:00
function scandir(string $path): Promise {
return filesystem()->scandir($path);
}
/**
2017-06-17 23:41:57 +02:00
* chmod a file or directory.
*
* @param string $path
* @param int $mode
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function chmod(string $path, int $mode): Promise {
return filesystem()->chmod($path, $mode);
}
/**
2017-06-17 23:41:57 +02:00
* chown a file or directory.
*
* @param string $path
2015-12-09 21:45:28 +01:00
* @param int $uid -1 to ignore
* @param int $gid -1 to ignore
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function chown(string $path, int $uid, int $gid = -1): Promise {
return filesystem()->chown($path, $uid, $gid);
}
/**
2017-06-17 23:41:57 +02:00
* Update the access and modification time of the specified path.
*
* If the file does not exist it will be created automatically.
*
* @param string $path
* @return \Amp\Promise<null>
*/
2016-11-15 06:17:19 +01:00
function touch(string $path): Promise {
return filesystem()->touch($path);
}
/**
2017-06-17 23:41:57 +02:00
* Buffer the specified file's contents.
*
* @param string $path The file path from which to buffer contents
* @return \Amp\Promise<string>
*/
2016-11-15 06:17:19 +01:00
function get(string $path): Promise {
return filesystem()->get($path);
}
/**
* Write the contents string to the specified path.
*
* @param string $path The file path to which to $contents should be written
* @param string $contents The data to write to the specified $path
* @return \Amp\Promise A promise resolving to the integer length written upon success
*/
2016-11-15 06:17:19 +01:00
function put(string $path, string $contents): Promise {
return filesystem()->put($path, $contents);
}