36 lines
909 B
PHP
36 lines
909 B
PHP
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace Grav\Theme;
|
||
|
|
||
|
use Grav\Common\Twig\Extension\GravExtension;
|
||
|
|
||
|
class DirFilesExtension extends GravExtension
|
||
|
{
|
||
|
public function getName(): string
|
||
|
{
|
||
|
return 'DirFilesExtension';
|
||
|
}
|
||
|
|
||
|
public function getFunctions(): array
|
||
|
{
|
||
|
return [new \Twig_SimpleFunction('dirFiles', [$this, 'getDirFiles'])];
|
||
|
}
|
||
|
|
||
|
public function getDirFiles(string $dir, string $exts): array
|
||
|
{
|
||
|
$res = array();
|
||
|
$dir = '..'.DIRECTORY_SEPARATOR.(str_starts_with($dir, '/') ? substr($dir, 1) : $dir);
|
||
|
if (file_exists($dir) && $dirHandle = opendir($dir))
|
||
|
{
|
||
|
while ($entry = readdir($dirHandle)) {
|
||
|
if (preg_match('/^[a-zA-Z0-9-_][a-zA-Z0-9-_.]*\.('.$exts.')$/', $entry)) {
|
||
|
$res[] = $dir.DIRECTORY_SEPARATOR.$entry;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $res;
|
||
|
}
|
||
|
}
|