borsch / finder
通过一组规则查找文件和目录。
v1.0
2018-10-23 09:56 UTC
This package is auto-updated.
Last update: 2024-09-17 20:17:47 UTC
README
此查找器类通过一组规则查找文件和目录。
此包是 Borsch 框架的一部分。
安装
通过 composer
$ composer require borsch/finder
用法
列出目录中的文件和文件夹
use Borsch\FileSystem\Finder; foreach (Finder::create()->in(__DIR__) as $file) { // $file is an instance of SplFileInfo }
仅列出目录中的文件或文件夹
use Borsch\FileSystem\Finder; foreach (Finder::create()->files()->in(__DIR__) as $file) { // $file is an instance of SplFileInfo } foreach (Finder::create()->directories()->in(__DIR__) as $file) { // $file is an instance of SplFileInfo }
选择一个目录
唯一必须的参数是路径。
您可以使用类似于 glob() 的 path 的 in() 方法。
in() 方法可以串联起来包含多个路径,或者可以提供一个路径数组。
use Borsch\FileSystem\Finder; foreach (Finder::create()->in(__DIR__.'/*/*/test') as $file) { // $file is an instance of SplFileInfo } foreach (Finder::create()->in(__DIR__)->in('/home/borsch/docs') as $file) { // $file is an instance of SplFileInfo } // Multidimentional array of any depth can be used $finder = new Finder(); $finder->in([ __DIR__, '/home/borsch/docs', [ '/some/other/path', [ '/some/more/other/path' ] ] ]); foreach ($finder as $file) { // $file is an instance of SplFileInfo }
排序
已经提供了一些排序方法,但您也可以提供自己的方法。
use Borsch\FileSystem\Finder; foreach (Finder::create()->in(__DIR__)->sortByName() as $file) { // $file is an instance of SplFileInfo } $finder = Finder::create()->in(__DIR__)->sort(function (SplFileInfo $a, SplFileInfo $b) { // Equivalent to the method sortByName() return strcmp($a->getFilename(), $b->getFilename()) }); foreach ($finder as $file) { // $file is an instance of SplFileInfo }
过滤
已经提供了一些过滤方法,但您也可以提供自己的方法。
use Borsch\FileSystem\Finder; $finder = Finder::create()->in(__DIR__)->filter(function (SplFileInfo $current) { // Remove index.php files return $current->getBasename() !== 'index.php'; }); foreach ($finder as $file) { // $file is an instance of SplFileInfo }
许可证
MIT License
Copyright (c) 2018 Alexandre DEBUSSCHERE
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.