Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add fromClassAndMethodName() to LogEnvironment #3419

Open
wants to merge 1 commit into
base: 8.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we also allow passing an object instead of the class name? That way you could just pass $this in most cases.

public static function fromClassAndMethodName(string|object $class, string $methodName): array
{
    $className = is_object($class) ? get_class($object) : $className;
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that make it easier to use? You can even use $this::class if you like…

{
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
Loading