GithubHelp home page GithubHelp logo

glob's Introduction

Webmozart Glob

Latest Stable Version Total Downloads

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, so usage of __DIR__ is encouraged:

use Webmozart\Glob\Glob;

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

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 [email protected] or shout out to @webmozart on Twitter.

License

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

glob's People

Contributors

asika32764 avatar dependabot-preview[bot] avatar firehed avatar harikt avatar liqrgv avatar ocramius avatar tgalopin avatar vincentlanglet avatar webmozart avatar zobo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

glob's Issues

streamwrapper support for Glob

I have a backuptool that uses a directory structure to store files, e.g.:

/my/path/to/backupfiles/year/month/day/filename.ext

I'd like to unittest my code, so I'm looking into e.g. vfsStream to emulate the expected (and unexpected) structure.

I generate a glob pattern somewhere that is used to find files relating to certain servers, backup types (sql/files/etc) etc.
php's glob() works fine in production but doesn't work well with vfsStream; webmozart\glob looks promising but doesn't work well with streamwrappers such as vfsStream.

I found Path::isAbsolute() to be the culprit; if I simply return true, the code works fine otherwise.

Matching a URI doesn't work

Glob::match(
  "file:///C:/Users/felix/git/OpenSource/php-language-server/fixtures/format.php",
  "C:/Users/felix/git/OpenSource/php-language-server/fixtures/**/*.php"
);

Expected: true (the glob should only be applied on the "path" component of the URI, not the scheme)
Actual: false (since no scheme is defined in the glob pattern)

Extended glob expressions

The current implementation could be extended to support the following expressions.
For reference see http://man7.org/linux/man-pages/man7/glob.7.html

  • "?" Wildcard: matches any single character, e.g. "fo?" matches "foo", but not "fo" or "fools"
  • Character classes: denoted by brackets "[...]", e.g. "[ABCDEFabcdef0123456789]"
  • Ranges: denoted by brackets, e.g. "[A-Fa-f0-9]" (equivalent to the above character class)
  • Negation for character classes and ranges: denoted by "!", e.g. "[!aeiouAEIOU]" matches any character, except vowels

Add Glob::combine()

The method Glob::combine($glob1, $glob2) should be added that combines two globs/paths into another, stricter glob/path. The method should behave like this:

  • If both globs are static (paths), return the path if they are equal and null otherwise.
  • If one glob is dynamic (wildcards) and the other static (path), return the path if it matches the glob and null otherwise.
  • If both globs are dynamic, return the combined glob if they match each other and null otherwise.

Example for a combined glob:

Glob::combine('/foo/bar/**/*.yml', '/**/messages.yml') => '/foo/bar/**/messages.yml'

This is needed for implementing GlobBinding::findResources() in puli/issues#114.

Project still active?

Hi @webmozart - thank you for providing the glob library.

Are you still maintaining the project? We would like have new version available on packist.org which runs with current PHP versions.

Remove symfony/filesystem dependence

I think would be great to remove symfony/filesystem dependence because it used only in tests and conflicts with symfony 3.4/4.4. Many developers who used symfony 3.4/4.4 can't require this great lib. This is no have any reasons to use symfony/filesystem in dev requirements. It should be replaced by native php or add a range symfony/filesystem: ^4.4|^5.0. Thanks

Package webmozart/path-util is abandoned, you should avoid using it. Use symfony/filesystem instead.

Just porting the issue from maglnet/ComposerRequireChecker#315 and quoting @Ocramius:

FWIW, if there's a non-symfony solution to that, it could be a better approach: mostly worried about dwindling package quality and continuously increasing dependency ranges in symfony/*.

I dont know another non-symfony package that is doing the stuff needed. Only used path-util / glob or filesystem / finder, so suggestions welcome i guess ๐Ÿ‘ฏ

GLOB_BRACE not defined in Alpine

It looks like that GLOB_BRACE is not supported in Alpine Linux (see https://bugs.php.net/bug.php?id=72095).

This leads to the following warning

Warning: glob() expects parameter 2 to be integer, string given in /app/vendor/webmozart/glob/src/Iterator/GlobIterator.php on line 55

In the link above there is also a proposed workaround to avoid such a problem

Glob and Symfony

I open this not for an issue, but to discuss the possibility of adding Glob to Symfony FileSystem or Symfony Finder. Will this be possible?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.