Skip to content

Commit 788d90f

Browse files
authored
Add support for "App" mappings without a Bundle class (#31)
1 parent 9e0702a commit 788d90f

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/composer.lock
77
/var/cache/
88
.phpunit.result.cache
9+
/.idea/

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getConfigTreeBuilder()
4747
'If profiler is disabled the tracer service will be disabled as well.'
4848
)
4949
->end()
50+
->scalarNode('app_root_class')->defaultValue(\class_exists('App\\AppBundle') ? null : 'App\\Kernel')->end()
5051
->append($this->getAnalysisNode())
5152
->append($this->getManagersNode())
5253
->end();

DependencyInjection/ONGRElasticsearchExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function load(array $configs, ContainerBuilder $container)
4949
$container->setParameter('es.cache', $config['cache']);
5050
$container->setParameter('es.analysis', $config['analysis']);
5151
$container->setParameter('es.managers', $managers);
52+
$container->setParameter('es.app_root_class', $config['app_root_class'] ?? null);
5253
$definition = new Definition(
5354
'ONGR\ElasticsearchBundle\Service\ManagerFactory',
5455
[

Mapping/DocumentFinder.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,20 @@ class DocumentFinder
3030
* Constructor.
3131
*
3232
* @param array $bundles Parameter kernel.bundles from service container.
33+
* @param string $appRootClass A class that has to be in the app's root folder.
34+
* By default, the app's kernel class is used.
35+
* This is to ensure the fake bundle "App" can be used for mappings.
36+
* It is not a perfect solution but the easiest.
3337
*/
34-
public function __construct(array $bundles)
38+
public function __construct(array $bundles, ?string $appRootClass = null)
3539
{
3640
$this->documentDir = 'Document';
3741
$this->bundles = $bundles;
42+
43+
if ($appRootClass && \class_exists($appRootClass) &&
44+
!isset($this->bundles['App']) && !isset($this->bundles['AppBundle'])) {
45+
$this->bundles['App'] = $appRootClass;
46+
}
3847
}
3948

4049
/**

Resources/config/services.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
parameters:
2+
es.app_root_class: 'App\Kernel'
23
es.logging.path: "%kernel.logs_dir%/elasticsearch_%kernel.environment%.log"
34

45
services:
@@ -27,7 +28,7 @@ services:
2728
es.document_finder:
2829
class: 'ONGR\ElasticsearchBundle\Mapping\DocumentFinder'
2930
public: true
30-
arguments: ["%kernel.bundles%"]
31+
arguments: ["%kernel.bundles%", "%es.app_root_class%"]
3132

3233
es.document_parser:
3334
class: 'ONGR\ElasticsearchBundle\Mapping\DocumentParser'

Resources/doc/mapping.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
Elasticsearch bundle requires mapping definitions for it to work with complex operations,
44
like insert and update documents, do a full-text search, etc.
55

6+
### App mapping
7+
8+
In order for the app's classes to be mapped a fake bundle with the name `App` is added. This is not a perfect solution but was the easiest to implement and add support for it.
9+
10+
It requires a class in the project's source root folder.
11+
12+
By default, the `App\Kernel` class is used. This class can be changed to any other class by using the configuration:
13+
14+
```yaml
15+
ongr_elasticsearch:
16+
app_root_class: 'App\YourKernel'
17+
```
18+
19+
The referenced class has to exist as it is used with various ReflectionClass instances to find their folder etc., but can otherwise be empty. If the class does not exist, no mappings for `App` can be configured.
20+
621
### Mapping configuration
722

823
Here's an example of configuration containing the definitions of filter and analyzer:
@@ -29,7 +44,7 @@ ongr_elasticsearch:
2944
hosts:
3045
- 127.0.0.1:9200
3146
mappings:
32-
- AppBundle
47+
- App
3348
```
3449

3550
From 5.0 version mapping was enchased, and now you can change documents directory. See the example below:
@@ -43,7 +58,7 @@ From 5.0 version mapping was enchased, and now you can change documents director
4358
hosts:
4459
- 127.0.0.1:9200
4560
mappings:
46-
AppBundle: ~ #Document dir will be Document.
61+
App: ~ #Document dir will be Document.
4762
CustomBundle:
4863
document_dir: Entity #For this bundle will search documents in the Entity.
4964
@@ -53,7 +68,7 @@ From 5.0 version mapping was enchased, and now you can change documents director
5368
hosts:
5469
- 127.0.0.1:9200
5570
mappings:
56-
- AppBundle
71+
- App
5772
```
5873

5974
> Both mappings are valid. In the above example, you can change the directory for the particular
@@ -132,17 +147,17 @@ ongr_elasticsearch:
132147
hosts:
133148
- 127.0.0.1:9200
134149
mappings:
135-
- AppBundle
150+
- App
136151
```
137152

138153
### Document class annotations
139154

140155
Lets start with a document class example.
141156

142157
```php
143-
// src/AppBundle/Document/Content.php
158+
// src/App/Document/Content.php
144159
145-
namespace AppBundle\Document;
160+
namespace App\Document;
146161
147162
use ONGR\ElasticsearchBundle\Annotation as ES;
148163
@@ -204,8 +219,8 @@ Analyzers names must be defined in `config.yml` under the `analysis` node (read
204219
Here's an example how to add it:
205220

206221
```php
207-
// src/AppBundle/Document/Product.php
208-
namespace AppBundle\Document;
222+
// src/App/Document/Product.php
223+
namespace App\Document;
209224
210225
use ONGR\ElasticsearchBundle\Annotation as ES;
211226
@@ -238,9 +253,9 @@ To define a nested or object type you have to use `@ES\Embedded` annotation and
238253
class for this annotation. Here's an example, lets assume we have a `Product` type with `Variant` object field.
239254

240255
```php
241-
// src/AppBundle/Document/Product.php
256+
// src/App/Document/Product.php
242257
243-
namespace AppBundle\Document;
258+
namespace App\Document;
244259
245260
use ONGR\ElasticsearchBundle\Annotation as ES;
246261
@@ -257,7 +272,7 @@ class Product
257272
/**
258273
* @var ContentMetaObject
259274
*
260-
* @ES\Embedded(class="AppBundle:CategoryObject")
275+
* @ES\Embedded(class="App\Document\CategoryObject")
261276
*/
262277
private $category;
263278
@@ -268,9 +283,9 @@ class Product
268283
And the `Category` object will look like:
269284

270285
```php
271-
// src/AppBundle/Document/CategoryObject.php
286+
// src/App/Document/CategoryObject.php
272287
273-
namespace AppBundle\Document;
288+
namespace App\Document;
274289
275290
use ONGR\ElasticsearchBundle\Annotation as ES;
276291
@@ -342,9 +357,9 @@ initiating a document with multiple items you need to initialize property with t
342357
Here's an example:
343358

344359
```php
345-
// src/AppBundle/Document/Product.php
360+
// src/App/Document/Product.php
346361
347-
namespace AppBundle\Document;
362+
namespace App\Document;
348363
349364
use Doctrine\Common\Collections\ArrayCollection;
350365
use ONGR\ElasticsearchBundle\Annotation as ES;
@@ -362,7 +377,7 @@ class Product
362377
/**
363378
* @var ContentMetaObject
364379
*
365-
* @ES\Embedded(class="AppBundle:VariantObject", multiple=true)
380+
* @ES\Embedded(class="App\Document\VariantObject", multiple=true)
366381
*/
367382
private $variants;
368383
@@ -392,9 +407,9 @@ And the object:
392407

393408

394409
```php
395-
// src/AppBundle/Document/VariantObject.php
410+
// src/App/Document/VariantObject.php
396411
397-
namespace AppBundle\Document;
412+
namespace App\Document;
398413
399414
use ONGR\ElasticsearchBundle\Annotation as ES;
400415

0 commit comments

Comments
 (0)