public function findTemplates(string $path, string $pattern = null, string $format = 'twig')
    {
        // Use glob() if possible
        if (false === strpos($path, '/**/') && (\defined('GLOB_BRACE') || false === strpos($path, '{'))) {
            $templates = glob(rtrim($path, '/').'/*.{'.$format.'}', \defined('GLOB_BRACE') ? GLOB_BRACE : 0);

            return null === $pattern ? $templates : preg_grep('$'.$pattern.'$', $templates);
        }

        $pattern = rtrim($path, '/').(null === $pattern ? '' : $pattern).'/*.{'.$format.'}';

        $finder = new Finder();
        $regex = Glob::toRegex($pattern);

        // All files in the given template folder
        $filesIterator = $finder->files()->followLinks()->sortByName()->in(\dirname($pattern));

        // Match the actual regex and filter the files
        $filesIterator = $filesIterator->filter(
            function (\SplFileInfo $info) use ($regex) {
                $path = $info->getPathname();

                return preg_match($regex, $path) && $info->isFile();
            }
        );

        $files = iterator_to_array($filesIterator);

        return array_keys($files);
    }