Completed
Push — master ( 0c53bc...e1860c )
by Nikola
05:26
created

QuerySourcesIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\Twig\CacheWarmer;
11
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\HttpKernel\KernelInterface;
14
15
/**
16
 * Class QuerySourcesIterator
17
 *
18
 * Iterator for all query resources in bundles and in the application Resources/query directory.
19
 *
20
 * @package RunOpenCode\Bundle\QueryResourcesLoader\Twig
21
 */
22
class QuerySourcesIterator implements \IteratorAggregate
23
{
24
    private $kernel;
25
    private $rootDir;
26
    private $queries;
27
    private $paths;
28
29
    /**
30
     * @param KernelInterface $kernel  A KernelInterface instance
31
     * @param string          $rootDir The directory where global query sources can be stored
32
     * @param array           $paths   Additional Twig paths to warm
33
     */
34 1
    public function __construct(KernelInterface $kernel, $rootDir, array $paths = array())
35
    {
36 1
        $this->kernel = $kernel;
37 1
        $this->rootDir = $rootDir;
38 1
        $this->paths = $paths;
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function getIterator()
45
    {
46 1
        if (null !== $this->queries) {
47 1
            return $this->queries;
48
        }
49
50 1
        $this->queries = $this->findQuerySourcesInDirectory($this->rootDir.'/Resources/query');
51
52 1
        foreach ($this->kernel->getBundles() as $bundle) {
53
54 1
            $name = $bundle->getName();
55
56 1
            if ('Bundle' === substr($name, -6)) {
57 1
                $name = substr($name, 0, -6);
58
            }
59
60 1
            $this->queries = array_merge(
61 1
                $this->queries,
62 1
                $this->findQuerySourcesInDirectory($bundle->getPath().'/Resources/query', $name),
63 1
                $this->findQuerySourcesInDirectory($this->rootDir.'/'.$bundle->getName().'/query', $name)
64
            );
65
        }
66
67 1
        foreach ($this->paths as $dir => $namespace) {
68 1
            $this->queries = array_merge($this->queries, $this->findQuerySourcesInDirectory($dir, $namespace));
69
        }
70
71 1
        return $this->queries = new \ArrayIterator(array_unique($this->queries));
72
    }
73
74
    /**
75
     * Find query sources in the given directory.
76
     *
77
     * @param string      $dir       The directory where to look for query sources
78
     * @param string|null $namespace The query source namespace
79
     *
80
     * @return array
81
     */
82 1
    private function findQuerySourcesInDirectory($dir, $namespace = null)
83
    {
84 1
        if (!is_dir($dir)) {
85 1
            return array();
86
        }
87
88 1
        $templates = array();
89
90
        /**
91
         * @var \Symfony\Component\Finder\SplFileInfo[] $files
92
         */
93 1
        $files = Finder::create()->files()->followLinks()->in($dir);
94
95 1
        foreach ($files as $file) {
96 1
            $templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname());
97
        }
98
99 1
        return $templates;
100
    }
101
}
102