-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSystemLogToMonologRector.php
80 lines (67 loc) · 3.08 KB
/
SystemLogToMonologRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Contao\Rector\Rector;
use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\System;
use PhpParser\Node;
use Psr\Log\LogLevel;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class SystemLogToMonologRector extends AbstractLegacyFrameworkCallRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Rewrites deprecated System::log() calls to Monolog', [
new CodeSample(
<<<'CODE_BEFORE'
\Contao\System::log('generic log message', __METHOD__, TL_ACCESS);
\Contao\System::log('error message', __METHOD__, TL_ERROR);
CODE_BEFORE
,
<<<'CODE_AFTER'
\Contao\System::getContainer()->get('logger')->log(\Psr\Log\LogLevel::INFO, 'generic log message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, \Contao\CoreBundle\Monolog\ContaoContext::ACCESS)]);
\Contao\System::getContainer()->get('logger')->log(\Psr\Log\LogLevel::ERROR, 'error message', ['contao' => new \Contao\CoreBundle\Monolog\ContaoContext(__METHOD__, \Contao\CoreBundle\Monolog\ContaoContext::ERROR)]);
CODE_AFTER
),
]);
}
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\StaticCall || $node instanceof Node\Expr\MethodCall);
if (!$this->isParentStaticOrMethodClassCall($node, System::class, 'log')) {
return null;
}
$args = $node->getArgs();
$message = $args[0];
$method = $args[1];
$level = $args[2]->value;
if ($level instanceof Node\Expr\ConstFetch) {
$name = $this->getName($level->name);
if (\in_array($name, [
'TL_ERROR',
'TL_ACCESS',
'TL_GENERAL',
'TL_FILES',
'TL_CRON',
'TL_FORMS',
'TL_EMAIL',
'TL_CONFIGURATION',
'TL_NEWSLETTER',
'TL_REPOSITORY',
])) {
$name = substr($name, 3);
$level = new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified(ContaoContext::class), $name);
}
}
$logLevel = 'INFO';
if ('Contao\CoreBundle\Monolog\ContaoContext::ERROR' === $this->getName($level) || 'ERROR' === $level->value) {
$logLevel = 'ERROR';
}
$context = new Node\Expr\New_(new Node\Name\FullyQualified(ContaoContext::class), $this->nodeFactory->createArgs([$method, $level]));
$levelConst = new Node\Expr\ClassConstFetch(new Node\Name\FullyQualified(LogLevel::class), $logLevel);
$container = new Node\Expr\StaticCall(new Node\Name\FullyQualified(System::class), 'getContainer');
$service = new Node\Expr\MethodCall($container, 'get', [new Node\Arg(new Node\Scalar\String_('monolog.logger.contao'))]);
$node = new Node\Expr\MethodCall($service, 'log', $this->nodeFactory->createArgs([$levelConst, $message, ['contao' => $context]]));
return $node;
}
}