neunerlei / filesystem
symfony 文件系统组件的静态包装,增加了额外的功能
Requires
- symfony/filesystem: ^5.4|^v6.2.0
Requires (Dev)
- neunerlei/dbg: ^1.8
- phpunit/phpunit: ^9.0
README
此包包含一个 symfony 文件系统组件的静态包装,并增加了一些额外功能。从版本 5.3 开始,它还包含对 Path 实用工具的扩展,并替代了 Neunerlei/path-util。
安装
使用 composer 安装此包
composer require neunerlei/filesystem
文件系统实用工具
文件系统类位于 Neunerlei\FileSystem\Fs
。
getFs()
返回 symfony 文件系统类的单例实例
use Neunerlei\FileSystem\Fs; // Access the symfony methods as you would normally Fs::getFs()->isAbsolutePath("...");
copy()
复制文件或目录。如果目标文件比原始文件旧,它总是会被覆盖。
use Neunerlei\FileSystem\Fs; // Copy a file Fs::copy("/path/file.txt", "/anotherPath/file.txt"); // Copy / mirror a directory Fs::copy("/path/to/directory", "/anotherPath");
mkdir()
递归创建目录。
use Neunerlei\FileSystem\Fs; Fs::mkdir("/create/directory/recursively");
exists()
检查文件或目录是否存在。
use Neunerlei\FileSystem\Fs; Fs::exists("/check/existence.txt"); // TRUE|FALSE
isReadable()
告诉文件或文件列表是否存在且可读。
use Neunerlei\FileSystem\Fs; Fs::isReadable("/check/readability.txt"); // TRUE|FALSE
isWritable()
告诉文件或文件列表是否存在且可写。
use Neunerlei\FileSystem\Fs; Fs::isWritable("/check/writeability.txt"); // TRUE|FALSE
isFile()
告诉文件或路径列表是否存在且仅包含文件。
use Neunerlei\FileSystem\Fs; Fs::isFile("/check/if/fileExists.txt"); // TRUE|FALSE
isDir()
告诉文件或路径列表是否存在且仅包含目录。
use Neunerlei\FileSystem\Fs; Fs::isDir("/check/if/directoryExists.txt"); // TRUE|FALSE
touch()
设置文件的访问和修改时间。
注意:您也可以传递 DateTime 对象作为时间戳!
use Neunerlei\FileSystem\Fs; Fs::touch("/check/touchy.txt", new DateTime());
remove()
删除文件或目录。
use Neunerlei\FileSystem\Fs; Fs::remove("/path/to/remove.txt");
flushDirectory()
从一个给定的目录中删除所有内容,但不删除该元素本身。
use Neunerlei\FileSystem\Fs; Fs::flushDirectory("/path/to/clean");
getDirectoryIterator()
辅助函数,用于创建目录迭代器,可以是单个文件夹或递归。点将被自动跳过。它还可以找到匹配正则表达式的文件。默认情况下,文件夹将在子文件夹之后,可以使用选项切换。
use Neunerlei\FileSystem\Fs; // Traverse the direct children of a directory Fs::getDirectoryIterator("/path/to/iterate"); // Traverse a directory recursively Fs::getDirectoryIterator("/path/to/iterate", true); // Filter by regex Fs::getDirectoryIterator("/path/to/iterate", true, ["regex" => "/\.txt$/"]); // Return the folders before returning the children Fs::getDirectoryIterator("/path/to/iterate", true, ["dirFirst"]);
rename()
允许您重命名文件或目录
use Neunerlei\FileSystem\Fs; Fs::rename("/path/a", "/path/b");
getPermissions()
返回给定文件的 Unix 文件权限,如 "0777",作为一个字符串。
use Neunerlei\FileSystem\Fs; Fs::getPermissions("/file/with/full/access.txt"); // "0777" Fs::getPermissions("/file/with/read/access.txt"); // "0222"
setPermissions()
可用于设置文件或文件夹的 Unix 权限。
use Neunerlei\FileSystem\Fs; Fs::setPermissions("/file/path.txt", 0222); Fs::setPermissions("/directory", 0222);
getOwner()
返回给定文件或文件夹的数字 Unix 用户 ID。
use Neunerlei\FileSystem\Fs; Fs::getOwner("/file/access.txt"); // 1000
setOwner()
可用于更新给定文件或文件夹的所有者。
use Neunerlei\FileSystem\Fs; Fs::setOwner("/file/access.txt", 1001);
getGroup()
返回给定文件名的数字 Unix 用户组。
use Neunerlei\FileSystem\Fs; Fs::getGroup("/file/access.txt"); // 5
setGroup()
可用于更新给定文件或文件夹的组。
use Neunerlei\FileSystem\Fs; Fs::setGroup("/file/access.txt", 10);
readFile()
file_get_contents 的包装,读取内容,但用有意义的异常处理不可读或不存在文件。
use Neunerlei\FileSystem\Fs; $content = Fs::readFile("/file/access.txt");
readFileAsLines()
file() 的包装,用有意义的异常处理不存在或不可读的文件。
use Neunerlei\FileSystem\Fs; $lines = Fs::readFileAsLines("/file/access.txt");
writeFile()
将给定内容写入您的文件系统上的文件。
use Neunerlei\FileSystem\Fs; Fs::writeFile("/file.txt", "myContent");
appendToFile()
将内容追加到现有文件。默认情况下,所有内容都将添加在新行上。
use Neunerlei\FileSystem\Fs; // Add "myContent" as a new line to the file Fs::appendToFile("/file.txt", "myContent"); // Add "myContent" directly at the end Fs::appendToFile("/file.txt", "myContent", false);
路径实用工具
文件系统类位于 Neunerlei\FileSystem\Path
。
use Neunerlei\FileSystem\Path; // These methods are added by this fork // ========================================================== echo Path::unifySlashes("\\foo/bar\\baz"); // => /foo/bar/baz (on linux) or \foo\bar\baz (on windows) echo Path::unifyPath("\\foo/bar\\baz"); // => /foo/bar/baz/ (on linux) or \foo\bar\baz\ (on windows) echo Path::classBasename(\Neunerlei\FileSystem\Path::class); // => Path echo Path::classNamespace(\Neunerlei\FileSystem\Path::class); // => Neunerlei\FileSystem $link = Path::makeUri(); // => Returns a new Uri object -> See "URI" Section for details. // Those methods were already in the base implementation // ========================================================== echo Path::canonicalize('/var/www/vhost/webmozart/../config.ini'); // => /var/www/vhost/config.ini echo Path::canonicalize('C:\Programs\Webmozart\..\config.ini'); // => C:/Programs/config.ini echo Path::canonicalize('~/config.ini'); // => /home/webmozart/config.ini echo Path::makeAbsolute('config/config.yml', '/var/www/project'); // => /var/www/project/config/config.yml echo Path::makeRelative('/var/www/project/config/config.yml', '/var/www/project/uploads'); // => ../config/config.yml $paths = array( '/var/www/vhosts/project/httpdocs/config/config.yml', '/var/www/vhosts/project/httpdocs/images/banana.gif', '/var/www/vhosts/project/httpdocs/uploads/../images/nicer-banana.gif', ); Path::getLongestCommonBasePath($paths); // => /var/www/vhosts/project/httpdocs Path::getFilename('/views/index.html.twig'); // => index.html.twig Path::getFilenameWithoutExtension('/views/index.html.twig'); // => index.html Path::getFilenameWithoutExtension('/views/index.html.twig', 'html.twig'); Path::getFilenameWithoutExtension('/views/index.html.twig', '.html.twig'); // => index Path::getExtension('/views/index.html.twig'); // => twig Path::hasExtension('/views/index.html.twig'); // => true Path::hasExtension('/views/index.html.twig', 'twig'); // => true Path::hasExtension('/images/profile.jpg', array('jpg', 'png', 'gif')); // => true Path::changeExtension('/images/profile.jpeg', 'jpg'); // => /images/profile.jpg Path::join('phar://C:/Documents', 'projects/my-project.phar', 'composer.json'); // => phar://C:/Documents/projects/my-project.phar/composer.json Path::getHomeDirectory(); // => /home/webmozart
运行测试
- 克隆仓库
- 使用
composer install
安装依赖 - 使用
composer test
运行测试
特别感谢
特别感谢 LABOR.digital(这是德语中的实验室,而不是英语中的“工作” :D)使我能够在线发布我的代码。
明信片软件
您可以自由使用这个软件包,但如果它进入您的生产环境,我将非常感激您从您家乡寄给我一张明信片,注明您正在使用我们哪个软件包。
您可以在这里找到我的地址。
谢谢 :D