Skip to content

Commit 6d76eaa

Browse files
committed
Backport Interface and Trait from master to 1.X
1 parent 8b09fd7 commit 6d76eaa

File tree

6 files changed

+218
-1
lines changed

6 files changed

+218
-1
lines changed

.php_cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ $finder = Symfony\CS\Finder::create()
1313
->files()
1414
->name('*.php')
1515
->exclude('Fixtures')
16+
// The next 4 files are here for forward compatibility, and are not used by
17+
// Monolog itself
18+
->exclude(__DIR__.'src/Monolog/Handler/FormattableHandlerInterface.php')
19+
->exclude(__DIR__.'src/Monolog/Handler/FormattableHandlerTrait.php')
20+
->exclude(__DIR__.'src/Monolog/Handler/ProcessableHandlerInterface.php')
21+
->exclude(__DIR__.'src/Monolog/Handler/ProcessableHandlerTrait.php')
1622
->in(__DIR__.'/src')
1723
->in(__DIR__.'/tests')
1824
;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
"scripts": {
6161
"test": [
62-
"parallel-lint . --exclude vendor",
62+
"parallel-lint . --exclude vendor --exclude src/Monolog/Handler/FormattableHandlerInterface.php --exclude src/Monolog/Handler/FormattableHandlerTrait.php --exclude src/Monolog/Handler/ProcessableHandlerInterface.php --exclude src/Monolog/Handler/ProcessableHandlerTrait.php",
6363
"phpunit"
6464
]
6565
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Monolog\Handler;
13+
14+
use Monolog\Formatter\FormatterInterface;
15+
16+
/**
17+
* Interface to describe loggers that have a formatter
18+
*
19+
* @internal This interface is present in monolog 1.x to ease forward compatibility.
20+
* @author Jordi Boggiano <j.boggiano@seld.be>
21+
*/
22+
interface FormattableHandlerInterface
23+
{
24+
/**
25+
* Sets the formatter.
26+
*
27+
* @param FormatterInterface $formatter
28+
* @return HandlerInterface self
29+
*/
30+
public function setFormatter(FormatterInterface $formatter): HandlerInterface;
31+
32+
/**
33+
* Gets the formatter.
34+
*
35+
* @return FormatterInterface
36+
*/
37+
public function getFormatter(): FormatterInterface;
38+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Monolog\Handler;
13+
14+
use Monolog\Formatter\FormatterInterface;
15+
use Monolog\Formatter\LineFormatter;
16+
17+
/**
18+
* Helper trait for implementing FormattableInterface
19+
*
20+
* @internal This interface is present in monolog 1.x to ease forward compatibility.
21+
* @author Jordi Boggiano <j.boggiano@seld.be>
22+
*/
23+
trait FormattableHandlerTrait
24+
{
25+
/**
26+
* @var FormatterInterface
27+
*/
28+
protected $formatter;
29+
30+
/**
31+
* {@inheritdoc}
32+
* @suppress PhanTypeMismatchReturn
33+
*/
34+
public function setFormatter(FormatterInterface $formatter): HandlerInterface
35+
{
36+
$this->formatter = $formatter;
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getFormatter(): FormatterInterface
45+
{
46+
if (!$this->formatter) {
47+
$this->formatter = $this->getDefaultFormatter();
48+
}
49+
50+
return $this->formatter;
51+
}
52+
53+
/**
54+
* Gets the default formatter.
55+
*
56+
* Overwrite this if the LineFormatter is not a good default for your handler.
57+
*/
58+
protected function getDefaultFormatter(): FormatterInterface
59+
{
60+
return new LineFormatter();
61+
}
62+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Monolog\Handler;
13+
14+
use Monolog\Processor\ProcessorInterface;
15+
16+
/**
17+
* Interface to describe loggers that have processors
18+
*
19+
* @internal This interface is present in monolog 1.x to ease forward compatibility.
20+
* @author Jordi Boggiano <j.boggiano@seld.be>
21+
*/
22+
interface ProcessableHandlerInterface
23+
{
24+
/**
25+
* Adds a processor in the stack.
26+
*
27+
* @param ProcessorInterface|callable $callback
28+
* @return HandlerInterface self
29+
*/
30+
public function pushProcessor(callable $callback): HandlerInterface;
31+
32+
/**
33+
* Removes the processor on top of the stack and returns it.
34+
*
35+
* @throws \LogicException In case the processor stack is empty
36+
* @return callable
37+
*/
38+
public function popProcessor(): callable;
39+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Monolog\Handler;
13+
14+
use Monolog\ResettableInterface;
15+
16+
/**
17+
* Helper trait for implementing ProcessableInterface
18+
*
19+
* @internal This interface is present in monolog 1.x to ease forward compatibility.
20+
* @author Jordi Boggiano <j.boggiano@seld.be>
21+
*/
22+
trait ProcessableHandlerTrait
23+
{
24+
/**
25+
* @var callable[]
26+
*/
27+
protected $processors = [];
28+
29+
/**
30+
* {@inheritdoc}
31+
* @suppress PhanTypeMismatchReturn
32+
*/
33+
public function pushProcessor(callable $callback): HandlerInterface
34+
{
35+
array_unshift($this->processors, $callback);
36+
37+
return $this;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function popProcessor(): callable
44+
{
45+
if (!$this->processors) {
46+
throw new \LogicException('You tried to pop from an empty processor stack.');
47+
}
48+
49+
return array_shift($this->processors);
50+
}
51+
52+
/**
53+
* Processes a record.
54+
*/
55+
protected function processRecord(array $record): array
56+
{
57+
foreach ($this->processors as $processor) {
58+
$record = $processor($record);
59+
}
60+
61+
return $record;
62+
}
63+
64+
protected function resetProcessors(): void
65+
{
66+
foreach ($this->processors as $processor) {
67+
if ($processor instanceof ResettableInterface) {
68+
$processor->reset();
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)