Skip to content

Commit bf6cb92

Browse files
committed
add hasListeners()
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 9a299ed commit bf6cb92

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

lib/private/EventDispatcher/EventDispatcher.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,17 @@
3333
use OCP\EventDispatcher\ABroadcastedEvent;
3434
use OCP\EventDispatcher\Event;
3535
use OCP\EventDispatcher\IEventDispatcher;
36-
use OCP\IContainer;
3736
use OCP\IServerContainer;
3837
use Psr\Log\LoggerInterface;
3938
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
4039
use function get_class;
4140

4241
class EventDispatcher implements IEventDispatcher {
43-
/** @var SymfonyDispatcher */
44-
private $dispatcher;
45-
46-
/** @var IContainer */
47-
private $container;
48-
49-
/** @var LoggerInterface */
50-
private $logger;
51-
52-
public function __construct(SymfonyDispatcher $dispatcher,
53-
IServerContainer $container,
54-
LoggerInterface $logger) {
55-
$this->dispatcher = $dispatcher;
56-
$this->container = $container;
57-
$this->logger = $logger;
58-
42+
public function __construct(
43+
private SymfonyDispatcher $dispatcher,
44+
private IServerContainer $container,
45+
private LoggerInterface $logger,
46+
) {
5947
// inject the event dispatcher into the logger
6048
// this is done here because there is a cyclic dependency between the event dispatcher and logger
6149
if ($this->logger instanceof Log || $this->logger instanceof Log\PsrLoggerAdapter) {
@@ -86,6 +74,10 @@ public function addServiceListener(string $eventName,
8674
$this->addListener($eventName, $listener, $priority);
8775
}
8876

77+
public function hasListeners(string $eventName): bool {
78+
return $this->dispatcher->hasListeners($eventName);
79+
}
80+
8981
/**
9082
* @deprecated
9183
*/

lib/private/Log.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,18 @@ public function debug(string $message, array $context = []) {
207207
*/
208208
public function log(int $level, string $message, array $context = []) {
209209
$minLevel = $this->getLogLevel($context);
210-
if ($level < $minLevel && (($this->crashReporters?->hasReporters() ?? false) === false)) {
211-
return; // we already know that log will be fully ignored
210+
if ($level < $minLevel
211+
&& (($this->crashReporters?->hasReporters() ?? false) === false)
212+
&& (($this->eventDispatcher?->hasListeners(BeforeMessageLoggedEvent::class) ?? false) === false)) {
213+
return; // no crash reporter, no listeners, we can stop for lower log level
212214
}
213215

214216
array_walk($context, [$this->normalizer, 'format']);
215217

216218
$app = $context['app'] ?? 'no app in context';
217219
$entry = $this->interpolateMessage($context, $message);
218220

219-
if ($this->eventDispatcher) {
220-
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));
221-
}
221+
$this->eventDispatcher?->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));
222222

223223
$hasBacktrace = isset($entry['exception']);
224224
$logBacktrace = $this->config->getValue('log.backtrace', false);

lib/public/EventDispatcher/IEventDispatcher.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public function removeListener(string $eventName, callable $listener): void;
6969
*/
7070
public function addServiceListener(string $eventName, string $className, int $priority = 0): void;
7171

72+
/**
73+
* @template T of \OCP\EventDispatcher\Event
74+
* @param string $eventName preferably the fully-qualified class name of the Event sub class
75+
*
76+
* @return bool TRUE if event has registered listeners
77+
* @since 29.0.0
78+
*/
79+
public function hasListeners(string $eventName): bool;
80+
7281
/**
7382
* @template T of \OCP\EventDispatcher\Event
7483
* @param string $eventName

0 commit comments

Comments
 (0)