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

272 lines
6.3 KiB
PHP
Raw Normal View History

2015-07-11 03:59:39 +02:00
<?php
2015-08-05 16:55:56 +02:00
namespace Amp\File;
2015-07-11 03:59:39 +02:00
/**
* Retrieve the application-wide filesystem instance
2015-07-11 03:59:39 +02:00
*
* @param \Amp\File\Driver $assign Use the specified object as the application-wide filesystem instance
* @return \Amp\File\Driver
2015-07-11 03:59:39 +02:00
*/
2015-07-30 16:10:53 +02:00
function filesystem(Driver $assign = null) {
static $driver;
2015-07-11 03:59:39 +02:00
if ($assign) {
2015-07-30 16:10:53 +02:00
return ($driver = $assign);
} elseif ($driver) {
return $driver;
} else {
2015-07-30 16:10:53 +02:00
return ($driver = driver());
}
}
/**
2015-07-30 16:10:53 +02:00
* Create a new filesystem driver best-suited for the current environment
*
* @return \Amp\File\Driver
*/
2015-07-30 16:10:53 +02:00
function driver() {
$reactor = \Amp\reactor();
if ($reactor instanceof \Amp\UvReactor) {
return new UvDriver($reactor);
2015-07-18 18:08:50 +02:00
} elseif (\extension_loaded("eio")) {
2015-07-30 16:10:53 +02:00
return new EioDriver;
2015-07-11 03:59:39 +02:00
} else {
2015-08-01 03:15:53 +02:00
return new BlockingDriver;
2015-07-11 03:59:39 +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>
*/
function stat($path) {
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>
*/
function exists($path) {
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>
*/
function size($path) {
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>
*/
function isdir($path) {
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>
*/
function isfile($path) {
return filesystem()->isfile($path);
}
/**
* Retrieve the path's last modification time as a unix timestamp
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist
* @return \Amp\Promise<int>
*/
function mtime($path) {
return filesystem()->mtime($path);
}
/**
* Retrieve the path's last access time as a unix timestamp
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist
* @return \Amp\Promise<int>
*/
function atime($path) {
return filesystem()->atime($path);
}
/**
* Retrieve the path's creation time as a unix timestamp
*
* @param string $path An absolute file system path
* @fails \Amp\Files\FilesystemException If the path does not exist
* @return \Amp\Promise<int>
*/
function ctime($path) {
return filesystem()->ctime($path);
}
/**
* 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>
*/
function lstat($path) {
return filesystem()->lstat($path);
}
/**
2015-08-08 16:09:07 +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>
*/
2015-08-08 16:09:07 +02:00
function symlink($original, $link) {
return filesystem()->symlink($original, $link);
}
/**
* 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>
*/
function rename($from, $to) {
return filesystem()->rename($from, $to);
}
/**
* Delete a file
*
* @param string $path
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<null>
*/
function unlink($path) {
return filesystem()->unlink($path);
}
/**
* Create a director
*
* @param string $path
* @param int $mode
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<null>
*/
function mkdir($path, $mode = 0644) {
return filesystem()->mkdir($path, $mode);
}
/**
* Delete a directory
*
2015-08-08 16:09:07 +02:00
* @param string $pat
* @return \Amp\Promise<null>
*/
function rmdir($path) {
return filesystem()->rmdir($path);
}
/**
* 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
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<array>
*/
function scandir($path) {
return filesystem()->scandir($path);
}
/**
* chmod a file or directory
*
* @param string $path
* @param int $mode
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<null>
*/
function chmod($path, $mode) {
return filesystem()->chmod($path, $mode);
}
/**
* chown a file or directory
*
* @param string $path
* @param int $uid
* @param int $gid
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<null>
*/
function chown($path, $uid, $gid) {
return filesystem()->chown($path, $uid, $gid);
}
/**
* Update the access and modification time of the specified path
*
* If the file does not exist it will be created automatically.
*
* @param string $path
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<null>
*/
function touch($path) {
return filesystem()->touch($path);
}
/**
* Buffer the specified file's contents
*
* @param string $path The file path from which to buffer contents
2015-08-08 16:09:07 +02:00
* @return \Amp\Promise<string>
*/
function get($path) {
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
*/
function put($path, $contents) {
return filesystem()->put($path, $contents);
}