TemplateFinder::findTemplatesInFolder()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php
2
3
namespace JaySDe\HandlebarsBundle\CacheWarmer;
4
5
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Templating\TemplateNameParserInterface;
8
use Symfony\Component\Templating\TemplateReferenceInterface;
9
10
/**
11
 * Finds all the templates.
12
 */
13
class TemplateFinder implements TemplateFinderInterface
14
{
15
    private $parser;
16
    private $paths;
17
    private $templates;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param TemplateNameParserInterface $parser  A TemplateNameParserInterface instance
23
     * @param array                       $paths The directory where global templates can be stored
24
     */
25 2
    public function __construct(TemplateNameParserInterface $parser, $paths = [])
26
    {
27 2
        $this->parser = $parser;
28 2
        $this->paths = $paths;
29 2
    }
30
31
    /**
32
     * Find all the templates in the bundle and in the kernel Resources folder.
33
     *
34
     * @return TemplateReferenceInterface[]
35
     */
36 2
    public function findAllTemplates()
37
    {
38 2
        if (null !== $this->templates) {
39 1
            return $this->templates;
40
        }
41
42 2
        $templates = array();
43
44 2
        foreach ($this->paths as $dir) {
45 2
            $templates = array_merge($templates, $this->findTemplatesInFolder($dir));
46
        }
47
48 2
        return $this->templates = $templates;
49
    }
50
51
    /**
52
     * Find templates in the given directory.
53
     *
54
     * @param string $dir The folder where to look for templates
55
     *
56
     * @return TemplateReferenceInterface[]
57
     */
58 2
    private function findTemplatesInFolder($dir)
59
    {
60 2
        $templates = array();
61
62 2
        if (is_dir($dir)) {
63 2
            $finder = new Finder();
64 2
            foreach ($finder->files()->followLinks()->in($dir) as $file) {
65 2
                $template = $this->parser->parse($file->getRelativePathname());
66 2
                if (false !== $template) {
67 2
                    $templates[] = $template;
68
                }
69
            }
70
        }
71
72 2
        return $templates;
73
    }
74
}
75