Skip to content

Commit 134c50c

Browse files
samizdamn.gnato
authored andcommitted
Fix var_export warnings on logs
1 parent c0ddc53 commit 134c50c

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88

9+
## [4.0.1] - 2024-10-09
10+
11+
### Fixed
12+
- Use simple string instead var_export on logging, prevent `Warning: var_export does not handle circular references in LoggerHelper`
13+
914
## [4.0.0] - 2024-06-03
1015

1116
### Added
@@ -110,7 +115,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
110115
### Added
111116
- All classes.
112117

113-
[Unreleased]: https://github.com/FreeElephants/php-di/compare/4.0.0...HEAD
118+
[Unreleased]: https://github.com/FreeElephants/php-di/compare/4.0.1...HEAD
119+
[4.0.1]: https://github.com/FreeElephants/php-di/compare/4.0.0...4.0.1
114120
[4.0.0]: https://github.com/FreeElephants/php-di/compare/3.1.1...4.0.0
115121
[3.1.1]: https://github.com/FreeElephants/php-di/compare/3.1.0...3.1.1
116122
[3.1.0]: https://github.com/FreeElephants/php-di/compare/3.0.0...3.1.0

src/LoggerHelper.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ public function logLazyLoading(string $interface, $service): void
3333
$debugMsg = 'Set service type ' . $interface . ' instance by lazy load. ';
3434
$context = [
3535
'typeName' => $interface,
36-
'instance' => $service
36+
'instance' => $this->stringifyService($service),
3737
];
3838
$this->logger->debug($debugMsg, $context);
3939
}
4040

4141
public function logServiceRegistration($implementation, string $interface): void
4242
{
43-
$msg = 'Service with type ' . $interface . ' and implementation ' . $this->stringifyImplementation($implementation) . ' register. ';
43+
$implementation = $this->stringifyService($implementation);
44+
$msg = 'Service with type ' . $interface . ' and implementation ' . $implementation . ' register. ';
4445
$context = [
4546
'interface' => $interface,
4647
'implementation' => $implementation,
@@ -52,7 +53,7 @@ public function logNotMatchedTypeInstance(string $typeName, $instance): void
5253
{
5354
$context = [
5455
'typeName' => $typeName,
55-
'instance' => $instance
56+
'instance' => $this->stringifyService($instance)
5657
];
5758
$this->logger->critical('Given instance not belong to this type. Exception will be thrown. ', $context);
5859
}
@@ -62,8 +63,8 @@ public function logServiceInstanceReplacing(string $typeName, $service, $previou
6263
$debugMsg = 'Replace service type ' . $typeName . ' instance with another. ';
6364
$context = [
6465
'typeName' => $typeName,
65-
'instance' => $service,
66-
'oldInstance' => $previousServiceInstance,
66+
'instance' => $this->stringifyService($service),
67+
'oldInstance' => $this->stringifyService($previousServiceInstance),
6768
];
6869
$this->logger->debug($debugMsg, $context);
6970
return [$debugMsg, $context];
@@ -74,8 +75,8 @@ public function logRegisterServiceReplacing($implementation, string $interface,
7475
$msg = 'Replace registered service type ' . $interface . ' with another. ';
7576
$context = [
7677
'interface' => $interface,
77-
'newImplementation' => $implementation,
78-
'oldImplementation' => $oldImplementation,
78+
'newImplementation' => $this->stringifyService($implementation),
79+
'oldImplementation' => $this->stringifyService($oldImplementation),
7980
];
8081
$this->logger->debug($msg, $context);
8182
}
@@ -85,18 +86,19 @@ public function logServiceSetting(string $typeName, $service): void
8586
$debugMsg = 'Instance for service type ' . $typeName . ' was set. ';
8687
$context = [
8788
'typeName' => $typeName,
88-
'instance' => $service
89+
'instance' => $this->stringifyService($service),
8990
];
9091
$this->logger->debug($debugMsg, $context);
9192
}
9293

93-
private function stringifyImplementation($implementation): string
94+
private function stringifyService($implementation): string
9495
{
95-
// var_export does not handle circular references
96-
// handle this case
9796
if($implementation instanceof CallableBeanContainer) {
9897
$implementation = 'user defined callable';
98+
} elseif(is_object($implementation)) {
99+
$implementation = get_class($implementation);
99100
}
100-
return var_export($implementation, true);
101+
102+
return $implementation;
101103
}
102104
}

0 commit comments

Comments
 (0)