mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-09-02 02:12:37 +02:00
Refactored directory sorting
This commit is contained in:
@@ -15,6 +15,16 @@ class FinderComposer
|
||||
/** @const Application paths to be hidden */
|
||||
protected const APP_FILES = ['app', 'node_modules', 'vendor', 'index.php'];
|
||||
|
||||
/** @const Array of sort options mapped to their respective methods */
|
||||
public const SORT_METHODS = [
|
||||
'accessed' => SortMethods\Accessed::class,
|
||||
'changed' => SortMethods\Changed::class,
|
||||
'modified' => SortMethods\Modified::class,
|
||||
'name' => SortMethods\Name::class,
|
||||
'natural' => SortMethods\Natural::class,
|
||||
'type' => SortMethods\Type::class,
|
||||
];
|
||||
|
||||
/** @var Config Application config */
|
||||
protected $config;
|
||||
|
||||
@@ -50,28 +60,12 @@ class FinderComposer
|
||||
if ($sortOrder instanceof Closure) {
|
||||
$finder->sort($sortOrder);
|
||||
} else {
|
||||
switch ($sortOrder) {
|
||||
case 'accessed':
|
||||
$finder->sortByAccessedTime();
|
||||
break;
|
||||
case 'changed':
|
||||
$finder->sortByChangedTime();
|
||||
break;
|
||||
case 'modified':
|
||||
$finder->sortByModifiedTime();
|
||||
break;
|
||||
case 'name':
|
||||
$finder->sortByName();
|
||||
break;
|
||||
case 'natural':
|
||||
$finder->sortByName(true);
|
||||
break;
|
||||
case 'type':
|
||||
$finder->sortByType();
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Invalid sort option '{$sortOrder}'");
|
||||
if (! array_key_exists($sortOrder, self::SORT_METHODS)) {
|
||||
throw new RuntimeException("Invalid sort option '{$sortOrder}'");
|
||||
}
|
||||
|
||||
$sortMethod = self::SORT_METHODS[$sortOrder];
|
||||
call_user_func(new $sortMethod, $finder);
|
||||
}
|
||||
|
||||
if ($this->config->get('app.reverse_sort', false)) {
|
||||
|
20
app/Bootstrap/SortMethods/Accessed.php
Normal file
20
app/Bootstrap/SortMethods/Accessed.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class Accessed extends SortMethod
|
||||
{
|
||||
/**
|
||||
* Sort by file accessed time.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Finder $finder): void
|
||||
{
|
||||
$finder->sortByAccessedTime();
|
||||
}
|
||||
}
|
20
app/Bootstrap/SortMethods/Changed.php
Normal file
20
app/Bootstrap/SortMethods/Changed.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class Changed extends SortMethod
|
||||
{
|
||||
/**
|
||||
* Sory by file changed time.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Finder $finder): void
|
||||
{
|
||||
$finder->sortByChangedTime();
|
||||
}
|
||||
}
|
20
app/Bootstrap/SortMethods/Modified.php
Normal file
20
app/Bootstrap/SortMethods/Modified.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class Modified extends SortMethod
|
||||
{
|
||||
/**
|
||||
* Sort by file modified time.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Finder $finder): void
|
||||
{
|
||||
$finder->sortByModifiedTime();
|
||||
}
|
||||
}
|
20
app/Bootstrap/SortMethods/Name.php
Normal file
20
app/Bootstrap/SortMethods/Name.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class Name extends SortMethod
|
||||
{
|
||||
/**
|
||||
* Sort by file name.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Finder $finder): void
|
||||
{
|
||||
$finder->sortByName();
|
||||
}
|
||||
}
|
20
app/Bootstrap/SortMethods/Natural.php
Normal file
20
app/Bootstrap/SortMethods/Natural.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class Natural extends SortMethod
|
||||
{
|
||||
/**
|
||||
* Sort by (natural) file name.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Finder $finder): void
|
||||
{
|
||||
$finder->sortByName(true);
|
||||
}
|
||||
}
|
17
app/Bootstrap/SortMethods/SortMethod.php
Normal file
17
app/Bootstrap/SortMethods/SortMethod.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
abstract class SortMethod
|
||||
{
|
||||
/**
|
||||
* Run the sort method.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function __invoke(Finder $finder): void;
|
||||
}
|
20
app/Bootstrap/SortMethods/Type.php
Normal file
20
app/Bootstrap/SortMethods/Type.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap\SortMethods;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class Type extends SortMethod
|
||||
{
|
||||
/**
|
||||
* Sory by file type.
|
||||
*
|
||||
* @param \Symfony\Component\Finder\Finder $finder
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Finder $finder): void
|
||||
{
|
||||
$finder->sortByType();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user