ralphjsmit / stubs
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-03-19 08:46:21 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
通用
🐞 如果您发现一个错误,请提交详细的bug报告,我会尽快尝试修复它。
🔐 如果您发现一个漏洞,请查阅我们的安全策略。
🙌 如果您想贡献,请提交一个pull request。所有PR都会得到充分认可。如果您不确定我是否会接受您的想法,欢迎联系我!
🙋♂️ Ralph J. Smit