glob

A PHP implementation of Ant's glob.

  • 所有者: webmozarts/glob
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Webmozart Glob

Build Status
Build status
Scrutinizer Code Quality
Latest Stable Version
Total Downloads
Dependency Status

Latest release: 4.1.0

A utility implementing Ant-like globbing.

Syntax:

  • ? matches any character
  • * matches zero or more characters, except /
  • /**/ matches zero or more directory names
  • [abc] matches a single character a, b or c
  • [a-c] matches a single character a, b or c
  • [^abc] matches any character but a, b or c
  • [^a-c] matches any character but a, b or c
  • {ab,cd} matches ab or cd

API Documentation

Comparison with glob()

Compared to PHP's native glob() function, this utility supports:

  • /**/ for matching zero or more directories
  • globbing custom stream wrappers, like myscheme://path/**/*.css
  • matching globs against path strings
  • filtering arrays of path strings by a glob
  • exceptions if the glob contains invalid syntax

Since PHP's native glob() function is much more efficient, this utility uses
glob() internally whenever possible (i.e. when no special feature is used).

Installation

Use Composer to install the package:

$ composer require webmozart/glob

Usage

The main class of the package is Glob. Use Glob::glob() to glob the
filesystem:

use Webmozart\Glob\Glob;

$paths = Glob::glob('/path/to/dir/*.css'); 

You can also use GlobIterator to search the filesystem iteratively. However,
the iterator is not guaranteed to return sorted results:

use Webmozart\Glob\Iterator\GlobIterator;

$iterator = new GlobIterator('/path/to/dir/*.css');

foreach ($iterator as $path) {
    // ...
}

Path Matching

The package also provides utility methods for comparing paths against globs.
Use Glob::match() to match a path against a glob:

if (Glob::match($path, '/path/to/dir/*.css')) {
    // ...
}

Glob::filter() filters a list of paths by a glob:

$paths = Glob::filter($paths, '/path/to/dir/*.css');

The same can be achieved iteratively with GlobFilterIterator:

use Webmozart\Glob\Iterator\GlobFilterIterator;

$iterator = new GlobFilterIterator('/path/to/dir/*.css', new ArrayIterator($paths));

foreach ($iterator as $path) {
    // ...
}

You can also filter the keys of the path list by passing the FILTER_KEY
constant of the respective class:

$paths = Glob::filter($paths, '/path/to/dir/*.css', Glob::FILTER_KEY);

$iterator = new GlobFilterIterator(
    '/path/to/dir/*.css', 
    new ArrayIterator($paths),
    GlobFilterIterator::FILTER_KEY
);

Relative Globs

Relative globs such as *.css are not supported. Usually, such globs refer to
paths relative to the current working directory. This utility, however, does not
want to make such assumptions. Hence you should always pass absolute globs.

If you want to allow users to pass relative globs, I recommend to turn the globs
into absolute globs using the Webmozart Path Utility:

use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path;

// If $glob is absolute, that glob is used without modification.
// If $glob is relative, it is turned into an absolute path based on the current
// working directory.
$paths = Glob::glob(Path::makeAbsolute($glob, getcwd());

Windows Compatibility

Globs need to be passed in canonical form with forward slashes only.
Returned paths contain forward slashes only.

Escaping

The Glob class supports escaping by typing a backslash character \ before
any special character:

$paths = Glob::glob('/backup\\*/*.css');

In this example, the glob matches all CSS files in the /backup* directory
rather than in all directories starting with /backup. Due to PHP's own
escaping in strings, the backslash character \ needs to be typed twice to
produce a single \ in the string.

The following escape sequences are available:

  • \\?: match a ? in the path
  • \\*: match a * in the path
  • \\{: match a { in the path
  • \\}: match a } in the path
  • \\[: match a [ in the path
  • \\]: match a ] in the path
  • \\^: match a ^ in the path
  • \\-: match a - in the path
  • \\\\: match a \ in the path

Stream Wrappers

The Glob class supports stream wrappers:

$paths = Glob::glob('myscheme:///**/*.css');

Authors

Contribute

Contributions to the package are always welcome!

Support

If you are having problems, send a mail to bschussek@gmail.com or shout out to
@webmozart on Twitter.

License

All contents of this package are licensed under the MIT license.

主要指標

概覽
名稱與所有者webmozarts/glob
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2014-12-29 14:29:54
推送於2024-08-14 14:53:07
最后一次提交
發布數20
最新版本名稱4.7.0 (發布於 2024-03-07 20:34:39)
第一版名稱1.0.0-beta (發布於 )
用户参与
星數253
關注者數13
派生數16
提交數200
已啟用問題?
問題數17
打開的問題數4
拉請求數30
打開的拉請求數1
關閉的拉請求數17
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?