Last active
December 18, 2023 12:39
-
-
Save stefthoen/4e6f6ca3ee0ce214663508df1b540991 to your computer and use it in GitHub Desktop.
Get files from directory using RecursiveDirectoryIterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dirs = get_directories('components'); | |
Timber::$dirname = array_merge(add_components_dir($dirs), ['views', 'svgs']); | |
function get_directories($componentsDir) | |
{ | |
$dir = get_template_directory() . '/' . $componentsDir; | |
$dirIterator = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS); | |
return array_reduce(iterator_to_array($dirIterator, true), function ($dirs, $dir) use ($componentsDir) { | |
$dirs[] = $dir->getFilename(); | |
return $dirs; | |
}, []); | |
} | |
function add_components_dir($dirs) | |
{ | |
return array_map(function ($dir) { | |
return 'components/' . $dir; | |
}, $dirs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment