Refactored directory sorting

This commit is contained in:
Chris Kankiewicz
2019-12-25 23:48:53 -07:00
parent 817adde706
commit c82f3d6cde
8 changed files with 152 additions and 21 deletions

View File

@@ -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)) {

View 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();
}
}

View 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();
}
}

View 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();
}
}

View 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();
}
}

View 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);
}
}

View 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;
}

View 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();
}
}