- Cleaned up a bit and added some methods to give Lean/Console more flexibility
This commit is contained in:
1 parent
531f786747
commit
4bd4cddbff
7 files changed
+78
-26
No files matched your search
+39
-5
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Picea;
|
||||
|
||||
use RecursiveIteratorIterator, RecursiveDirectoryIterator;
|
||||
|
||||
class FileFetcher
|
||||
{
|
||||
protected array $folderList = [];
|
||||
@@ -12,13 +14,13 @@ class FileFetcher
|
||||
|
||||
public function __construct(?array $folderList = null) {
|
||||
if ( $folderList !== null ) {
|
||||
$this->folderList = $folderList;
|
||||
$this->addFolders($folderList);
|
||||
}
|
||||
}
|
||||
|
||||
public function addFolder(string $folder, int $order = 100) : void
|
||||
{
|
||||
$folder = rtrim($folder, DIRECTORY_SEPARATOR);
|
||||
$folder = $this->normalizeFolder($folder);
|
||||
|
||||
$this->folderList[$folder] = [
|
||||
'path' => $folder,
|
||||
@@ -28,7 +30,14 @@ class FileFetcher
|
||||
|
||||
public function addFolders(array $folderList) : void
|
||||
{
|
||||
$this->folderList = array_replace($this->folderList, $folderList);
|
||||
foreach($folderList as $folder) {
|
||||
$this->addFolder($folder['path'], $folder['order']);
|
||||
}
|
||||
}
|
||||
|
||||
protected function normalizeFolder(string $path) : string
|
||||
{
|
||||
return rtrim($path, '/\\');
|
||||
}
|
||||
|
||||
public function folderList(?array $set = null) : ?array
|
||||
@@ -40,7 +49,7 @@ class FileFetcher
|
||||
{
|
||||
usort($this->folderList, fn($a, $b) => $a['order'] <=> $b['order']);
|
||||
|
||||
foreach($this->folderList as $folder) {
|
||||
foreach($this->folderList() as $folder) {
|
||||
foreach($this->supportedExtensionList as $extension) {
|
||||
$file = $folder['path'] . DIRECTORY_SEPARATOR . "$fileName.$extension";
|
||||
$file = str_replace([ '\\', '/' ], DIRECTORY_SEPARATOR, $file);
|
||||
@@ -56,7 +65,7 @@ class FileFetcher
|
||||
}
|
||||
|
||||
# Fallback on full-path
|
||||
foreach($this->folderList as $folder) {
|
||||
foreach($this->folderList() as $folder) {
|
||||
$file = $folder['path'] . DIRECTORY_SEPARATOR . $fileName;
|
||||
$file = str_replace([ '\\', '/' ], DIRECTORY_SEPARATOR, $file);
|
||||
|
||||
@@ -72,6 +81,30 @@ class FileFetcher
|
||||
throw new \RuntimeException("Given view file `$fileName` can not be found within given folder list..");
|
||||
}
|
||||
|
||||
public function getFileList() : array
|
||||
{
|
||||
usort($this->folderList, fn($a, $b) => $a['order'] <=> $b['order']);
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach($this->folderList() as $folder) {
|
||||
$path = $folder['path'] . "/";
|
||||
$list[$path] = [];
|
||||
|
||||
if ( \file_exists($path) ) {
|
||||
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isFile() && in_array($file->getExtension(), $this->supportedExtensionList)) {
|
||||
$list[$path][] = $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getFilePath(string $fileName) : string
|
||||
{
|
||||
return $this->findFile($fileName);
|
||||
@@ -81,4 +114,5 @@ class FileFetcher
|
||||
{
|
||||
return file_get_contents($this->getFilePath($fileName));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user