Giter Club home page Giter Club logo

flyfinder's Introduction

License: MIT Scrutinizer Code Coverage Scrutinizer Code Quality Stable Version Unstable Version

FlyFinder

FlyFinder is a plugin for Flysystem that will enable you to find files based on certain criteria.

FlyFinder can search for files that are hidden (either because they are hidden files themselves, or because they are inside a hidden directory), that have a certain extension, or that exist in a certain path.

Flyfinder does not return directories themselves... only files.

Installation

The easiest way to install this library is with Composer using the following command:

$ composer require phpdocumentor/flyfinder

Examples

Ready to dive in and don't want to read through all that text below? Just consult the examples folder and check which type of action that your want to accomplish.

Usage

In order to use the FlyFinder plugin you first need a Flyfinder filesystem with an adapter, for instance the local adapter.

use League\Flysystem\Filesystem;
use League\Flysystem\Adapter;
use Flyfinder\Finder;

$filesystem = new Filesystem(new Adapter\Local(__DIR__.'/path/to/files/'));

Now you can add the plugin as follows:

$filesystem->addPlugin(new Finder());

FlyFinder will need specifications to know what to look for. The following specifications are available:

  • IsHidden (this specification will return true when a file or directory is hidden,
  • HasExtension (this specification will return true when a file or directory has the specified extension),
  • InPath (this specification will return true when a file is in the given path. Wildcards are allowed.)
    • note that this path should be considered relative to the $filesystem's path

Specifications can be instantiated as follows:

use Flyfinder\Path;
use Flyfinder\Specification\IsHidden;
use Flyfinder\Specification\HasExtension;
use Flyfinder\Specification\InPath;

$isHidden = new IsHidden();
$hasExtension = new HasExtension(['txt']);
$inPath = new InPath(new Path('mydir'));

Combining specifications

You can search on more criteria by combining the specifications. Specifications can be chained as follows:

$isHidden->andSpecification($hasExtension) will find all files and directories that are hidden AND have the given extension.

$isHidden->orSpecification($hasExtension) will find all files and directories that are hidden OR have the given extension.

$isHidden->notSpecification will find all files and directories that are NOT hidden.

You can also make longer chains like this:

$specification = $inPath->andSpecification($hasExtension)->andSpecification($isHidden->notSpecification());

This will find all files in the given path, that have the given extension and are not hidden.

flyfinder's People

Contributors

ashnazg avatar carusogabriel avatar dependabot-preview[bot] avatar dependabot[bot] avatar douglasmiller avatar jaapio avatar jnkowa-gfk avatar mbed67 avatar mvriel avatar rulatir avatar villfa 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

Watchers

 avatar  avatar  avatar  avatar  avatar

flyfinder's Issues

Should InPath('foo/bar') be satisfied by 'foo/barMOAR/some-file.txt'?

I don't think it should. I think it is a bug.

This test I added to InPathTest.php currently fails on master:

    /**
     * @uses \Flyfinder\Path
     *
     * @covers ::__construct
     * @covers ::isSatisfiedBy
     */
    public function testNoFalsePositiveWithLongerDirName() : void
    {
        $prefixDir    = 'absolute/path';
        $absolutePath = 'absolute/pathMOAR/to/file.txt';
        $spec         = new InPath(new Path($prefixDir));
        $this->assertFalse($spec->isSatisfiedBy([
            'type' => 'file',
            'path' => $absolutePath,
            'dirname' => dirname($absolutePath),
            'filename' => 'file',
            'extension' => 'txt',
            'basename' => 'file.txt',
        ]));
    }

If this is indeed a bug then I have a fix. PR will follow shortly.

Stable tag

Will a 1.0.0 tag be coming soon. Is there anything you need a hand with?

Optimization based on InPath paths found in the specification?

By looking at Finder code, if I create a local filesystem on the root directory and try to ->find() in it, Finder will attempt crawling everything. This is bad.

One could extend SpecificationInterface with yieldCandidatePaths():

  • CompositeSpecification would yield nothing by default
  • InPath would yield its own path
  • OrSpecification would merge, dedup and remove those paths whose ancestors are also specified
  • AndSpecification would merge, dedup and remove those paths whose descendants are also specified

Finder would only search in those paths instead of starting from ''.

Interested in a PR? This would be a breaking change because every specification class that supplies search paths would have to override yieldCandidatePaths() so that the paths it supplies won't be missed.

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.