Completed
Pull Request — master (#500)
by
unknown
04:19
created

DocumentFinder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Mapping;
13
14
/**
15
 * Finds documents in bundles.
16
 */
17
class DocumentFinder
18
{
19
    /**
20
     * @var array
21
     */
22
    private $bundles;
23
24
    /**
25
     * @var string Directory in bundle to load documents from.
26
     */
27
    const DOCUMENT_DIR = 'Document';
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param array $bundles Parameter kernel.bundles from service container.
33
     */
34
    public function __construct(array $bundles)
35
    {
36
        $this->bundles = $bundles;
37
    }
38
39
    /**
40
     * Formats namespace from short syntax.
41
     *
42
     * @param string $namespace
43
     *
44
     * @return string
45
     */
46
    public function getNamespace($namespace)
47
    {
48
        if (strpos($namespace, ':') !== false) {
49
            list($bundle, $document) = explode(':', $namespace);
50
            $bundle = $this->getBundleClass($bundle);
51
            $namespace = substr($bundle, 0, strrpos($bundle, '\\')) . '\\' .
52
                self::DOCUMENT_DIR . '\\' . $document;
53
        }
54
55
        return $namespace;
56
    }
57
58
    /**
59
     * Returns bundle class namespace else throws an exception.
60
     *
61
     * @param string $name
62
     *
63
     * @return string
64
     *
65
     * @throws \LogicException
66
     */
67
    public function getBundleClass($name)
68
    {
69
        if (array_key_exists($name, $this->bundles)) {
70
            return $this->bundles[$name];
71
        }
72
73
        throw new \LogicException(sprintf('Bundle \'%s\' does not exist.', $name));
74
    }
75
76
    /**
77
     * Returns bundle document paths.
78
     *
79
     * @param string $bundle
80
     *
81
     * @return array
82
     */
83
    public function getBundleDocumentPaths($bundle)
84
    {
85
        $bundleReflection = new \ReflectionClass($this->getBundleClass($bundle));
86
87
        $path = dirname($bundleReflection->getFileName()) .
88
            DIRECTORY_SEPARATOR .
89
            self::DOCUMENT_DIR .
90
            DIRECTORY_SEPARATOR .
91
            '*.php';
92
93
        return $this->rglob($path);
94
    }
95
96
    /**
97
     * Recusive glob
98
     * @param $pattern
99
     * @param int $flags
100
     * @return array
101
     */
102
    private function rglob($pattern, $flags = 0)
103
    {
104
        $files = glob($pattern, $flags);
105
        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
106
            $files = array_merge($files, $this->rglob($dir . '/' . basename($pattern), $flags));
107
        }
108
        return $files;
109
    }
110
}
111