Silex Finder Provider
, (*1)
, (*2)
License
GPL-3.0, (*3)
Requirements
Install
composer require nachonerd/silex-finder-provider
Usage
Include following line of code somewhere in your initial Silex file (index.php or whatever):, (*4)
...
$app->register(new \NachoNerd\Silex\Finder\Provider());
...
Now you have access to instance of finder throw $app['nn.finder']., (*5)
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., (*6)
<?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());
}
...
<?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());
}
...
<?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());
}
...
<?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());
}
...
<?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., (*7)
<?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());
}
...