Skip to content

Commit

Permalink
TASK: Add fromClassAndMethodName() to LogEnvironment
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kdambekalns authored Nov 28, 2024
1 parent 8f2b430 commit 2283b7b
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions Neos.Flow/Classes/Log/Utility/LogEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,29 @@

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
*/
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 {
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -86,7 +91,6 @@ protected static function getPackageKeyFromClassName(string $className): string
}

/**
* @return array
* @Flow\CompileStatic
*/
protected static function getPackageKeys(): array
Expand All @@ -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;
Expand Down

0 comments on commit 2283b7b

Please sign in to comment.