1
|
|
|
<?php |
2
|
|
|
namespace FOS\ElasticaBundle\Doctrine; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
5
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use FOS\ElasticaBundle\Persister\Event\Events; |
8
|
|
|
use FOS\ElasticaBundle\Persister\Event\PersistEvent; |
9
|
|
|
use FOS\ElasticaBundle\Provider\PagerInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
class RegisterListenersService |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var EventDispatcherInterface |
16
|
|
|
*/ |
17
|
|
|
private $dispatcher; |
18
|
|
|
|
19
|
9 |
|
public function __construct(EventDispatcherInterface $dispatcher) |
20
|
|
|
{ |
21
|
9 |
|
$this->dispatcher = $dispatcher; |
22
|
9 |
|
} |
23
|
|
|
|
24
|
8 |
|
public function register(ObjectManager $manager, PagerInterface $pager, array $options) |
25
|
|
|
{ |
26
|
8 |
|
$options = array_replace([ |
27
|
8 |
|
'clear_object_manager' => true, |
28
|
|
|
'debug_logging' => false, |
29
|
|
|
'sleep' => 0, |
30
|
8 |
|
], $options); |
31
|
|
|
|
32
|
8 |
|
if ($options['clear_object_manager']) { |
33
|
2 |
|
$this->addListener($pager, Events::POST_INSERT_OBJECTS, function() use ($manager) { |
34
|
1 |
|
$manager->clear(); |
35
|
2 |
|
}); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
if ($options['sleep']) { |
39
|
2 |
|
$this->addListener($pager, Events::POST_INSERT_OBJECTS, function() use ($options) { |
40
|
1 |
|
usleep($options['sleep']); |
41
|
2 |
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
8 |
View Code Duplication |
if (false == $options['debug_logging'] && $manager instanceof EntityManagerInterface) { |
|
|
|
|
45
|
1 |
|
$configuration = $manager->getConnection()->getConfiguration(); |
46
|
1 |
|
$logger = $configuration->getSQLLogger(); |
47
|
|
|
|
48
|
1 |
|
$this->addListener($pager, Events::PRE_FETCH_OBJECTS, function() use ($configuration) { |
49
|
|
|
$configuration->setSQLLogger(null); |
50
|
1 |
|
}); |
51
|
|
|
|
52
|
1 |
|
$this->addListener($pager, Events::PRE_INSERT_OBJECTS, function() use ($configuration, $logger) { |
53
|
|
|
$configuration->setSQLLogger($logger); |
54
|
1 |
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
View Code Duplication |
if (false == $options['debug_logging'] && $manager instanceof DocumentManager) { |
|
|
|
|
58
|
|
|
$configuration = $manager->getConnection()->getConfiguration(); |
59
|
|
|
$logger = $configuration->getLoggerCallable(); |
60
|
|
|
|
61
|
|
|
$this->addListener($pager, Events::PRE_FETCH_OBJECTS, function() use ($configuration) { |
62
|
|
|
$configuration->setLoggerCallable(null); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$this->addListener($pager, Events::PRE_INSERT_OBJECTS, function() use ($configuration, $logger) { |
66
|
|
|
$configuration->setLoggerCallable($logger); |
67
|
|
|
}); |
68
|
|
|
} |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param PagerInterface $pager |
73
|
|
|
* @param string $eventName |
74
|
|
|
* @param \Closure $callable |
75
|
|
|
*/ |
76
|
|
|
private function addListener(PagerInterface $pager, $eventName, \Closure $callable) |
77
|
|
|
{ |
78
|
5 |
|
$this->dispatcher->addListener($eventName, function(PersistEvent $event) use ($pager, $callable) { |
79
|
4 |
|
if ($event->getPager() !== $pager) { |
80
|
2 |
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
call_user_func_array($callable, func_get_args()); |
84
|
5 |
|
}); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.