private function scan_dir($folder, $files = [])
    {
        // Ensure maximum portability.
        $folder = \trailingslashit($folder);

        if (isset($this->visited[ $folder ])) {
            return $files;
        }

        // Check the target exists.
        if (\is_dir($folder)) {
            // Scan the directory for files to include.
            $contents = \scandir($folder);
            $this->visited[ $folder ] = null;
        }

        if (!empty($contents)) {
            // go through each file, adding it to the $files list.
            foreach ($contents as $file) {
                $path = $folder . $file;

                $class = \str_replace([\get_stylesheet_directory(), \get_template_directory(), '.php'], '', $path);
                $class = \trim(
                    \str_replace(['/', 'theme'], ['\\', 'Theme'], $class),
                    '\\'
                );

                if ('.' === $file || '..' === $file) {
                    continue;
                } elseif (\pathinfo($path, PATHINFO_EXTENSION) === 'php') {
                    $files[ $class ] = $path;
                } elseif (\is_dir($path)) {
                    // Sub directory, scan this dir as well.
                    $files = $this->scan_dir(\trailingslashit($path), $files);
                }
            }
        }

        return $files;
    }