Skip to content

Commit

Permalink
Unify file iteration code
Browse files Browse the repository at this point in the history
  • Loading branch information
Usbac committed Jan 2, 2025
1 parent 2a79a2a commit 40b0fed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/bootstrap/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
$absolute_theme_dir = Helper::getPath(Kernel::config('views') . "/$theme_dir");
$view_files = [];

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($absolute_theme_dir)) as $file) {
foreach (Helper::getFileIterator($absolute_theme_dir) as $file) {
if ($file->isFile()) {
$view_files[] = mb_substr($file->getPathname(), mb_strlen($absolute_theme_dir) + 1);
}
Expand Down Expand Up @@ -950,7 +950,7 @@
$zip = new ZipArchive();
$zip->open($file_path, ZipArchive::CREATE | ZipArchive::OVERWRITE);

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($absolute_path)) as $file) {
foreach (\Aurora\Core\Helper::getFileIterator($absolute_path) as $file) {
$real_path = $file->getRealPath();
$relative_path = mb_substr($real_path, mb_strlen($absolute_path) + 1);

Expand Down
2 changes: 1 addition & 1 deletion bin/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function getThemeFiles()
$absolute_theme_dir = \Aurora\Core\Helper::getPath($this->config['views'] . '/themes/' . $this->settings['theme']);
$view_files = [ '' ];

foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($absolute_theme_dir)) as $file) {
foreach (\Aurora\Core\Helper::getFileIterator($absolute_theme_dir) as $file) {
if ($file->isFile()) {
$view_files[] = mb_substr($file->getPathname(), mb_strlen($absolute_theme_dir) + 1);
}
Expand Down
18 changes: 16 additions & 2 deletions core/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public static function copy(string $source, string $destination, int $permission
mkdir($destination, $permission);
}

$dir_iterator = new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST);
$iterator = self::getFileIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $item) {
$new_path = "$destination/" . call_user_func([ $iterator, 'getSubPathname' ]);
$res = $item->isDir()
Expand All @@ -102,6 +101,21 @@ public static function copy(string $source, string $destination, int $permission
return true;
}

/**
* Returns a recursive file iterator
* @param string $path the path
* @param [int] $flags the flags
* @param [int] $mode the mode
* @return \RecursiveIteratorIterator a recursive file iterator
*/
public static function getFileIterator(string $path,
int $flags = \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO,
int $mode = \RecursiveIteratorIterator::LEAVES_ONLY,
): \RecursiveIteratorIterator
{
return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, $flags), $mode);
}

/**
* Returns the given byte size in a human readable format
* @param float $bytes the size in bytes
Expand Down

0 comments on commit 40b0fed

Please sign in to comment.