automattic/ignorefile 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

automattic/ignorefile

最新稳定版本:v3.0.2

Composer 安装命令:

composer require automattic/ignorefile

包简介

Handle .gitignore style files.

README 文档

README

This small library allows for processing of .gitignore-style files in PHP.

It follows the documentation at gitignore, even in cases where git itself does not.

Installation

Require using composer require automattic/ignorefile.

Usage

// Read a .myignore file.
$ignore = new IgnoreFile();
$ignore->add( file_get_contents( '.myignore' ) );

// Test if a file is ignored.
if ( $ignore->ignores( $filename ) ) {
        echo "$filename is ignored\n";
} else {
        echo "$filename is not ignored\n";
}

// Filter ignored files from an array of files.
$filesToProcess = $ignore->filter( $allFiles );

// Load all .myignore files in a directory tree, then list all non-ignored files.
$ignore = new IgnoreFile();
$iter = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( '.' ) );
foreach ( $iter as $path ) {
        if ( basename( $path ) === '.myignore' ) {
                $ignore->add( file_get_contents( $path ), dirname( $path ) . '/' );
        }
}

$iter = new RecursiveIteratorIterator(
    $ignore->filterIterator( new RecursiveDirectoryIterator( '.' ) )
);
foreach ( $iter as $file ) {
        echo "$file\n";
}

Strict mode

To match git's behavior, invalid patterns passed to add() will be silently ignored. If you'd rather have exceptions thrown, set $ignore->strictMode to true.

Known incompatibilities with git

As of git 2.43.0.

The gitignore documentation refers to fnmatch for specifics of *, ?, and bracket expressions. That in turn refers to a few other documents.

Pattern Documented Git
a.[ Matches a.[, per §2.13.1 "Otherwise, '[' shall match the character itself." Never matches anything. Git aborts on unclosed bracket.
a.[x Matches a.[x, as above. Never matches anything, as above.
a.[x\\] Matches a.[x], as above. Never matches anything, as above.
a.[] Matches a.[], as above. Never matches anything, as above.
a.[\\] Matches a.[], as above. Never matches anything, as above.
a.[!] Matches a.[!], as above. Never matches anything, as above.
a.[z-a] Either matches nothing or is an error, per §9.3.5 "If the represented set of collating elements is empty, it is unspecified whether the expression matches nothing, or is treated as invalid." Matches a.z.

Inspiration

I needed something similar to the npm module ignore for PHP, but couldn't find one. So I wrote one, copying the fairly comprehensive set of tests to make sure my implementation was accurate.

统计信息

  • 总下载量: 201.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2021-12-16