1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Loggers\MonologLoggerHandlerFactory |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Loggers; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
24
|
|
|
use TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Monolog Logger handler factory implementation. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/techdivision/import |
33
|
|
|
* @link http://www.techdivision.com |
34
|
|
|
*/ |
35
|
|
|
class MonologLoggerHandlerFactory implements MonologLoggerHandlerFactoryInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The DI container instance. |
40
|
|
|
* |
41
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
42
|
|
|
*/ |
43
|
|
|
protected $container; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Monolog Logger formatter factory instance. |
47
|
|
|
* |
48
|
|
|
* @var \TechDivision\Import\Loggers\MonologLoggerFormatterFactoryInterface |
49
|
|
|
*/ |
50
|
|
|
protected $monologLoggerFormatterFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize the factory with the DI container instance. |
54
|
|
|
* |
55
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance |
56
|
|
|
* @param \TechDivision\Import\Loggers\MonologLoggerFormatterFactoryInterface $monologLoggerFormatterFactory The Monolog Logger formatter factory instance |
57
|
|
|
*/ |
58
|
|
|
public function __construct(ContainerInterface $container, MonologLoggerFormatterFactoryInterface $monologLoggerFormatterFactory) |
59
|
|
|
{ |
60
|
|
|
$this->container = $container; |
61
|
|
|
$this->monologLoggerFormatterFactory = $monologLoggerFormatterFactory; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return's the DI container instance. |
66
|
|
|
* |
67
|
|
|
* @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance |
68
|
|
|
*/ |
69
|
|
|
protected function getContainer() |
70
|
|
|
{ |
71
|
|
|
return $this->container; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return's the Monolog Logger formatter factory instance. |
76
|
|
|
* |
77
|
|
|
* @return \TechDivision\Import\Loggers\MonologLoggerFormatterFactoryInterface The Monolog Logger formatter factory instance |
78
|
|
|
*/ |
79
|
|
|
protected function getMonologLoggerFormatterFactory() |
80
|
|
|
{ |
81
|
|
|
return $this->monologLoggerFormatterFactory; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a new logger handler instance based on the passed handler configuration. |
86
|
|
|
* |
87
|
|
|
* @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration The handler configuration |
88
|
|
|
* |
89
|
|
|
* @return \Monolog\Handler\HandlerInterface The logger handler instance |
90
|
|
|
*/ |
91
|
|
|
public function factory(HandlerConfigurationInterface $handlerConfiguration) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
// create the handler (factory) instance |
95
|
|
|
$possibleHandler = $this->getContainer()->get($handlerConfiguration->getId()); |
96
|
|
|
// query whether or not we've a factory or the instance |
97
|
|
|
if ($possibleHandler instanceof HandlerFactoryInterface) { |
98
|
|
|
$handler = $possibleHandler->factory($handlerConfiguration); |
99
|
|
|
} else { |
100
|
|
|
$handler = $possibleHandler; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// if we've a formatter, initialize the formatter also |
104
|
|
|
if ($formatterConfiguration = $handlerConfiguration->getFormatter()) { |
105
|
|
|
$handler->setFormatter($this->getMonologLoggerFormatterFactory()->factory($formatterConfiguration)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// return the handler |
109
|
|
|
return $handler; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|