nachonerd/silex-finder-provider

This is a Silex service provider and a wrapper of Symfony finder

1.1.1 2015-09-11 23:47 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Code Climate Test Coverage Minimum PHP Version

SensioLabsInsight

License

GPL-3.0

Requirements

Install

composer require nachonerd/silex-finder-provider

Usage

Include following line of code somewhere in your initial Silex file (index.php or whatever):

...
$app->register(new \NachoNerd\Silex\Finder\Provider());
...

Now you have access to instance of finder throw $app['nn.finder'].

Example

<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->in(__DIR__);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());

        // Dump the relative path to the file, omitting the filename
        var_dump($file->getRelativePath());

        // Dump the relative path to the file
        var_dump($file->getRelativePathname());
    }
    ...

What new in version 1.1.0 ?

I Add New Sort Desc methods

The methods sort in the original finder are ASC. I add DESC sort method.

  • sortByModifiedTimeDesc
<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->sortByModifiedTimeDesc()->in(__DIR__);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...
  • sortByChangedTimeDesc
<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->sortByChangedTimeDesc()->in(__DIR__);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...
  • sortByAccessedTimeDesc
<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->sortByAccessedTimeDesc()->in(__DIR__);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...
  • sortByTypeDesc
<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->sortByTypeDesc()->in(__DIR__);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...
  • sortByNameDesc
<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->sortByNameDesc()->in(__DIR__);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...

I Add Get N First Files or Dirs

Now you can get the n first files or dirs. And if you combine this new method with the sort desc new methods you can get last first files or dirs.

<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->sortByChangedTimeDesc()->in(__DIR__);
    $finder = $finder->getNFirst(10);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...
<?php
    require_once __DIR__.'/../vendor/autoload.php';

    $app = new Silex\Application();

    // Considering the config.yml files is in the same directory as index.php
    $app->register(new \NachoNerd\Silex\Finder\Provider());

    ...
    $finder = $app["nn.finder"];
    $finder->files()->in(__DIR__);
    $finder = $finder->getNFirst(10);
    foreach ($finder as $file) {
        // Dump the absolute path
        var_dump($file->getRealpath());
    }
    ...