ralphjsmit / filesystem
一个针对PHP的流畅文件系统包。
Requires
- php: ^8.0
- illuminate/contracts: ^8.73|^9.0|^10.0
- spatie/laravel-package-tools: ^1.11.0
- symfony/finder: ^5.4|^6.0
Requires (Dev)
- nesbot/carbon: ^2.66
- nunomaduro/collision: ^5.10|^6.0|^7.0
- orchestra/testbench: ^6.23|^7.0|^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpunit/phpunit: ^9.5|^10.0
- ralphjsmit/pest-plugin-filesystem: ^1.0
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-19 09:55:34 UTC
README
一个针对PHP的流畅文件系统包
此包可以帮助您加快文件移动和复制的进程。它还使替换命名空间变得更加容易,因此成为处理重文件系统任务的宝贵工具。享受吧!
重要
此包仅更新至Laravel 10。
⚡️ 请查看我的网站上的发布文章或订阅我的通讯以获取偶尔的更新
安装
您可以通过composer安装此包
composer require ralphjsmit/filesystem
用法
此包通过为您提供基类 Stub
和类 File
来工作。正如其名所示,Stub
类是一个包含特定配置的对象,例如命名空间和基本目录。类 File
用于表示单个文件。
创建Stub配置
您可以使用 Stub::new()
创建一个新的 Stub
配置
use RalphJSmit\Filesystem\Stub; $stub = Stub::new();
您可以使用 Stub
配置如下(我将在下面详细讨论文件操作)
$stub->getFile(__DIR__ . '/tmp/testFileA.php')->move(__DIR__ . '/tmp/otherFolder');
您也可以为您的 Stub
设置一个基本目录
$stub = Stub::dir(__DIR__); $stub->getFile('/tmp/testFileA.php')->move('/tmp/otherFolder');
如果您已经有一个 $stub
实例,您可以在它们上配置命名空间。这些命名空间用于 File
对象的 ->namespace()
操作。这基本上意味着您定义项目中每个命名空间对应的目录。
$stubs = Stub::dir(__DIR__)->namespaces([ 'Support' => '/src/Support/', 'Domain' => '/src/Domain/', 'App' => '/src/App/', ]); $stubs->getFile('/tmp/TestFileA.php')->namespace('Support/Models'); // Moves __DIR__ . `/tmp/testFileA.php` to __DIR__ . `/src/Support/Models/testFileA.php`.
您也可以同时拥有多个stub配置
$stubA = Stub::dir(__DIR__ . '/src'); $stubB = Stub::dir(__DIR__ . '/app'); $stubC = Stub::dir(__DIR__ . '/tmp');
获取File对象
您可以直接在 Stub
类上调用 file()
来获取一个 File
对象
$file = Stub::file(__DIR__ . '/tmp/testFileA.php');
您也可以从 $stub
实例获取一个 File
对象
$stub = Stub::dir(__DIR__); $file = $stub->getFile('/tmp/testFileA.php');
对File对象的操作
如果您有一个 File
对象,您可以在其上执行以下操作
复制文件
您可以使用 copy()
函数将文件复制到新的 目录
$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->copy('/tmp/test'); // $file is now in __DIR__ . '/tmp/testFileA.php' AND in __DIR__ . '/tmp/test/testFileA.php'
删除文件
您可以使用 delete()
函数删除文件
Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->delete(); // returns true on success
获取文件的基名
您可以使用 getBasename()
函数获取文件的基名
Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getBasename(); // 'testFileA.php'
获取文件的目录位置
您可以使用 getDirectory()
函数获取文件的目录位置
Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getDirectory(); // __DIR__ . '/tmp'
获取文件的完整路径
您可以使用 getFilepath()
函数获取文件的完整路径
Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getFilepath(); // __DIR__ . '/testFileA.php'
获取文件内容
您可以使用 getContents()
函数获取文件内容
$contents = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getContents();
移动文件
您可以使用 move()
函数将文件移动到新的 目录
$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->move('/tmp/test'); // $file is now in __DIR__ . '/tmp/test/testFileA.php'
更新文件的命名空间
您可以使用 namespace()
助手更新文件的命名空间并将其移动到正确的目录。如果您需要将PHP文件移动到新目录并更新其命名空间,这将非常理想。我在 ralphjsmit/tall-install 包中使用此技术。
$basePath = __DIR__; $stubs = Stub::dir($basePath)->namespaces([ 'Support' => '/src/Support/', 'Domain' => '/src/Domain/', 'App' => '/src/App/', ]); $stubs->getFile('/app/Console/Kernel.php')->namespace('Support\App\Console'); // file is not in __DIR__ . '/src/Support/App/Console/Kernel.php'
更新文件内容
您可以使用 putFile()
方法更新文件内容
$newContents = 'Hello world!'; $file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->putFile($newContents);
您也可以指定文件的新位置
$newContents = 'Hello world!'; $file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->putFile($newContents, '/tmp/test/myFile.php'); // Will create a file with the "Hello world!" in __DIR__ . '/tmp/test/myFile.php` // Old file will still exist
如果您只想移动或复制文件,应使用这些方法。
更新文件的命名空间
您可以使用 replaceNamespace($newNamespace)
方法替换文件的命名空间。
$file = Stub::dir(__DIR)->getFile('/tmp/test/MyClass.php'); $file->replaceNamespace('App\Models'); // $file will now have the namespace App\Models
一般
🐞 如果您发现一个错误,请提交详细的问题报告,我会尽快修复。
🔐 如果您发现一个漏洞,请查看我们的安全策略。
🙌 如果您想贡献,请提交拉取请求。所有拉取请求都将得到全额认可。如果您不确定我会否接受您的想法,请随时联系我!
🙋♂️ Ralph J. Smit