From 2283b7b893fe2048067f1994969ef55e9d817c14 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Thu, 28 Nov 2024 18:57:23 +0100 Subject: [PATCH] TASK: Add fromClassAndMethodName() to LogEnvironment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This does some code cleanup and adds a new method `fromClassAndMethodName($className, $methodName)` to the `LogEnvironment`. It be used like this: LogEnvironment::fromClassAndMethodName(__CLASS__, __METHOD__) and will return useful data – as opposed to `LogEnvironment::fromMethodName(__METHOD__)`, which only works for static methods (`SomeClass::someMethod` or closures). --- .../Classes/Log/Utility/LogEnvironment.php | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/Neos.Flow/Classes/Log/Utility/LogEnvironment.php b/Neos.Flow/Classes/Log/Utility/LogEnvironment.php index 8d3dec8c52..105fdddfbf 100644 --- a/Neos.Flow/Classes/Log/Utility/LogEnvironment.php +++ b/Neos.Flow/Classes/Log/Utility/LogEnvironment.php @@ -15,27 +15,20 @@ use Neos\Flow\Core\Bootstrap; use Neos\Flow\ObjectManagement\ObjectManagerInterface; -use Neos\Flow\Package\PackageInterface; use Neos\Flow\Package\PackageKeyAwareInterface; use Neos\Flow\Package\PackageManager; use Neos\Flow\Annotations as Flow; abstract class LogEnvironment { - /** - * @var array - */ - protected static $packageKeys = []; + protected static array $packageKeys = []; - /** - * @var bool - */ - protected static $initialized = false; + protected static bool $initialized = false; /** * Returns an array containing the log environment variables - * under the key FLOW_LOG_ENVIRONMENT to be set as part of the additional data - * in an log method call. + * under the key FLOW_LOG_ENVIRONMENT to be set as part of + * the additional data in an log method call. * * @param string $methodName * @return array @@ -43,8 +36,8 @@ abstract class LogEnvironment public static function fromMethodName(string $methodName): array { if (strpos($methodName, '::') > 0) { - list($className, $functionName) = explode('::', $methodName); - } elseif (substr($methodName, -9, 9) === '{closure}') { + [$className, $functionName] = explode('::', $methodName); + } elseif (str_ends_with($methodName, '{closure}')) { $className = substr($methodName, 0, -9); $functionName = '{closure}'; } else { @@ -61,9 +54,21 @@ public static function fromMethodName(string $methodName): array } /** - * @param string $className - * @return string + * Returns an array containing the log environment variables + * under the key FLOW_LOG_ENVIRONMENT to be set as part of the + * additional data in an log method call. */ + public static function fromClassAndMethodName(string $className, string $methodName): array + { + return [ + 'FLOW_LOG_ENVIRONMENT' => [ + 'packageKey' => self::getPackageKeyFromClassName($className), + 'className' => $className, + 'methodName' => $methodName + ] + ]; + } + protected static function getPackageKeyFromClassName(string $className): string { $packageKeys = static::getPackageKeys(); @@ -73,7 +78,7 @@ protected static function getPackageKeyFromClassName(string $className): string $packageKeyCandidate = $determinedPackageKey; foreach ($classPathArray as $classPathSegment) { - $packageKeyCandidate = $packageKeyCandidate . '.' . $classPathSegment; + $packageKeyCandidate .= '.' . $classPathSegment; if (!isset($packageKeys[$packageKeyCandidate])) { continue; @@ -86,7 +91,6 @@ protected static function getPackageKeyFromClassName(string $className): string } /** - * @return array * @Flow\CompileStatic */ protected static function getPackageKeys(): array @@ -99,7 +103,6 @@ protected static function getPackageKeys(): array /** @var PackageManager $packageManager */ $packageManager = Bootstrap::$staticObjectManager->get(PackageManager::class); - /** @var PackageInterface $package */ foreach ($packageManager->getAvailablePackages() as $package) { if ($package instanceof PackageKeyAwareInterface) { self::$packageKeys[$package->getPackageKey()] = true;