Skip to content

Commit

Permalink
Removed unnecessary code from extension class
Browse files Browse the repository at this point in the history
Added data validity checks in IndexManager::reindex() method
  • Loading branch information
pmishev committed Nov 19, 2015
1 parent 562198c commit d65aa64
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
40 changes: 1 addition & 39 deletions DependencyInjection/SineflowElasticsearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,12 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('sfes.document_dir', $config['document_dir']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');

$container->setParameter('sfes.document_dir', $config['document_dir']);
$container->setParameter('sfes.connections', $config['connections']);
$container->setParameter('sfes.indices', $config['indices']);

$this->addDocumentsResource($config, $container);
}

/**
* Adds document directory resource.
* This is done, so if any entity definition is changed, the cache can be rebuilt
*
* @param array $config
* @param ContainerBuilder $container
*/
private function addDocumentsResource(array $config, ContainerBuilder $container)
{
$watchedBundles = [];
foreach ($config['indices'] as $index) {
foreach ($index['types'] as $typeEntity) {
// Get the bundle name from the entity class short syntax (e.g. AppBundle:Product)
$bundleName = substr($typeEntity, 0, strpos($typeEntity, ':'));
$watchedBundles[$bundleName] = true;
}
}
// Get the bundles' classes from the container registered bundles
$watchedBundles = array_intersect_key(
$container->getParameter('kernel.bundles'),
$watchedBundles
);

// TODO: once the metadata is no longer gathered during container compilation,
// figure out another way of invalidating the cache when a document in the watched bundles' document dirs is changed
// because it won't be the container cache that needs invalidation, but the metadata cache, which I guess would be separate


foreach ($watchedBundles as $name => $class) {
$bundle = new \ReflectionClass($class);
$dir = dirname($bundle->getFileName()) . DIRECTORY_SEPARATOR . $config['document_dir'];
$container->addResource(new DirectoryResource($dir));
}
}

}
30 changes: 24 additions & 6 deletions Manager/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,33 @@ public function rebuildIndex($deleteOld = false)
*/
public function reindex($documentClass, $id)
{
$documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass);

$dataProvider = $this->getDataProvider($documentClass);
$document = $dataProvider->getDocument($id);
// TODO: verify that the id of the returned document matches $id
if (is_array($document)) {
$documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass);
$this->persistRaw($documentMetadata->getType(), $document);
} else {
$this->persist($document);

switch (true) {
case $document instanceof DocumentInterface:
if (get_class($document) !== $documentMetadata->getClassName()) {
throw new \RuntimeException(sprintf('Document must be "%s", but "%s" was returned from data provider', $documentMetadata->getClassName(), get_class($document)));
}
$this->persist($document);
break;

case is_array($document):
if (!isset($document['_id'])) {
throw new \RuntimeException(sprintf('The returned document array must include an "_id" field: (%s)', serialize($document)));
}
if ($document['_id'] !== $id) {
throw new \RuntimeException(sprintf('The document id must be "%s", but "%s" was returned from data provider', $id, $document['_id']));
}
$this->persistRaw($documentMetadata->getType(), $document);
break;

default:
throw new \RuntimeException('Document must be either a DocumentInterface instance or an array with raw data');
}

$this->commit();
}

Expand Down
2 changes: 1 addition & 1 deletion Mapping/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private function getProperties(\ReflectionClass $documentReflection, array $inde
foreach ($this->languageProvider->getLanguages() as $language) {
$mapping[$propertyAnnotation->name . $this->languageSeparator . $language] = $this->getPropertyMapping($propertyAnnotation, $language, $indexAnalyzers);
}
// TODO: This is a temporary hardcode. The application should decide whether it wants to use a default field at all and set its mapping on a global base (or per property?)
// TODO: The application should decide whether it wants to use a default field at all and set its mapping on a global base (or per property?)
// The custom mapping from the application should be set here, using perhaps some kind of decorator
$mapping[$propertyAnnotation->name . $this->languageSeparator . MLProperty::DEFAULT_LANG_SUFFIX] = [
'type' => 'string',
Expand Down

0 comments on commit d65aa64

Please sign in to comment.