Skip to content

Commit

Permalink
introducing document_dir option (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
saimaz authored Dec 13, 2016
1 parent 5a609ee commit e0d4b6e
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 99 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function ($v) {
->end()
->arrayNode('mappings')
->info('Maps manager to the bundles. f.e. AppBundle')
->prototype('scalar')->end()
->prototype('variable')->end()
->end()
->booleanNode('force_commit')
->info('Forces commit to the elasticsearch on kernel terminate event.')
Expand Down
24 changes: 17 additions & 7 deletions Mapping/DocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ public function __construct(array $bundles)
* Formats namespace from short syntax.
*
* @param string $namespace
* @param string $documentsDirectory Directory name where documents are stored in the bundle.
*
* @return string
*/
public function getNamespace($namespace)
public function getNamespace($namespace, $documentsDirectory = null)
{
if (!$documentsDirectory) {
$documentsDirectory = self::DOCUMENT_DIR;
}

if (strpos($namespace, ':') !== false) {
list($bundle, $document) = explode(':', $namespace);
$bundle = $this->getBundleClass($bundle);
$namespace = substr($bundle, 0, strrpos($bundle, '\\')) . '\\' .
self::DOCUMENT_DIR . '\\' . $document;
$documentsDirectory . '\\' . $document;
}

return $namespace;
Expand Down Expand Up @@ -84,16 +89,21 @@ public function getBundleClass($name)
* 'SubDir\SomeObject'
* ]
*
* @param string $bundle
* @param string $bundle Bundle name. E.g. AppBundle
* @param string $documentsDirectory Directory name where documents are stored in the bundle.
*
* @return array
*/
public function getBundleDocumentClasses($bundle)
public function getBundleDocumentClasses($bundle, $documentsDirectory = null)
{
if (!$documentsDirectory) {
$documentsDirectory = self::DOCUMENT_DIR;
}

$bundleReflection = new \ReflectionClass($this->getBundleClass($bundle));

$documentDirectory = DIRECTORY_SEPARATOR . self::DOCUMENT_DIR . DIRECTORY_SEPARATOR;
$directory = dirname($bundleReflection->getFileName()) . $documentDirectory;
$documentsDirectory = DIRECTORY_SEPARATOR . $documentsDirectory . DIRECTORY_SEPARATOR;
$directory = dirname($bundleReflection->getFileName()) . $documentsDirectory;

if (!is_dir($directory)) {
return [];
Expand All @@ -108,7 +118,7 @@ public function getBundleDocumentClasses($bundle)
$documents[] = str_replace(
DIRECTORY_SEPARATOR,
'\\',
substr(strstr($file, $documentDirectory), strlen($documentDirectory), -4)
substr(strstr($file, $documentsDirectory), strlen($documentsDirectory), -4)
);
}

Expand Down
75 changes: 33 additions & 42 deletions Mapping/MetadataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ class MetadataCollector
*/
private $enableCache = false;

/**
* Bundles mappings local cache container. Could be stored as the whole bundle or as single document.
* e.g. AcmeDemoBundle, AcmeDemoBundle:Product.
*
* @var mixed
*/
private $mappings = [];

/**
* @param DocumentFinder $finder For finding documents.
* @param DocumentParser $parser For reading document annotations.
Expand All @@ -58,10 +50,6 @@ public function __construct($finder, $parser, $cache = null)
$this->finder = $finder;
$this->parser = $parser;
$this->cache = $cache;

if ($this->cache) {
$this->mappings = $this->cache->fetch('ongr.metadata.mappings');
}
}

/**
Expand All @@ -83,8 +71,13 @@ public function setEnableCache($enableCache)
public function getMappings(array $bundles)
{
$output = [];
foreach ($bundles as $bundle) {
$mappings = $this->getBundleMapping($bundle);
foreach ($bundles as $name => $bundleConfig) {
// Backward compatibility hack for support.
if (!is_array($bundleConfig)) {
$name = $bundleConfig;
$bundleConfig = [];
}
$mappings = $this->getBundleMapping($name, $bundleConfig);

$alreadyDefinedTypes = array_intersect_key($mappings, $output);
if (count($alreadyDefinedTypes)) {
Expand All @@ -105,30 +98,38 @@ public function getMappings(array $bundles)
* Searches for documents in the bundle and tries to read them.
*
* @param string $name
* @param array $config Bundle configuration
*
* @return array Empty array on containing zero documents.
*/
public function getBundleMapping($name)
public function getBundleMapping($name, $config = [])
{
if (!is_string($name)) {
throw new \LogicException('getBundleMapping() in the Metadata collector expects a string argument only!');
}

if (isset($this->mappings[$name])) {
return $this->mappings[$name];
$cacheName = 'ongr.metadata.mapping.' . md5($name.serialize($config));

$this->enableCache && $mappings = $this->cache->contains($cacheName);

if (isset($mappings)) {
return $mappings;
}

$mappings = [];
$documentDir = isset($config['document_dir']) ? $config['document_dir'] : DocumentFinder::DOCUMENT_DIR;

// Handle the case when single document mapping requested
// Usage od ":" in name is deprecated. This if is only for BC.
if (strpos($name, ':') !== false) {
list($bundle, $documentClass) = explode(':', $name);
$documents = $this->finder->getBundleDocumentClasses($bundle);
$documents = in_array($documentClass, $documents) ? [$documentClass] : [];
} else {
$documents = $this->finder->getBundleDocumentClasses($name);
$documents = $this->finder->getBundleDocumentClasses($name, $documentDir);
$bundle = $name;
}

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

Expand All @@ -140,7 +141,7 @@ public function getBundleMapping($name)
foreach ($documents as $document) {
$documentReflection = new \ReflectionClass(
$bundleNamespace .
'\\' . DocumentFinder::DOCUMENT_DIR .
'\\' . $documentDir .
'\\' . $document
);

Expand All @@ -162,7 +163,7 @@ public function getBundleMapping($name)
}
}

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

return $mappings;
}
Expand Down Expand Up @@ -235,10 +236,10 @@ function ($value) {
*/
public function getClientAnalysis(array $bundles, $analysisConfig = [])
{
$cacheName = 'ongr.metadata.analysis.'.md5(implode(',', $bundles));
$typesAnalysis = $this->cache->fetch($cacheName);
$cacheName = 'ongr.metadata.analysis.'.md5(serialize($bundles));
$this->enableCache && $typesAnalysis = $this->cache->fetch($cacheName);

if ($typesAnalysis) {
if (isset($typesAnalysis)) {
return $typesAnalysis;
}

Expand Down Expand Up @@ -279,7 +280,7 @@ public function getClientAnalysis(array $bundles, $analysisConfig = [])
}
}

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

return $typesAnalysis;
}
Expand Down Expand Up @@ -335,30 +336,20 @@ private function getDocumentReflectionMapping(\ReflectionClass $reflectionClass)
*/
public function getMapping($namespace)
{
$cacheName = 'ongr.metadata.document.'.md5($namespace);

$namespace = $this->getClassName($namespace);
$this->enableCache && $mapping = $this->cache->fetch($cacheName);

if (isset($this->mappings[$namespace])) {
return $this->mappings[$namespace];
if (isset($mapping)) {
return $mapping;
}

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

return $mapping;
}
$this->enableCache && $this->cache->save($cacheName, $mapping);

/**
* Adds metadata information to the cache storage.
*
* @param string $bundle
* @param array $mapping
*/
private function cacheBundle($bundle, array $mapping)
{
if ($this->enableCache) {
$this->mappings[$bundle] = $mapping;
$this->cache->save('ongr.metadata.mappings', $this->mappings);
}
return $mapping;
}

/**
Expand Down
23 changes: 8 additions & 15 deletions Resources/doc/configuration.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuration tree

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

```yml
ongr_elasticsearch:
Expand Down Expand Up @@ -28,24 +28,17 @@ ongr_elasticsearch:
index_name: ongr-default
settings:
refresh_interval: -1
number_of_replicas: 1
analysis:
analyzer:
- pathAnalyzer
tokenizer:
- pathTokenizer
number_of_replicas: 0
number_of_shards: 1
logger: true #default %kernel.debug%
mappings:
- AcmeBarBundle #Scans all bundle documents
foo:
custom:
index:
hosts:
- 10.0.0.1:9200 #default 127.0.0.1:9200
index_name: ongr-bar
settings:
refresh_interval: 1 #default -1
number_of_replicas: 0 #default 0
analysis:
filter:
- incremental_filter
index_name: ongr-custom
mappings:
AcmeBundle:
document_dir: Document
```
Loading

0 comments on commit e0d4b6e

Please sign in to comment.