Skip to content

Commit

Permalink
Add annotation reader to dynamically add the document annotation (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Sep 11, 2020
1 parent cd7d87f commit 57916c9
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Document/ArticleViewDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
/**
* Indexable document for articles.
*
* @Document(type="article")
* Following annotation will be set by the annotation reader if this document is used for mapping.
* Document(type="article")
*/
class ArticleViewDocument implements ArticleViewDocumentInterface
{
Expand Down
72 changes: 72 additions & 0 deletions Elasticsearch/AnnotationReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Elasticsearch;

use Doctrine\Common\Annotations\Reader;
use ONGR\ElasticsearchBundle\Annotation\Document;

class AnnotationReader implements Reader
{
/**
* @var Reader
*/
private $inner;

/**
* @var string
*/
private $articleViewDocumentClass;

public function __construct(Reader $inner, $articleViewDocumentClass)
{
$this->inner = $inner;
$this->articleViewDocumentClass = $articleViewDocumentClass;
}

public function getClassAnnotations(\ReflectionClass $class)
{
return $this->inner->getClassAnnotations($class);
}

public function getClassAnnotation(\ReflectionClass $class, $annotationName)
{
$result = $this->inner->getClassAnnotation($class, $annotationName);
if (!$result && Document::class === $annotationName && $class->getName() === $this->articleViewDocumentClass) {
$annotation = new Document();
$annotation->type = 'article';

return $annotation;
}

return $result;
}

public function getMethodAnnotations(\ReflectionMethod $method)
{
return $this->inner->getMethodAnnotations($method);
}

public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
{
return $this->inner->getMethodAnnotation($method, $annotationName);
}

public function getPropertyAnnotations(\ReflectionProperty $property)
{
return $this->inner->getPropertyAnnotations($property);
}

public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
{
return $this->inner->getPropertyAnnotation($property, $annotationName);
}
}
9 changes: 8 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@
<tag name="massive_build.builder" />
</service>


<!-- webspace settings resolver -->
<service id="sulu_bundle_article.webspace_settings_configuration_resolver"
class="Sulu\Bundle\ArticleBundle\DependencyInjection\WebspaceSettingsConfigurationResolver">
Expand All @@ -415,5 +414,13 @@

<tag name="kernel.event_subscriber" />
</service>

<!-- annotation reader -->
<service id="sulu_article.annotations.cached_reader"
decorates="es.annotations.cached_reader"
class="Sulu\Bundle\ArticleBundle\Elasticsearch\AnnotationReader">
<argument type="service" id="sulu_article.annotations.cached_reader.inner"/>
<argument type="string">%sulu_article.view_document.article.class%</argument>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Resources/doc/article-view-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use ONGR\ElasticsearchBundle\Annotation\Property;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument as SuluArticleViewDocument;

/**
* @Document(type="app_article")
* @Document(type="article")
*/
class ArticleViewDocument extends SuluArticleViewDocument
{
Expand Down
12 changes: 10 additions & 2 deletions Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,12 @@ public function testPutExtensions(
$this->assertNotEquals($article['title'], $response['title']);
$this->assertEquals($title, $response['title']);
$this->assertEquals($extensions['seo'], $response['ext']['seo']);
$this->assertEquals($extensions['excerpt'], $response['ext']['excerpt']);

// segment is only returned for sulu versions >= 2.2
$responseExcerpt = $response['ext']['excerpt'];
unset($response['segment']);

$this->assertEquals($extensions['excerpt'], $responseExcerpt);
}

public function testPutDifferentTemplate($title = 'Sulu', $description = 'Sulu is awesome')
Expand Down Expand Up @@ -1696,7 +1701,10 @@ private function findViewDocument($uuid, $locale)
/** @var Manager $manager */
$manager = $this->getContainer()->get('es.manager.default');

return $manager->find(ArticleViewDocument::class, $this->getViewDocumentId($uuid, $locale));
return $manager->find(
$this->getContainer()->getParameter('sulu_article.view_document.article.class'),
$this->getViewDocumentId($uuid, $locale)
);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Tests/Functional/Controller/ArticlePageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Ferrandini\Urlizer;
use ONGR\ElasticsearchBundle\Service\Manager;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
use Sulu\Bundle\ArticleBundle\Metadata\ArticleViewDocumentIdTrait;
Expand Down Expand Up @@ -405,7 +404,10 @@ private function findViewDocument($uuid, $locale)
/** @var Manager $manager */
$manager = $this->getContainer()->get('es.manager.default');

return $manager->find(ArticleViewDocument::class, $this->getViewDocumentId($uuid, $locale));
return $manager->find(
$this->getContainer()->getParameter('sulu_article.view_document.article.class'),
$this->getViewDocumentId($uuid, $locale)
);
}

private function getRoute($title, $page)
Expand Down
5 changes: 4 additions & 1 deletion Tests/Functional/Document/Index/ArticleIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ private function createPage($title = 'Test Page', $resourceSegment = '/test-page
*/
private function findViewDocument($uuid, $locale = null)
{
return $this->manager->find(ArticleViewDocument::class, $uuid . '-' . ($locale ? $locale : $this->locale));
return $this->manager->find(
$this->getContainer()->getParameter('sulu_article.view_document.article.class'),
$uuid . '-' . ($locale ? $locale : $this->locale)
);
}
}
29 changes: 29 additions & 0 deletions Tests/TestExtendBundle/Document/ArticleViewDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Tests\TestExtendBundle\Document;

use ONGR\ElasticsearchBundle\Annotation\Document;
use ONGR\ElasticsearchBundle\Annotation\Property;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument as SuluArticleViewDocument;

/**
* @Document(type="article")
*/
class ArticleViewDocument extends SuluArticleViewDocument
{
/**
* @var string
*
* @Property(type="text")
*/
public $article;
}
14 changes: 14 additions & 0 deletions Tests/app/config/config_extend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ sulu_route:
options:
route_schema: "/{translator.trans(\"page\")}-{object.getPageNumber()}"
parent: "{object.getParent().getRoutePath()}"

ongr_elasticsearch:
managers:
default:
mappings:
- TestExtendBundle
live:
mappings:
- TestExtendBundle

sulu_article:
documents:
article:
view: Sulu\Bundle\ArticleBundle\Tests\TestExtendBundle\Document\ArticleViewDocument
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^5.6 || ^7.0",
"sulu/sulu": "^1.6.8",
"handcraftedinthealps/elasticsearch-bundle": "^5.2.6.2",
"handcraftedinthealps/elasticsearch-bundle": "^5.2.6.4",
"handcraftedinthealps/elasticsearch-dsl": "^5.0.7.1 || ^6.2.0.1 || ^7.2.0.1",
"jackalope/jackalope": "^1.2.8 || >=1.3.3",
"jms/serializer-bundle": "^1.1 || ^2.0"
Expand Down

0 comments on commit 57916c9

Please sign in to comment.