Skip to content

Commit 7ab3565

Browse files
committed
Only register logging listeners when logging is enabled
1 parent 518fd4c commit 7ab3565

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/LdapServiceProvider.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function boot(): void
3030
$this->registerLogging();
3131
$this->registerCommands();
3232
$this->registerConfiguration();
33-
$this->registerEventListeners();
3433
$this->registerLdapConnections();
3534

3635
if ($this->app->runningUnitTests()) {
@@ -84,6 +83,14 @@ protected function registerLogging(): void
8483
Config::get('ldap.logging.channel')
8584
)
8685
);
86+
87+
Event::listen('LdapRecord\Laravel\Events\*', function ($eventName, array $events) {
88+
collect($events)->filter(function ($event) {
89+
return $event instanceof LoggableEvent && $event->shouldLogEvent();
90+
})->each(function (LoggableEvent $event) {
91+
Log::log($event->getLogLevel(), $event->getLogMessage());
92+
});
93+
});
8794
}
8895

8996
/**
@@ -114,20 +121,6 @@ protected function registerConfiguredConnections(): void
114121
}
115122
}
116123

117-
/**
118-
* Registers the LDAP event listeners.
119-
*/
120-
protected function registerEventListeners(): void
121-
{
122-
Event::listen('LdapRecord\Laravel\Events\*', function ($eventName, array $events) {
123-
collect($events)->filter(function ($event) {
124-
return $event instanceof LoggableEvent && $event->shouldLogEvent();
125-
})->each(function (LoggableEvent $event) {
126-
Log::log($event->getLogLevel(), $event->getLogMessage());
127-
});
128-
});
129-
}
130-
131124
/**
132125
* Register the connections that exist in the environment file.
133126
*/

tests/Feature/LdapUserSynchronizerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Facades\Event;
66
use Illuminate\Support\Facades\Hash;
7+
use LdapRecord\Container;
78
use LdapRecord\Laravel\Events\Import\Importing;
89
use LdapRecord\Laravel\Events\Import\Synchronized;
910
use LdapRecord\Laravel\Events\Import\Synchronizing;
@@ -17,6 +18,7 @@ class LdapUserSynchronizerTest extends DatabaseUserProviderTest
1718

1819
public function test_eloquent_model_can_be_set()
1920
{
21+
ray(Container::getInstance());
2022
$synchronizer = new UserSynchronizer(TestUserModelStub::class, []);
2123

2224
$this->assertSame(TestUserModelStub::class, $synchronizer->getEloquentModel());
@@ -25,6 +27,7 @@ public function test_eloquent_model_can_be_set()
2527

2628
public function test_config_can_be_set()
2729
{
30+
ray(Container::getInstance());
2831
$synchronizer = new UserSynchronizer(TestUserModelStub::class, []);
2932

3033
$config = ['foo' => 'bar'];
@@ -36,6 +39,7 @@ public function test_config_can_be_set()
3639

3740
public function test_new_ldap_user_has_guid_and_domain_set()
3841
{
42+
ray(Container::getInstance());
3943
Event::fake();
4044

4145
$ldapModel = $this->getMockLdapModel();
@@ -80,6 +84,7 @@ public function test_new_ldap_user_has_attributes_synchronized()
8084

8185
public function test_new_ldap_user_has_attributes_synchronized_via_handler()
8286
{
87+
ray(Container::getInstance());
8388
Event::fake();
8489

8590
$ldapModel = $this->getMockLdapModel(['cn' => 'john', 'mail' => '[email protected]']);
@@ -132,6 +137,7 @@ public function test_ldap_sync_attributes_can_be_string()
132137

133138
public function test_password_is_synchronized_when_enabled()
134139
{
140+
ray(Container::getInstance());
135141
Event::fake();
136142

137143
$ldapModel = $this->getMockLdapModel();

0 commit comments

Comments
 (0)