Skip to content

Instantly share code, notes, and snippets.

@stefthoen
Last active December 18, 2023 12:39
Show Gist options
  • Save stefthoen/4e6f6ca3ee0ce214663508df1b540991 to your computer and use it in GitHub Desktop.
Save stefthoen/4e6f6ca3ee0ce214663508df1b540991 to your computer and use it in GitHub Desktop.
Get files from directory using RecursiveDirectoryIterator
<?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