Skip to content

Commit

Permalink
Use reflection library inside util.log.LogConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 28, 2024
1 parent d7faf69 commit 1b096e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Logging change log

## ?.?.? / ????-??-??

* Changed to use reflection library inside `util.log.LogConfiguration`
(@thekid)

## 11.1.0 / 2024-03-24

* Made compatible with XP 12 - @thekid
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^12.0 | ^11.0 | ^10.0",
"xp-framework/reflection": "^3.0 | ^2.0",
"php" : ">=7.0.0"
},
"require-dev" : {
Expand Down
36 changes: 4 additions & 32 deletions src/main/php/util/log/LogConfiguration.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace util\log;

use lang\{FormatException, IllegalArgumentException, Throwable, XPClass};
use lang\{FormatException, IllegalArgumentException, Throwable, Reflection};
use util\PropertyAccess;

/**
Expand Down Expand Up @@ -46,34 +46,6 @@ public function __construct(PropertyAccess $properties) {
}
}

/**
* Creates a new log appender from a class and arguments, which are either
* a list matching the constructor argument order or a map of named arguments.
*
* @param lang.XPClass $class
* @param var[]|[:var] $args
* @return util.log.LogAppender
* @throws lang.FormatException
*/
private function newAppender($class, $args) {
if (null === ($c= $class->getConstructor())) {
return $class->newInstance();
} else if (0 === key($args)) {
return $c->newInstance($args);
} else {
$pass= [];
foreach ($c->getParameters() as $param) {
$name= $param->getName();
$pass[]= $args[$name] ?? $param->getDefaultValue();
unset($args[$name]);
}
if ($args) {
throw new FormatException('Unknown named argument(s) '.implode(', ', array_keys($args)));
}
return $c->newInstance($pass);
}
}

/**
* Returns log appenders for a given property file section
*
Expand All @@ -95,14 +67,14 @@ private function appendersFor($properties, $section) {
// Class
if ($class= $properties->readString($section, 'class', null)) {
try {
$appender= $this->newAppender(XPClass::forName($class), $properties->readArray($section, 'args', []));
$appender= Reflection::type($class)->newInstance(...$properties->readArray($section, 'args', []));
if ($layout= $properties->readString($section, 'layout', null)) {
if (false !== ($p= strcspn($layout, '('))) {
$appender->setLayout(XPClass::forName(substr($layout, 0, $p))->newInstance(...eval(
$appender->setLayout(Reflection::type(substr($layout, 0, $p))->newInstance(...eval(
'return ['.substr($layout, $p + 1, -1).'];'
)));
} else {
$appender->setLayout(XPClass::forName($layout)->newInstance());
$appender->setLayout(Reflection::type($layout)->newInstance());
}
}
} catch (Throwable $e) {
Expand Down

0 comments on commit 1b096e2

Please sign in to comment.