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 a list of bundle document classes. |
78
|
|
|
* |
79
|
|
|
* Example output: |
80
|
|
|
* |
81
|
|
|
* [ |
82
|
|
|
* 'Category', |
83
|
|
|
* 'Product', |
84
|
|
|
* 'SubDir\SomeObject' |
85
|
|
|
* ] |
86
|
|
|
* |
87
|
|
|
* @param string $bundle |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getBundleDocumentClasses($bundle) |
92
|
|
|
{ |
93
|
|
|
$bundleReflection = new \ReflectionClass($this->getBundleClass($bundle)); |
94
|
|
|
|
95
|
|
|
$documentDirectory = DIRECTORY_SEPARATOR . self::DOCUMENT_DIR . DIRECTORY_SEPARATOR; |
96
|
|
|
$directory = dirname($bundleReflection->getFileName()) . $documentDirectory; |
97
|
|
|
|
98
|
|
|
if (!is_dir($directory)) { |
99
|
|
|
return []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); |
103
|
|
|
$files = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH); |
104
|
|
|
|
105
|
|
|
$documents = []; |
106
|
|
|
|
107
|
|
|
foreach ($files as $file => $v) { |
108
|
|
|
$documents[] = str_replace( |
109
|
|
|
DIRECTORY_SEPARATOR, |
110
|
|
|
'\\', |
111
|
|
|
substr(strstr($file, $documentDirectory), strlen($documentDirectory), -4) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $documents; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|