italystrap/finder

PHP的面向对象方式的文件查找器

This package is auto-updated.

Last update: 2024-09-19 19:54:55 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License PHP from Packagist Mutation testing badge

面向对象的文件查找API

这是一个仍在进行中的仓库。

目录

安装

使用此包的最佳方式是通过Composer

composer require italystrap/finder

此包遵循SemVer规范,并且将在次要版本之间完全向后兼容。

基本用法

功能:搜索多个目录中第一个存在的文件

Given a list of file name
    And a list of directories to search on
When I search a file
Then the first available file is returned

基本示例

文件

    $list_of_file = [
        'file-specialized.php',
        'file.php',
    ];

目录

    $dirs = [
        'my/theme/child/template', // First dir to search the file
        'my/theme/parent/template', // Second dir to search the file
    ];

如果file-specialized.php存在于给定的目录之一中,它将返回文件名和完整路径。my/theme/child/template/file-specialized.phpmy/theme/parent/template/file-specialized.php

如果找不到file-specialized.php,则将搜索file.php并返回如果存在则完整路径my/theme/child/template/file.phpmy/theme/parent/template/file.php

如果没有找到file.php,则将抛出错误消息。

真实代码示例

use ItalyStrap\Finder\Finder;
use ItalyStrap\Finder\FilesHierarchyIterator;
use ItalyStrap\Finder\FileInfoFactory;
use ItalyStrap\Finder\FinderFactory;

    $dirs = [
        'my/theme/child/template', // First dir to search the file
        'my/theme/parent/template', // Second dir to search the file
    ];

$find = new Finder( new FilesHierarchyIterator( new FileInfoFactory() ) );
//or
$find = ( new FinderFactory() )->make();
$find->in( $dirs );

// Will search for:
// my/theme/child/template/file-specialized.php
// my/theme/child/template/file.php
// my/theme/parent/template/file-specialized.php
// my/theme/parent/template/file.php
/**
 * @var \SplFileInfo $files_found
 */
$file_found = $find->firstFile(['file', 'specialized'], 'php', '-');

功能:按优先级搜索资产文件

Given a list of file name
    And a list of directories to search on
When I search an asset file
Then the file with highest priority file is returned

基本示例

文件

    $min = \defined( 'WP_DEBUG' ) && WP_DEBUG ? '.min' : '';

    $list_of_file = [
        'style' . $min . '.css',
    ];

目录

    $dirs = [
        'my/theme/child/asset/css', // First dir to search the file
        'my/theme/parent/asset/css', // Second dir to search the file
    ];

如果style存在于给定的目录之一中,它将返回来自具有最高优先级的目录的文件名和完整路径my/theme/child/asset/css/style.css

如果在子目录中找不到style.css,则将在父目录中搜索并返回如果存在则完整路径my/theme/parent/asset/css/style.css

如果没有找到style.css,则将抛出错误消息。

真实代码示例

use ItalyStrap\Finder\Finder;
use ItalyStrap\Finder\FilesHierarchyIterator;
use ItalyStrap\Finder\FileInfoFactory;
use ItalyStrap\Finder\FinderFactory;

    $min = \defined( 'WP_DEBUG' ) && WP_DEBUG ? '.min' : '';
    $dirs = [
        'my/theme/child/asset/css', // First dir to search the file
        'my/theme/parent/asset/css', // Second dir to search the file
    ];

$find = new Finder( new FilesHierarchyIterator( new FileInfoFactory() ) );
//or
$find = ( new FinderFactory() )->make();
$find->in( $dirs );

// Will search for:
// my/theme/child/asset/css/style.min.css
// my/theme/child/asset/css/style.css
// my/theme/parent/asset/css/style.min.css
// my/theme/parent/asset/css/style.css
/**
 * @var \SplFileInfo $files_found
 */
$file_found = $find->firstFile(['style', $min], 'css', '.');

功能:搜索配置文件

Given a name of a config file
    And a list of directories to search on
When I search the config files
Then The list of all file with the same name founded are returned sorted

基本示例

文件

    $list_of_file = [
        'config.php',
    ];

目录

    $dirs = [
        'my/theme/child/config', // First dir to search the file
        'my/theme/parent/config', // Second dir to search the file
    ];

如果config.php存在于给定的一个或所有目录中,它将返回来自目录的文件名和完整路径my/theme/child/config/config.phpmy/theme/parent/config/config.php

如果没有找到config.php,则将抛出错误消息。

真实代码示例

use ItalyStrap\Finder\Finder;
use ItalyStrap\Finder\FilesHierarchyIterator;
use ItalyStrap\Finder\FileInfoFactory;
use ItalyStrap\Finder\FinderFactory;

    $dirs = [
        'my/theme/child/config', // First dir to search the file
        'my/theme/parent/config', // Second dir to search the file
    ];

$find = new Finder( new FilesHierarchyIterator( new FileInfoFactory() ) );
//or
$find = ( new FinderFactory() )->make();
$find->in( $dirs );

// Will search for:
// my/theme/child/config/config.php
// my/theme/parent/config/config.php
/**
 * @var array<\SplFileInfo> $files_found
 */
$files_found = $find->allFiles(['config'], 'php', '-');

高级用法

贡献

欢迎所有反馈/错误报告/拉取请求。

许可证

版权(c)2019 Enea Overclokk,ItalyStrap

此代码根据MIT许可证授权。

致谢