Skip to content

Commit 3fd00a9

Browse files
author
Simonas Šerlinskas
committed
added events for manager factory
1 parent e504c75 commit 3fd00a9

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

Event/Events.php

+11
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ final class Events
3030
* The POST_COMMIT event occurs after committing queries to ES
3131
*/
3232
const POST_COMMIT = 'es.post_commit';
33+
34+
/**
35+
* The PRE_MANAGER_CREATE event occurs before manager is created, right after client is initiated.
36+
* You can modify anything in the core elasticsearch-php client by this event.
37+
*/
38+
const PRE_MANAGER_CREATE = 'es.pre_manager_create';
39+
40+
/**
41+
* The POST_MANAGER_CREATE event occurs after manager is created.
42+
*/
43+
const POST_MANAGER_CREATE = 'es.post_manager_create';
3344
}

Event/PostCreateManagerEvent.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Event;
13+
14+
use ONGR\ElasticsearchBundle\Service\Manager;
15+
use Symfony\Component\EventDispatcher\Event;
16+
17+
class PostCreateManagerEvent extends Event
18+
{
19+
/**
20+
* @var Manager
21+
*/
22+
private $manager;
23+
24+
/**
25+
* PostCreateManagerEvent constructor.
26+
*
27+
* @param Manager $manager
28+
*/
29+
public function __construct(Manager $manager)
30+
{
31+
$this->manager = $manager;
32+
}
33+
34+
/**
35+
* @return Manager
36+
*/
37+
public function getManager()
38+
{
39+
return $this->manager;
40+
}
41+
42+
/**
43+
* @param Manager $manager
44+
*/
45+
public function setManager($manager)
46+
{
47+
$this->manager = $manager;
48+
}
49+
}

Event/PreCreateManagerEvent.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Event;
13+
14+
use Elasticsearch\ClientBuilder;
15+
use Symfony\Component\EventDispatcher\Event;
16+
17+
class PreCreateManagerEvent extends Event
18+
{
19+
/**
20+
* @var ClientBuilder
21+
*/
22+
private $client;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $indexSettings;
28+
29+
/**
30+
* CreateManagerEvent constructor.
31+
*
32+
* @param PreClientBuilder $client
33+
* @param $indexSettings array
34+
*/
35+
public function __construct(ClientBuilder $client, &$indexSettings)
36+
{
37+
$this->client = $client;
38+
$this->indexSettings = $indexSettings;
39+
}
40+
41+
/**
42+
* @return ClientBuilder
43+
*/
44+
public function getClient()
45+
{
46+
return $this->client;
47+
}
48+
49+
/**
50+
* @param ClientBuilder $client
51+
*/
52+
public function setClient($client)
53+
{
54+
$this->client = $client;
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
public function getIndexSettings()
61+
{
62+
return $this->indexSettings;
63+
}
64+
65+
/**
66+
* @param array $indexSettings
67+
*/
68+
public function setIndexSettings($indexSettings)
69+
{
70+
$this->indexSettings = $indexSettings;
71+
}
72+
}

Exception/BulkWithErrorsException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ public function getResponse()
3434
{
3535
return $this->response;
3636
}
37-
}
37+
}

Service/ManagerFactory.php

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace ONGR\ElasticsearchBundle\Service;
1313

1414
use Elasticsearch\ClientBuilder;
15+
use ONGR\ElasticsearchBundle\Event\Events;
16+
use ONGR\ElasticsearchBundle\Event\PostCreateManagerEvent;
17+
use ONGR\ElasticsearchBundle\Event\PreCreateManagerEvent;
1518
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
1619
use ONGR\ElasticsearchBundle\Result\Converter;
1720
use Psr\Log\LoggerInterface;
@@ -130,6 +133,12 @@ public function createManager($managerName, $connection, $analysis, $managerConf
130133
),
131134
];
132135

136+
$this->eventDispatcher &&
137+
$this->eventDispatcher->dispatch(
138+
Events::POST_MANAGER_CREATE,
139+
new PreCreateManagerEvent($client, $indexSettings)
140+
);
141+
133142
$manager = new Manager(
134143
$managerName,
135144
$managerConfig,
@@ -147,6 +156,9 @@ public function createManager($managerName, $connection, $analysis, $managerConf
147156
$manager->setEventDispatcher($this->eventDispatcher);
148157
$manager->setBulkCommitSize($managerConfig['bulk_size']);
149158

159+
$this->eventDispatcher &&
160+
$this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager));
161+
150162
return $manager;
151163
}
152164
}

0 commit comments

Comments
 (0)