简体   繁体   English

Symfony Finder:获取具有特定扩展名的所有文件以及特定目录中的所有目录

[英]Symfony Finder: Get all the files with a specific extension and all the directories within a specific directory

I am using the Symfony Finder to get all the files with a certain extension and all the directories in a specific directory.我正在使用Symfony Finder获取具有特定扩展名的所有文件以及特定目录中的所有目录。


    protected function getDirectoryContent(string $directory): array
    {
        $finder = Finder::create()
            ->in($directory)
            ->depth(0)
            ->name(['*.json', '*.php'])
            ->sortByName();

        return iterator_to_array($finder, true);
    }

In this way, this method returns only all the files with extension .php or .json within a certain directory.这样,该方法只返回某个目录下所有扩展名为.php.json的文件。 For example, the directory structure I am looking in is the following:例如,我正在查找的目录结构如下:

/my/directory/
├── A
├── A.JSON
├── anotherfile.kas
├── file0.ds
├── file1.json
├── file2.php
├── file3.php
├── B
└── C

A , B and C are directories. ABC是目录。

When I pass the above directory path as the $directory argument in the method I showed above, I get an array with the following elements:当我在上面显示的方法中将上述directory path作为$directory参数传递时,我得到一个包含以下元素的数组:

file1.json
file2.php
file3.php

Great!, but my question is, how could I also add all the directories to the resulting array?太棒了!,但我的问题是,我怎样才能将所有directories添加到结果数组中? My expectations are to get an array like the following:我的期望是得到一个如下所示的数组:

A
B
C
file1.json
file2.php
file3.php

In you case, you speak to finder:在你的情况下,你和finder说话:

  • Please add recursive directory iterator with depth 0 (it's OK, we want to search only in root)请添加深度为0的递归目录迭代器(没关系,我们只想在root中搜索)
  • Please add file name iterator (it's wrong, because you find only files ).请添加文件名迭代器(这是错误的,因为您只找到files )。

As result it's wrong, because these two rules contradict each other - because you want search only files.结果是错误的,因为这两个规则相互矛盾 - 因为您只想搜索文件。

But, symfony finder can use CallbackIterator with filter model.但是,symfony finder 可以将CallbackIterator与过滤器模型一起使用。 In this situation you can add many rules or condition.在这种情况下,您可以添加许多规则或条件。 In you example:在你的例子中:

namespace Acme;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

include __DIR__.'/vendor/autoload.php';

$finder = Finder::create();

$finder
    ->in(__DIR__)
    ->depth(0)
    ->filter(static function (SplFileInfo $file) {
        return $file->isDir() || \preg_match('/\.(php|json)$/', $file->getPathname());
    });

print_r(\iterator_to_array($finder));

In this case, you speak:在这种情况下,你说:

  • Please find only in root.请仅在 root 中查找。
  • Please check - or file or match by my pattern.请检查 - 或归档或匹配我的模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在特定时间后从目录和所有子目录中修改的PHP删除(.extension)文件 - PHP delete (.extension) files that are modified after specific time from directory and all sub-directories 递归获取目录中的所有文件,并按扩展名获取子目录 - Recursivly get all files in a directory, and sub directories by extension 如何扫描目录和所有子目录中的特定文件(扩展名为.php) - How to scan a directory and all subdirectorys for specific files (with .php extension) PHP递归函数:获取目录及其子目录中的所有文件和目录 - PHP Recursive function: Get all files and directories within a directory and its sub-directories 如何获取JavaScript数组中特定目录中文件的所有路径 - How to get all the paths of files inside a specific directory in JavaScript array Symfony Finder:查找除以下划线开头的所有目录 - Symfony Finder: Finding all directories except ones that start with an underscore 删除除特定文件和具有特定扩展名的文件以外的所有文件 - Delete All Except Specific Files and Files With Specific Extension 递归将给定目录PHP或命令行中的所有文件和目录大写 - Recursively Capitalize all Files and Directories within a Given Directory PHP or Commandline 仅为特定文件而不是特定扩展名的所有文件启用 PHP - Enable PHP for only a specific file instead of all files of a specific extension PHP-特定目录中的所有脚本具有相同的包含路径 - PHP - All scripts within specific directory to have same include path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM