GPL-3.0
- PHP version 5.4
- Composer
- SILEX
- Synfony Finder
- PHP Unit 4.7.x (optional)
- PHP_CodeSniffer 2.x (optional)
composer require nachonerd/silex-finder-provider
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'].
<?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());
}
...
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());
}
...
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());
}
...