Skip to content

Commit e0d4b6e

Browse files
authored
introducing document_dir option (#724)
1 parent 5a609ee commit e0d4b6e

File tree

9 files changed

+159
-99
lines changed

9 files changed

+159
-99
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function ($v) {
181181
->end()
182182
->arrayNode('mappings')
183183
->info('Maps manager to the bundles. f.e. AppBundle')
184-
->prototype('scalar')->end()
184+
->prototype('variable')->end()
185185
->end()
186186
->booleanNode('force_commit')
187187
->info('Forces commit to the elasticsearch on kernel terminate event.')

Mapping/DocumentFinder.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ public function __construct(array $bundles)
4040
* Formats namespace from short syntax.
4141
*
4242
* @param string $namespace
43+
* @param string $documentsDirectory Directory name where documents are stored in the bundle.
4344
*
4445
* @return string
4546
*/
46-
public function getNamespace($namespace)
47+
public function getNamespace($namespace, $documentsDirectory = null)
4748
{
49+
if (!$documentsDirectory) {
50+
$documentsDirectory = self::DOCUMENT_DIR;
51+
}
52+
4853
if (strpos($namespace, ':') !== false) {
4954
list($bundle, $document) = explode(':', $namespace);
5055
$bundle = $this->getBundleClass($bundle);
5156
$namespace = substr($bundle, 0, strrpos($bundle, '\\')) . '\\' .
52-
self::DOCUMENT_DIR . '\\' . $document;
57+
$documentsDirectory . '\\' . $document;
5358
}
5459

5560
return $namespace;
@@ -84,16 +89,21 @@ public function getBundleClass($name)
8489
* 'SubDir\SomeObject'
8590
* ]
8691
*
87-
* @param string $bundle
92+
* @param string $bundle Bundle name. E.g. AppBundle
93+
* @param string $documentsDirectory Directory name where documents are stored in the bundle.
8894
*
8995
* @return array
9096
*/
91-
public function getBundleDocumentClasses($bundle)
97+
public function getBundleDocumentClasses($bundle, $documentsDirectory = null)
9298
{
99+
if (!$documentsDirectory) {
100+
$documentsDirectory = self::DOCUMENT_DIR;
101+
}
102+
93103
$bundleReflection = new \ReflectionClass($this->getBundleClass($bundle));
94104

95-
$documentDirectory = DIRECTORY_SEPARATOR . self::DOCUMENT_DIR . DIRECTORY_SEPARATOR;
96-
$directory = dirname($bundleReflection->getFileName()) . $documentDirectory;
105+
$documentsDirectory = DIRECTORY_SEPARATOR . $documentsDirectory . DIRECTORY_SEPARATOR;
106+
$directory = dirname($bundleReflection->getFileName()) . $documentsDirectory;
97107

98108
if (!is_dir($directory)) {
99109
return [];
@@ -108,7 +118,7 @@ public function getBundleDocumentClasses($bundle)
108118
$documents[] = str_replace(
109119
DIRECTORY_SEPARATOR,
110120
'\\',
111-
substr(strstr($file, $documentDirectory), strlen($documentDirectory), -4)
121+
substr(strstr($file, $documentsDirectory), strlen($documentsDirectory), -4)
112122
);
113123
}
114124

Mapping/MetadataCollector.php

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ class MetadataCollector
4040
*/
4141
private $enableCache = false;
4242

43-
/**
44-
* Bundles mappings local cache container. Could be stored as the whole bundle or as single document.
45-
* e.g. AcmeDemoBundle, AcmeDemoBundle:Product.
46-
*
47-
* @var mixed
48-
*/
49-
private $mappings = [];
50-
5143
/**
5244
* @param DocumentFinder $finder For finding documents.
5345
* @param DocumentParser $parser For reading document annotations.
@@ -58,10 +50,6 @@ public function __construct($finder, $parser, $cache = null)
5850
$this->finder = $finder;
5951
$this->parser = $parser;
6052
$this->cache = $cache;
61-
62-
if ($this->cache) {
63-
$this->mappings = $this->cache->fetch('ongr.metadata.mappings');
64-
}
6553
}
6654

6755
/**
@@ -83,8 +71,13 @@ public function setEnableCache($enableCache)
8371
public function getMappings(array $bundles)
8472
{
8573
$output = [];
86-
foreach ($bundles as $bundle) {
87-
$mappings = $this->getBundleMapping($bundle);
74+
foreach ($bundles as $name => $bundleConfig) {
75+
// Backward compatibility hack for support.
76+
if (!is_array($bundleConfig)) {
77+
$name = $bundleConfig;
78+
$bundleConfig = [];
79+
}
80+
$mappings = $this->getBundleMapping($name, $bundleConfig);
8881

8982
$alreadyDefinedTypes = array_intersect_key($mappings, $output);
9083
if (count($alreadyDefinedTypes)) {
@@ -105,30 +98,38 @@ public function getMappings(array $bundles)
10598
* Searches for documents in the bundle and tries to read them.
10699
*
107100
* @param string $name
101+
* @param array $config Bundle configuration
108102
*
109103
* @return array Empty array on containing zero documents.
110104
*/
111-
public function getBundleMapping($name)
105+
public function getBundleMapping($name, $config = [])
112106
{
113107
if (!is_string($name)) {
114108
throw new \LogicException('getBundleMapping() in the Metadata collector expects a string argument only!');
115109
}
116110

117-
if (isset($this->mappings[$name])) {
118-
return $this->mappings[$name];
111+
$cacheName = 'ongr.metadata.mapping.' . md5($name.serialize($config));
112+
113+
$this->enableCache && $mappings = $this->cache->contains($cacheName);
114+
115+
if (isset($mappings)) {
116+
return $mappings;
119117
}
120118

119+
$mappings = [];
120+
$documentDir = isset($config['document_dir']) ? $config['document_dir'] : DocumentFinder::DOCUMENT_DIR;
121+
121122
// Handle the case when single document mapping requested
123+
// Usage od ":" in name is deprecated. This if is only for BC.
122124
if (strpos($name, ':') !== false) {
123125
list($bundle, $documentClass) = explode(':', $name);
124126
$documents = $this->finder->getBundleDocumentClasses($bundle);
125127
$documents = in_array($documentClass, $documents) ? [$documentClass] : [];
126128
} else {
127-
$documents = $this->finder->getBundleDocumentClasses($name);
129+
$documents = $this->finder->getBundleDocumentClasses($name, $documentDir);
128130
$bundle = $name;
129131
}
130132

131-
$mappings = [];
132133
$bundleNamespace = $this->finder->getBundleClass($bundle);
133134
$bundleNamespace = substr($bundleNamespace, 0, strrpos($bundleNamespace, '\\'));
134135

@@ -140,7 +141,7 @@ public function getBundleMapping($name)
140141
foreach ($documents as $document) {
141142
$documentReflection = new \ReflectionClass(
142143
$bundleNamespace .
143-
'\\' . DocumentFinder::DOCUMENT_DIR .
144+
'\\' . $documentDir .
144145
'\\' . $document
145146
);
146147

@@ -162,7 +163,7 @@ public function getBundleMapping($name)
162163
}
163164
}
164165

165-
$this->cacheBundle($name, $mappings);
166+
$this->enableCache && $this->cache->save($cacheName, $mappings);
166167

167168
return $mappings;
168169
}
@@ -235,10 +236,10 @@ function ($value) {
235236
*/
236237
public function getClientAnalysis(array $bundles, $analysisConfig = [])
237238
{
238-
$cacheName = 'ongr.metadata.analysis.'.md5(implode(',', $bundles));
239-
$typesAnalysis = $this->cache->fetch($cacheName);
239+
$cacheName = 'ongr.metadata.analysis.'.md5(serialize($bundles));
240+
$this->enableCache && $typesAnalysis = $this->cache->fetch($cacheName);
240241

241-
if ($typesAnalysis) {
242+
if (isset($typesAnalysis)) {
242243
return $typesAnalysis;
243244
}
244245

@@ -279,7 +280,7 @@ public function getClientAnalysis(array $bundles, $analysisConfig = [])
279280
}
280281
}
281282

282-
$this->cacheBundle($cacheName, $typesAnalysis);
283+
$this->enableCache && $this->cache->save($cacheName, $mappings);
283284

284285
return $typesAnalysis;
285286
}
@@ -335,30 +336,20 @@ private function getDocumentReflectionMapping(\ReflectionClass $reflectionClass)
335336
*/
336337
public function getMapping($namespace)
337338
{
339+
$cacheName = 'ongr.metadata.document.'.md5($namespace);
340+
338341
$namespace = $this->getClassName($namespace);
342+
$this->enableCache && $mapping = $this->cache->fetch($cacheName);
339343

340-
if (isset($this->mappings[$namespace])) {
341-
return $this->mappings[$namespace];
344+
if (isset($mapping)) {
345+
return $mapping;
342346
}
343347

344348
$mapping = $this->getDocumentReflectionMapping(new \ReflectionClass($namespace));
345-
$this->cacheBundle($namespace, $mapping);
346349

347-
return $mapping;
348-
}
350+
$this->enableCache && $this->cache->save($cacheName, $mapping);
349351

350-
/**
351-
* Adds metadata information to the cache storage.
352-
*
353-
* @param string $bundle
354-
* @param array $mapping
355-
*/
356-
private function cacheBundle($bundle, array $mapping)
357-
{
358-
if ($this->enableCache) {
359-
$this->mappings[$bundle] = $mapping;
360-
$this->cache->save('ongr.metadata.mappings', $this->mappings);
361-
}
352+
return $mapping;
362353
}
363354

364355
/**

Resources/doc/configuration.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration tree
22

3-
Here's an example of full configuration with all options:
3+
Here's an example of full configuration with all possible options including default values:
44

55
```yml
66
ongr_elasticsearch:
@@ -28,24 +28,17 @@ ongr_elasticsearch:
2828
index_name: ongr-default
2929
settings:
3030
refresh_interval: -1
31-
number_of_replicas: 1
32-
analysis:
33-
analyzer:
34-
- pathAnalyzer
35-
tokenizer:
36-
- pathTokenizer
31+
number_of_replicas: 0
32+
number_of_shards: 1
3733
logger: true #default %kernel.debug%
3834
mappings:
3935
- AcmeBarBundle #Scans all bundle documents
40-
foo:
36+
custom:
4137
index:
4238
hosts:
4339
- 10.0.0.1:9200 #default 127.0.0.1:9200
44-
index_name: ongr-bar
45-
settings:
46-
refresh_interval: 1 #default -1
47-
number_of_replicas: 0 #default 0
48-
analysis:
49-
filter:
50-
- incremental_filter
40+
index_name: ongr-custom
41+
mappings:
42+
AcmeBundle:
43+
document_dir: Document
5144
```

0 commit comments

Comments
 (0)