debuss-a / finder
这个 Finder 类通过一系列规则查找文件和目录,就像 Symfony 的 Finder 组件一样,但实现更加简单。
v1.0.0
2018-03-09 13:33 UTC
Requires
- php: >=5.5.11
This package is auto-updated.
Last update: 2024-09-23 17:07:16 UTC
README
这个 Finder 类通过一系列规则查找文件和目录,就像 Symfony 的 Finder 组件 一样,但实现更加简单。
主要目的是创建一个 Symfony Finder 的替代品,因为我觉得它对于它所要做的事情来说太“重”了。许多文件、文件夹、迭代器、比较器、类和异常(约20个)。这对于一个查找器来说太多了...
让我们保持简单!
由于我的 Finder 是 Symfony 的简化版,您可能找不到一些功能
- 自定义异常 (只有 SPL 的 Exception 类)
- 自定义 SplFileInfo,因此这些方法不可用
- getRelativePath()
- getRelativePathname()
- getContents()
(您仍然可以轻松地自行实现它们...)
安装
通过 composer
$ composer require debuss-a/finder "dev-master"
用法
列出目录中的文件和文件夹
use KeepItSimple\FileSystem\Finder; foreach (Finder::create()->in(__DIR__) as $file) { // Magic here // $file is an instance of SplFileInfo }
仅列出目录中的文件或文件夹
use KeepItSimple\FileSystem\Finder; foreach (Finder::create()->files()->in(__DIR__) as $file) { // Magic here // $file is an instance of SplFileInfo } foreach (Finder::create()->directories()->in(__DIR__) as $file) { // Magic here // $file is an instance of SplFileInfo }
选择一个目录
唯一的必填参数是路径。
您可以使用 in() 方法与 glob() 一样的路径。
in() 方法可以链式使用以包括多个路径,或者可以提供一个路径数组。
use KeepItSimple\FileSystem\Finder; foreach (Finder::create()->in(__DIR__.'/*/*/test') as $file) { // Magic here // $file is an instance of SplFileInfo } foreach (Finder::create()->in(__DIR__)->in('/home/alex/docs') as $file) { // Magic here // $file is an instance of SplFileInfo } foreach (Finder::create()->in([__DIR__, '/home/alex/docs']) as $file) { // Magic here // $file is an instance of SplFileInfo }
排序
已经提供了一些排序方法,但您也可以提供自己的。
use KeepItSimple\FileSystem\Finder; foreach (Finder::create()->in(__DIR__)->sortByName() as $file) { // Magic here // $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) { // Magic here // $file is an instance of SplFileInfo }
过滤
已经提供了一些过滤方法,但您也可以提供自己的。
use KeepItSimple\FileSystem\Finder; // The name() method accepts globs, strings, or regexes foreach (Finder::create()->in(__DIR__)->name('*.php') as $file) { // Magic here // $file is an instance of SplFileInfo } $finder = Finder::create()->in(__DIR__)->filter(function (SplFileInfo $current) { return $current->getBasename() !== 'index.php'; }); foreach ($finder as $file) { // Magic here // $file is an instance of SplFileInfo }
完整文档
完整的文档在 文档 文件夹中提供。
由 APIGen 生成。